{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 数组类型" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from numpy import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "之前已经看过整数数组和布尔数组,除此之外还有浮点数数组和复数数组。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 复数数组" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "产生一个复数数组:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = array([1 + 1j, 2, 3, 4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Python**会自动判断数组的类型:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('complex128')" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "对于复数我们可以查看它的实部和虚部:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3., 4.])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.real" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1., 0., 0., 0.])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.imag" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "还可以设置它们的值:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a.imag = [1,2,3,4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "查看 `a`:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.+1.j, 2.+2.j, 3.+3.j, 4.+4.j])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "查看复共轭:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.-1.j, 2.-2.j, 3.-3.j, 4.-4.j])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.conj()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "事实上,这些属性方法可以用在浮点数或者整数数组上:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array([0.,1,2,3])\n", "a.dtype" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0., 1., 2., 3.])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.real" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0., 0., 0., 0.])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.imag" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0., 1., 2., 3.])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.conj()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "但这里,虚部是只读的,并不能修改它的值:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "array does not have imaginary part to set", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# 会报错\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimag\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: array does not have imaginary part to set" ] } ], "source": [ "# 会报错\n", "a.imag = [1,2,3,4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 指定数组类型" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "之前已经知道,构建数组的时候,数组会根据传入的内容自动判断类型:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = array([0,1.0,2,3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "对于浮点数,默认为双精度:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "查看所用字节(`8 bytes * 4`):" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "32" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.nbytes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当然,我们也可以在构建的时候指定类型:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = array([0,1.0,2,3],\n", " dtype=float32)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "此时类型为单精度浮点数:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('float32')" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "查看所用字节(`4 bytes * 4`):" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.nbytes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "除此之外,还可以指定有无符号,例如无符号整数:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('uint8')" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array([0,1,2,3],\n", " dtype=uint8)\n", "a.dtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`uint8` 只使用一个字节,表示 `0` 到 `255` 的整数。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "还可以从二进制数据中读取。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "先写入二进制数据:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = array([102,111,212], \n", " dtype=uint8)\n", "a.tofile('foo.dat')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "从数据中读入,要指定类型:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([102, 111, 111], dtype=uint8)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = frombuffer('foo', \n", " dtype=uint8)\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "清理数据文件:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import os\n", "os.remove('foo.dat')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`0-255` 的数字可以表示ASCⅡ码,我们可以用 ord 函数来查看字符的ASCⅡ码值:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "102" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ord('f')" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "83" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ord('S')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numpy 类型" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "具体如下:\n", "\n", "|基本类型|可用的**Numpy**类型|备注\n", "|--|--|--\n", "|布尔型|`bool`|占1个字节\n", "|整型|`int8, int16, int32, int64, int128, int`| `int` 跟**C**语言中的 `long` 一样大\n", "|无符号整型|`uint8, uint16, uint32, uint64, uint128, uint`| `uint` 跟**C**语言中的 `unsigned long` 一样大\n", "|浮点数| `float16, float32, float64, float, longfloat`|默认为双精度 `float64` ,`longfloat` 精度大小与系统有关\n", "|复数| `complex64, complex128, complex, longcomplex`| 默认为 `complex128` ,即实部虚部都为双精度\n", "|字符串| `string, unicode` | 可以使用 `dtype=S4` 表示一个4字节字符串的数组\n", "|对象| `object` |数组中可以使用任意值|\n", "|Records| `void` ||\n", "|时间| `datetime64, timedelta64` ||\n", "\n", "任意类型的数组:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = array([1,1.2,'hello', [10,20,30]], \n", " dtype=object)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "乘法:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([2, 2.4, 'hellohello', [10, 20, 30, 10, 20, 30]], dtype=object)" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 类型转换" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "转换数组的类型:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.5, -3. ], dtype=float32)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array([1.5, -3], \n", " dtype=float32)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### asarray 函数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用 `asarray` 函数:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([ 1.5, -3. ])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "asarray(a, dtype=float64)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 253], dtype=uint8)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "asarray(a, dtype=uint8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`asarray` 不会修改原来数组的值:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.5, -3. ], dtype=float32)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "但当类型相同的时候,`asarray` 并不会产生新的对象,而是使用同一个引用:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b = asarray(a, dtype=float32)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b is a " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "这么做的好处在与,`asarray` 不仅可以作用于数组,还可以将其他类型转化为数组。\n", "\n", "有些时候为了保证我们的输入值是数组,我们需要将其使用 `asarray` 转化,当它已经是数组的时候,并不会产生新的对象,这样保证了效率。" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "asarray([1,2,3,4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### astype 方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`astype` 方法返回一个新数组:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.5, -3. ])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.astype(float64)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 253], dtype=uint8)" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.astype(uint8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "astype也不会改变原来数组的值:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.5, -3. ], dtype=float32)" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "另外,`astype` 总是返回原来数组的一份复制,即使转换的类型是相同的:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1.5 -3. ]\n", "[ 1.5 -3. ]\n" ] } ], "source": [ "b = a.astype(float32)\n", "print a\n", "print b" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a is b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### view 方法" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array((1,2,3,4), dtype=int32)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`view` 会将 `a` 在内存中的表示看成是 `uint8` 进行解析:" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0], dtype=uint8)" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a.view(uint8)\n", "b" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1073741824, 2, 3, 4])" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0] = 2**30\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "修改 `a` 会修改 `b` 的值,因为共用一块内存:" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0, 0, 0, 64, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0], dtype=uint8)" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }