{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Scipy\n", "\n", "## Scipy特征\n", "\n", "1. 内置了图像处理, 优化,统计等等相关问题的子模块\n", "2. scipy 是Python科学计算环境的核心。 它被设计为利用 numpy 数组进行高效的运行。从这个角度来讲,scipy和numpy是密不可分的。\n", "\n", "## 文件操作io\n", "\n", "1. 导包:`from scipy import io as spio`\n", "2. 保存mat格式文件`spio.savemat(\"test.mat\", {'a':a})`\n", "3. 加载mat文件`data = spio.loadmat(\"test.mat\")`\n", " 访问值:data[‘a’]–>相当于map\n", "4. 读取图片文件\n", " 导包:`from scipy import misc`\n", " 读取:`data = misc.imread(\"123.png\")`\n", "\n", "[注1]:与`matplotlib中plt.imread('fname.png')`类似\n", "\n", "[注2]:执行misc.imread时可能提醒不存在这个模块,那就安装pillow的包\n", "\n", "## 线性代数操作linalg\n", "\n", "1. 求行列式det`res = linalg.det(a)`\n", "2. 求逆矩阵inv`res = linalg.inv(a)`\n", " 若是矩阵不可逆,则会抛异常LinAlgError: singular matrix\n", "3. 奇异值分解svd`u,s,v = linalg.svd(a)`\n", "\n", "[注1]:s为a的特征值(一维),降序排列,\n", "\n", "[注2]:a = u*s*v’(需要将s转换一下才能相乘)\n", "\n", "```\n", "t = np.diag(s)\n", "print u.dot(t).dot(v)\n", "```\n", "\n", "## 梯度下降优化算法\n", "\n", "1. fmin_bfgs\n", "```\n", "def f(x):\n", " return x**2-2*x\n", "initial_x = 0\n", "optimize.fmin_bfgs(f,initial_x)\n", "```\n", "[注]:initial_x为初始点(此方法可能会得到局部最小值)\n", "\n", "2. fmin()、fmin_cg等等方法\n", "\n", "## 拟合(最小二乘法)\n", "\n", "1. `curve_fit`\n", "```\n", "#产生数据\n", "def f(x):\n", " return x**2 + 10*np.sin(x)\n", "xdata = np.linspace(-10, 10, num=20)\n", "ydata = f(xdata)+np.random.randn(xdata.size)\n", "plt.scatter(xdata, ydata, linewidths=3.0, \n", " edgecolors=\"red\")\n", "#plt.show()\n", "#拟合\n", "def f2(x,a,b):\n", " return a*x**2 + b*np.sin(x)\n", "guess = [2,2]\n", "params, params_covariance = optimize.curve_fit(f2, xdata, ydata, guess)\n", "#画出拟合的曲线\n", "x1 = np.linspace(-10,10,256)\n", "y1 = f2(x1,params[0],params[1])\n", "plt.plot(x1,y1)\n", "plt.show()\n", "```\n", "\n", "## 统计检验\n", "\n", "1. T-检验stats.ttest_ind\n", " ```\n", " a = np.random.normal(0, 1, size=10)\n", " b = np.random.normal(1, 1, size=10)\n", " print stats.ttest_ind(a, b)\n", " ```\n", " 输出:`(-2.6694785119868358, 0.015631342180817954)`\n", " 后面的是概率p: 两个过程相同的概率。如果其值接近1,那么两个过程几乎可以确定是相同的,如果其值接近0,那么它们很可能拥有不同的均值。\n", "\n", "## 插值\n", "\n", "1. 导入包:from scipy.interpolate import interp1d\n", "```\n", "#产生一些数据\n", "x = np.linspace(0, 1, 10)\n", "y = np.sin(2 * np.pi * x)\n", "computed_time = np.linspace(0, 1, 50)\n", "#线性插值\n", "linear_interp = interp1d(x, y)\n", "linear_results = linear_interp(computed_time)\n", "#三次方插值\n", "cubic_interp = interp1d(x, y, kind='cubic')\n", "cubic_results = cubic_interp(computed_time)\n", "#作图\n", "plt.plot(x, y, 'o', ms=6, label='y')\n", "plt.plot(computed_time, linear_results, label='linear interp')\n", "plt.plot(computed_time, cubic_results, label='cubic interp')\n", "plt.legend()\n", "plt.show()\n", "```\n", "\n", "## 求解非线性方程组\n", "\n", "1. optimize中的fsolve\n", "```\n", "from scipy.optimize import fsolve\n", "def func(x):\n", " x0,x1,x2 = x.tolist()\n", " return [5*x1-25,5*x0*x0-x1*x2,x2*x0-27]\n", "initial_x = [1,1,1]\n", "result = fsolve(func, initial_x)\n", "print result\n", "```" ] } ], "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": "174px", "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 }