{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 数组属性方法总结" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "||作用|\n", "|-|-|\n", "|1|**基本属性**\n", "|`a.dtype`|数组元素类型 `float32,uint8,...`\n", "|`a.shape`|数组形状 `(m,n,o,...)`\n", "|`a.size`|数组元素数\n", "|`a.itemsize`|每个元素占字节数\n", "|`a.nbytes`|所有元素占的字节\n", "|`a.ndim`|数组维度\n", "|2|**形状相关**\n", "|`a.flat`|所有元素的迭代器\n", "|`a.flatten()`|返回一个1维数组的复制\n", "|`a.ravel()`|返回一个1维数组,高效\n", "|`a.resize(new_size)`|改变形状\n", "|`a.swapaxes(axis1, axis2)`|交换两个维度的位置\n", "|`a.transpose(*axex)`|交换所有维度的位置\n", "|`a.T`|转置,`a.transpose()`\n", "|`a.squeeze()`| 去除所有长度为1的维度\n", "|3|**填充复制**\n", "|`a.copy()`| 返回数组的一个复制\n", "|`a.fill(value)`| 将数组的元组设置为特定值\n", "|4|**转化**\n", "|`a.tolist()`|将数组转化为列表\n", "|`a.tostring()`|转换为字符串\n", "|`a.astype(dtype)`|转化为指定类型\n", "|`a.byteswap(False)`|转换大小字节序\n", "|`a.view(type_or_dtype)`|生成一个使用相同内存,但使用不同的表示方法的数组\n", "|5|**复数**\n", "|`a.imag`|虚部\n", "|`a.real`|实部\n", "|`a.conjugate()`|复共轭\n", "|`a.conj()`|复共轭(缩写)\n", "|6|**保存**\n", "|`a.dump(file)`|将二进制数据存在file中\n", "|`a.dump()`|将二进制数据表示成字符串\n", "|`a.tofile(fid, sep=\"\",format=\"%s\")`|格式化ASCⅡ码写入文件\n", "|7|**查找排序**\n", "|`a.nonzero()`|返回所有非零元素的索引\n", "|`a.sort(axis=-1)`|沿某个轴排序\n", "|`a.argsort(axis=-1)`|沿某个轴,返回按排序的索引\n", "|`a.searchsorted(b)`|返回将b中元素插入a后能保持有序的索引值\n", "|8|**元素数学操作**\n", "|`a.clip(low, high)`|将数值限制在一定范围内\n", "|`a.round(decimals=0)`|近似到指定精度\n", "|`a.cumsum(axis=None)`|累加和\n", "|`a.cumprod(axis=None)`|累乘积\n", "|9|**约简操作**\n", "|`a.sum(axis=None)`|求和\n", "|`a.prod(axis=None)`|求积\n", "|`a.min(axis=None)`|最小值\n", "|`a.max(axis=None)`|最大值\n", "|`a.argmin(axis=None)`|最小值索引\n", "|`a.argmax(axis=None)`|最大值索引\n", "|`a.ptp(axis=None)`|最大值减最小值\n", "|`a.mean(axis=None)`|平均值\n", "|`a.std(axis=None)`|标准差\n", "|`a.var(axis=None)`|方差\n", "|`a.any(axis=None)`|只要有一个不为0,返回真,逻辑或\n", "|`a.all(axis=None)`|所有都不为0,返回真,逻辑与" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from numpy import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 基本属性" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3],\n", " [4, 5, 6, 7]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array([[0, 1, 2, 3], [4, 5, 6, 7]])\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "数组元素属性:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('int32')" ] }, "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, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "(2L, 4L)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "元素数目:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.size" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "元素占字节大小:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.itemsize" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "所有元素所占字节:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "32" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.nbytes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "数据维度:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.ndim" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 形状相关" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3]\n", "[4 5 6 7]\n" ] } ], "source": [ "for row in a:\n", " print row" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "所有元素的迭代器:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n" ] } ], "source": [ "for elt in a.flat:\n", " print elt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "所有元素组成的一维数组,按照行排列:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.flatten()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.ravel()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "重新改变形状:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1],\n", " [2, 3],\n", " [4, 5],\n", " [6, 7]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.resize((4,2))\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "交换这两个轴的顺序:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 2, 4, 6],\n", " [1, 3, 5, 7]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.swapaxes(0,1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "转置:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[0, 2, 4, 6],\n", " [1, 3, 5, 7]])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.transpose()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "转置:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 2, 4, 6],\n", " [1, 3, 5, 7]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.T" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(3L,)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a2 = array([1,2,3])\n", "a2.shape" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1L, 3L, 1L)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a2.resize((1,3,1))\n", "a2.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "去除长度为1的维度:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(3L,)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a2 = a2.squeeze()\n", "a2.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 填充复制" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "复制:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1],\n", " [2, 3],\n", " [4, 5],\n", " [6, 7]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a.copy()\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "复制不影响原来的数组:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[-1, 1],\n", " [ 2, 3],\n", " [ 4, 5],\n", " [ 6, 7]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[0][0] = -1\n", "b # First value changed" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1],\n", " [2, 3],\n", " [4, 5],\n", " [6, 7]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a # original not changed because b is a copy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "填充:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[4, 4],\n", " [4, 4],\n", " [4, 4],\n", " [4, 4]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.fill(4)\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 转化" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "转化为列表:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[0, 1], [2, 3], [4, 5], [6, 7]]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.tolist()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "转化为字符串:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "'\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x06\\x00\\x00\\x00\\x07\\x00\\x00\\x00'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.tostring()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "改变数组元素类型:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 1.],\n", " [ 2., 3.],\n", " [ 4., 5.],\n", " [ 6., 7.]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.astype(float)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 16777216],\n", " [ 33554432, 50331648],\n", " [ 67108864, 83886080],\n", " [100663296, 117440512]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a.copy()\n", "b.byteswap(False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "将它看成16位整数:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[0, 0, 1, 0],\n", " [2, 0, 3, 0],\n", " [4, 0, 5, 0],\n", " [6, 0, 7, 0]], dtype=int16)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.view(dtype=int16)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 复数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "实部:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1., 3., 5.])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = array([1+2j, 3+4j, 5+6j])\n", "b.real" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "虚部:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "array([ 2., 4., 6.])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.imag" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "共轭:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.-2.j, 3.-4.j, 5.-6.j])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.conj()" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.-2.j, 3.-4.j, 5.-6.j])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.conjugate()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 保存" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "保存成文本:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a.dump(\"file.txt\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "字符串:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "'\\x80\\x02cnumpy.core.multiarray\\n_reconstruct\\nq\\x01cnumpy\\nndarray\\nq\\x02K\\x00\\x85U\\x01b\\x87Rq\\x03(K\\x01\\x8a\\x01\\x04\\x8a\\x01\\x02\\x86cnumpy\\ndtype\\nq\\x04U\\x02i4K\\x00K\\x01\\x87Rq\\x05(K\\x03U\\x01