{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Numpy\n", "\n", "## Numpy特征和导入\n", "\n", "1. 用于多维数组的第三方Python包\n", "2. 更接近于底层和硬件 (高效)\n", "3. 专注于科学计算 (方便)\n", "4. 导入包:`import numpy as np`\n", "\n", "## list转为数组\n", "\n", "1. `a = np.array([0,1,2,3])`\n", "2. 输出为:`[0 1 2 3]`\n", "3. 数据类型:`<type 'numpy.ndarray'>`\n", "\n", "## 一维数组\n", "\n", "1. a = np.array([1,2,3,4])属性\n", " `a.ndim`–>维度为1\n", " `a.shape`–>形状,返回(4,)\n", " `len(a)`–>长度,4\n", "2. 访问数组`a[1:5:2]`下标1-5,下标关系+2\n", "3. 逆序`a[::-1]`\n", "\n", "## 多维数组\n", "\n", "1. 二维:`a = np.array([[0,1,2,3],[1,2,3,4]])`\n", " 输出为:\n", " ```\n", " [[0 1 2 3]\n", " [1 2 3 4]]\n", " ```\n", " `a.ndm` –>2\n", " `a.shape` –>(2,4)–>行数,列数\n", " `len(a)` –>2–>第一维大小\n", "\n", "2. 三维:`a = np.array([[[0],[1]],[[2],[4]]])`\n", " `a.shape`–>(2,2,1)\n", "\n", "## 用函数创建数组\n", "\n", "1. np.arange()`\n", " ```\n", " a = np.arange(0, 10)\n", " b = np.arange(10)\n", " c = np.arange(0,10,2)\n", " ```\n", " 输出:\n", " ```\n", " [0 1 2 3 4 5 6 7 8 9]\n", " [0 1 2 3 4 5 6 7 8 9]\n", " [0 2 4 6 8]\n", " ```\n", "2. `np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)`\n", " 等距离产生num个数\n", "3. `np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)`\n", " 以log函数取\n", "\n", "## 常用数组\n", "\n", "1. `a = np.ones((3,3))`\n", " 输出:\n", " ```\n", " [[ 1. 1. 1.]\n", " [ 1. 1. 1.]\n", " [ 1. 1. 1.]]\n", " ```\n", "2. `np.zeros((3,3))`\n", "3. `np.eye(2)`单位矩阵\n", "4. `np.diag([1,2,3],k=0)`对角矩阵,k为对角线的偏移\n", "\n", "## 随机数矩阵\n", "\n", "1. `a = np.random.rand(4)`\n", " 输出:`[ 0.99890402 0.41171695 0.40725671 0.42501804]`范围在`[0,1]`之间\n", "2. `a = np.random.randn(4) `Gaussian函数,\n", "3. 生成100个0-m的随机数: `[t for t in [np.random.randint(x-x, m) for x in range(100)]]` \n", " 也可以\n", " ```\n", " m_arr = np.arange(0,m) # 生成0-m-1\n", " np.random.shuffle(m_arr) # 打乱m_arr顺序\n", " ```\n", " 然后取前100个即可\n", "\n", "## 查看数据类型\n", "\n", "1. `a.dtype`\n", "\n", "## 数组复制\n", "\n", "1. 共享内存\n", " ```\n", " a = np.array([1,2,3,4,5])\n", " b = a\n", " print np.may_share_memory(a,b)\n", " ```\n", " 输出:`True`\n", " 说明使用的同一个存储区域,修改一个数组同时另外的也会修改\n", "\n", "2. 不共享内存\n", " ```\n", " b = a.copy()\n", " ```\n", "## 布尔型\n", "\n", "```\n", "a = np.random.random_integers(0,20,5)\n", "print a\n", "print a%3==0\n", "print a[a % 3 == 0]\n", "```\n", "输出:\n", "```\n", "[14 3 6 15 4]\n", "[False True True True False]\n", "[ 3 6 15]\n", "```\n", "## 中间数、平均值\n", "\n", "1. 中间数np.median(a)\n", "2. 平均值np.mean(a),\n", " 若是矩阵,不指定axis默认求所有元素的均值\n", " axis=0,求列的均值\n", " axis=1,求行的均值\n", " \n", "## 矩阵操作\n", "\n", "1. 乘积`np.dot(a,b)`\n", "```\n", "a = np.array([[1,2,3],[2,3,4]])\n", "b = np.array([[1,2],[2,3],[2,2]])\n", "print np.dot(a,b)\n", "```\n", "\n", "或者使用`np.matrix()`生成矩阵,相乘需要满足矩阵相乘的条件\n", "\n", "2. 内积`np.inner(a,b)`\n", " 行相乘\n", "3. 逆矩阵`np.linalg.inv(a)`\n", "4. 列的最大值`np.max(a[:,0])`–>返回第一列的最大值\n", "5. 每列的和`np.sum(a,0)`\n", "6. 每行的平均数`np.mean(a,1)`\n", "7. 求交集`p.intersect1d(a,b)`,返回一维数组\n", "8. 转置:`np.transpose(a)`\n", "9. 两个矩阵对应对应元素相乘(点乘):`a*b`\n", "\n", "## 文件操作\n", "\n", "1. 保存:tofile()\n", " ```\n", " a = np.arange(10)\n", " a.shape=2,5\n", " a.tofile(\"test.bin\")\n", " ```\n", " 读取:(需要注意指定保存的数据类型)\n", " ```\n", " a = np.fromfile(\"test.bin\",dtype=np.int32)\n", " print a\n", " ```\n", "2. 保存:`np.save(\"test\",a)`–>会保存成test.npy文件\n", " 读取:`a = np.load(\"test\")`\n", " \n", "## 组合两个数组\n", "\n", "1. 垂直组合\n", "```\n", "a = np.array([1,2,3])\n", "b = np.array([[1,2,3],[4,5,6]])\n", "c = np.vstack((b,a))\n", "```\n", "2. 水平组合\n", "```\n", "a = np.array([[1,2],[3,4]])\n", "b = np.array([[1,2,3],[4,5,6]])\n", "c = np.hstack((a,b))\n", "```\n", "## 读声音Wave文件\n", "\n", "1. wave\n", "```\n", "import wave\n", "from matplotlib import pyplot as plt\n", "import numpy as np\n", "# 打开WAV文档\n", "f = wave.open(r\"c:\\WINDOWS\\Media\\ding.wav\", \"rb\")\n", "# 读取格式信息\n", "# (nchannels, sampwidth, framerate, nframes, comptype, compname)\n", "params = f.getparams()\n", "nchannels, sampwidth, framerate, nframes = params[:4]\n", "# 读取波形数据\n", "str_data = f.readframes(nframes)\n", "f.close()\n", "#将波形数据转换为数组\n", "wave_data = np.fromstring(str_data, dtype=np.short)\n", "wave_data.shape = -1, 2\n", "wave_data = wave_data.T\n", "time = np.arange(0, nframes) * (1.0 / framerate)\n", "# 绘制波形\n", "plt.subplot(211) \n", "plt.plot(time, wave_data[0])\n", "plt.subplot(212) \n", "plt.plot(time, wave_data[1], c=\"g\")\n", "plt.xlabel(\"time (seconds)\")\n", "plt.show()\n", "```\n", "## where\n", "\n", "找到y数组中=1的位置:np.where(y==1)\n", "\n", "## np.ravel(y)\n", "\n", "将二维的转化为一维的,eg:(5000,1)-->(5000,)\n", "\n", "## ndarray.flat函数\n", "\n", "将数据展开对应的数组,可以进行访问\n", "\n", "应用:0/1映射\n", "```\n", "def dense_to_one_hot(label_dense,num_classes):\n", " num_labels = label_dense.shape[0]\n", " index_offset = np.arange(num_labels)*num_classes\n", " labels_one_hot = numpy.zeros((num_labels, num_classes))\n", " labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1\n", " return labels_one_hot\n", "```\n", "## 数组访问\n", "```\n", "X = np.array([[1,2],[3,4]])\n", "```\n", "`X[0:1]`和`X[0:1,:]`等价,都是系那是第一行数据\n", "\n", "## `np.c_()`\n", "\n", "按照第二维度,即列拼接数据\n", "\n", "`np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]`\n", "\n", "输出:`array([[1, 2, 3, 0, 0, 4, 5, 6]])`\n", "\n", "两个列表list拼接,长度要一致\n", "\n", "`np.c_[[1,2,3],[2,3,4]]`\n", "\n", "`np.c_[range(1,5),range(2,6)]`" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" }, "toc": { "colors": { "hover_highlight": "#DAA520", "navigate_num": "#000000", "navigate_text": "#333333", "running_highlight": "#FF0000", "selected_highlight": "#FFD700", "sidebar_border": "#EEEEEE", "wrapper_background": "#FFFFFF" }, "moveMenuLeft": true, "nav_menu": { "height": "390px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": false, "toc_section_display": "block", "toc_window_display": false, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 2 }