第17章 科学计算与可视化

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第17章 科学计算与可视化
用于Python科学计算与可视化的扩展模块主要有: NumPy、SciPy、SymPy、matplotlib、Traits、TraitsUI、 Chaco、TVTK、Mayavi、VPython、OpenCV。 numpy:科学计算包,支持N维数组运算、处理大型矩阵、 成熟的广播函数库、矢量运算、线性代数、傅里叶变换、 随机数生成,并可与C++/Fortran语言无缝结合。 scipy:scipy依赖于numpy,提供了更多的数学工具,包 括矩阵运算、线性方程组求解、积分、优化等等。 大量科学计算库安装包下载: http://www.lfd.uci.edu/~gohlke/pythonlibs/ enthought科学计算解决方案: https://www.enthought.com/
17.1 numpy简单应用
转置 >>> b = np.array(([1,2,3],[4,5,6],[7,8,9])) >>> print b [[1 2 3] [4 5 6] [7 8 9]] >>> print b.T [[1 4 7] [2 5 8] [3 6 9]]
点积 >>> c = np.dot(a,b) >>> print c [30 36 42]
17.1 numpy简单应用
计算均值 >>> np.average(x, axis =0) array([ 2.5, 3.5, 4.5, 5.5, 6.5]) >>> np. average(x,axis=1) array([ 2., 7.])
17.1 numpy简单应用
计算标准差、方差 >>> x = np.random.randint(0,10,size=(3,3)) >>> x array([[4, 2, 8], [0, 8, 9], [0, 2, 7]]) >>> np.std(x) 3.4029761846919007 >>> np.std(x,axis=1) array([ 2.49443826, 4.02768199, 2.94392029]) >>> np.var(x) 11.580246913580245
17.1 numpy简单应用
改变数组大小
>>> a = np.arange(1,11,1) >>> a array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) >>> a.shape = 2,5 >>> a array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]) >>> a.shape = 5,-1#-1表示自动计算 >>> a array([[ 1, 2], [ 3, 4], [ 5, 6], [ 7, 8], [ 9, 10]]) >>> b = a.reshape(2,5) >>> b array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])
17.1 numpy简单应用
矩阵元素求和 >>> x = np.arange(0,10).reshape(2,5) >>> x array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) >>> np.sum(x) 45 >>> np.sum(x, axis=0) array([ 5, 7, 9, 11, 13]) >>> np.sum(x, axis=1) array([10, 35])
17.1 numpy简单应用
数组元素访问
>>> import numpy as np >>> import scipy.signal as signal >>> x = np.arange(0,100,10,dtype=np.floating) >>> x array([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90.]) >>> index = np.random.randint(0,len(x),5) >>> index array([9, 5, 9, 5, 6]) >>> noise = np.random.standard_normal(5)*0.3 >>> noise array([ 0.07000973, 0.17404817, 0.16620985, 0.15281169, -0.08693726]) >>> x[index] array([ 90., 50., 90., 50., 60.]) >>> x[index] += noise >>> x[index] array([ 90.16620985, 50.15281169, 90.16620985, 50.15281169, 59.91306274])
17.1 numpy简单应用
求最大值 >>> x array([[4, 2, 8], [0, 8, 9], [0, 2, 7]]) >>> np.max(x) 9 >>> np.max(x,axis=1) array([8, 9, 7])
Βιβλιοθήκη Baidu
17.1 numpy简单应用
排序 >>> x array([[4, 2, 8], [0, 8, 9], [0, 2, 7]]) >>> np.sort(x) array([[2, 4, 8], [0, 8, 9], [0, 2, 7]]) >>> np.sort(x,axis=0) array([[0, 2, 7], [0, 2, 8], [4, 8, 9]])
17.1 numpy简单应用
切片操作 >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> a[::-1] array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) >>> a[::2] array([0, 2, 4, 6, 8]) >>> a[:5] array([0, 1, 2, 3, 4])
17.1 numpy简单应用
布尔运算
>>> x = np.random.rand(10) >>> x array([ 0.93874098, 0.97312716, 0.45264749, 0.74117525, 0.89758246, 0.29755703, 0.2182093 , 0.5673035 , 0.90745768, 0.71920431]) >>> x>0.5 array([ True, True, False, True, True, False, False, True, True, True], dtype=bool) >>> x[x>0.5] array([ 0.93874098, 0.97312716, 0.74117525, 0.89758246, 0.5673035 , 0.90745768, 0.71920431]) >>> np.array([1,2,3]) < np.array([3,2,1]) array([ True, False, False], dtype=bool) >>> np.array([1,2,3]) ==np.array([3,2,1]) array([False, True, False], dtype=bool)
17.1 numpy简单应用
切片操作
>>> c array([[ 0, 1, 2, 3, 4, 5], [10, 11, 12, 13, 14, 15], [20, 21, 22, 23, 24, 25], [30, 31, 32, 33, 34, 35], [40, 41, 42, 43, 44, 45], [50, 51, 52, 53, 54, 55]]) >>> c[0,3:5] array([3, 4]) >>> c[0] array([0, 1, 2, 3, 4, 5]) >>> c[2:5,2:5] array([[22, 23, 24], [32, 33, 34], [42, 43, 44]])
17.1 numpy简单应用
生成特殊数组
>>> print np.zeros((3,3)) [[ 0. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]] >>> print np.ones((3,3)) [[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.]] >>> print np.identity(3) [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]] >>> np.empty((3,3))#只申请空间,不初始化,速度很快 array([[ 4.24510694e+175, 5.03061214e+223, 4.72100120e+164], [ 2.63551414e-144, -1.00000000e+000, 0.00000000e+000], [ 0.00000000e+000, 0.00000000e+000, 1.00000000e+000]])
17.1 numpy简单应用
数组与数值的运算
>>> a = a*2 >>> b = b/2.0
数组与数组的运算
>>>b = b+b >>> a = np.array((1,2,3)) >>> b = np.array(([1,2,3],[4,5,6],[7,8,9])) >>> c = a*b >>> print c [[ 1 4 9] [ 4 10 18] [ 7 16 27]] >>> print c/b [[1 2 3] [1 2 3] [1 2 3]]
17.1 numpy简单应用
三角函数运算 >>> b = np.array(([1,2,3],[4,5,6],[7,8,9])) >>> print np.sin(b) [[ 0.84147098 0.90929743 0.14112001] [-0.7568025 -0.95892427 -0.2794155 ] [ 0.6569866 0.98935825 0.41211849]] 四舍五入 >>> print np.round(np.sin(b)) [[ 1. 1. 0.] [-1. -1. 0.] [ 1. 1. 0.]]
17.1 numpy简单应用
导入模块
>>> import numpy as np
生成数组
>>> a = np.array((1,2,3,4,5)) >>> b = np.array(([1,2,3],[4,5,6],[7,8,9])) >>> x = np.linspace(0,5,10) >>> x array([ 0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222, 2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ]) >>> y = np.logspace(0,100,10) >>> y array([ 1.00000000e+000, 1.29154967e+011, 1.66810054e+022, 2.15443469e+033, 2.78255940e+044, 3.59381366e+055, 4.64158883e+066, 5.99484250e+077, 7.74263683e+088, 1.00000000e+100])
17.1 numpy简单应用
取整运算
>>> x = np.random.rand(10)*50 >>> x array([ 0.69708323, 14.99931488, 15.04431214, 24.60547929, 12.12020273, 42.72638176, 16.01128916, 38.91558471, 39.6877989 , 21.98678429]) >>> np.array([t-int(t) for t in x]) array([ 0.69708323, 0.99931488, 0.04431214, 0.60547929, 0.12020273, 0.72638176, 0.01128916, 0.91558471, 0.6877989 , 0.98678429])
相关文档
最新文档