Jupyter中python3之numpy练习

---恢复内容开始---

Jupyter Notebook
Numpy_pratice
In [2]:
n
= 10 L = [i for i in range(n)] In [3]: L * 2 Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [4]: import numpy as np X = np.arange(1,16).reshape(3,5) X Out[4]: array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]) In [5]: X+1 Out[5]: array([[ 2, 3, 4, 5, 6], [ 7, 8, 9, 10, 11], [12, 13, 14, 15, 16]]) In [6]: X//2 Out[6]: array([[0, 1, 1, 2, 2], [3, 3, 4, 4, 5], [5, 6, 6, 7, 7]]) In [7]: X%2 Out[7]: array([[1, 0, 1, 0, 1], [0, 1, 0, 1, 0], [1, 0, 1, 0, 1]]) In [8]: 1/X Out[8]: array([[1. , 0.5 , 0.33333333, 0.25 , 0.2 ], [0.16666667, 0.14285714, 0.125 , 0.11111111, 0.1 ], [0.09090909, 0.08333333, 0.07692308, 0.07142857, 0.06666667]]) In [9]: np.abs(X) Out[9]: array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]) In [10]: np.sin(X) Out[10]: array([[ 0.84147098, 0.90929743, 0.14112001, -0.7568025 , -0.95892427], [-0.2794155 , 0.6569866 , 0.98935825, 0.41211849, -0.54402111], [-0.99999021, -0.53657292, 0.42016704, 0.99060736, 0.65028784]]) In [11]: np.log2(X) Out[11]: array([[0. , 1. , 1.5849625 , 2. , 2.32192809], [2.5849625 , 2.80735492, 3. , 3.169925 , 3.32192809], [3.45943162, 3.5849625 , 3.70043972, 3.80735492, 3.9068906 ]]) In [12]: np.exp(X) Out[12]: array([[2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 5.45981500e+01, 1.48413159e+02], [4.03428793e+02, 1.09663316e+03, 2.98095799e+03, 8.10308393e+03, 2.20264658e+04], [5.98741417e+04, 1.62754791e+05, 4.42413392e+05, 1.20260428e+06, 3.26901737e+06]]) In [13]: np.exp2(X) # 指定幂,结果同下述power(2,x) Out[13]: array([[2.0000e+00, 4.0000e+00, 8.0000e+00, 1.6000e+01, 3.2000e+01], [6.4000e+01, 1.2800e+02, 2.5600e+02, 5.1200e+02, 1.0240e+03], [2.0480e+03, 4.0960e+03, 8.1920e+03, 1.6384e+04, 3.2768e+04]]) In [14]: np.power(2,X) Out[14]: array([[ 2, 4, 8, 16, 32], [ 64, 128, 256, 512, 1024], [ 2048, 4096, 8192, 16384, 32768]]) In [15]: 2**X Out[15]: array([[ 2, 4, 8, 16, 32], [ 64, 128, 256, 512, 1024], [ 2048, 4096, 8192, 16384, 32768]]) In [16]: np.log2(X) Out[16]: array([[0. , 1. , 1.5849625 , 2. , 2.32192809], [2.5849625 , 2.80735492, 3. , 3.169925 , 3.32192809], [3.45943162, 3.5849625 , 3.70043972, 3.80735492, 3.9068906 ]]) In [17]: np.log10(X) Out[17]: array([[0. , 0.30103 , 0.47712125, 0.60205999, 0.69897 ], [0.77815125, 0.84509804, 0.90308999, 0.95424251, 1. ], [1.04139269, 1.07918125, 1.11394335, 1.14612804, 1.17609126]]) 矩阵运算 In [18]: A = np.arange(4).reshape(2,2) A Out[18]: array([[0, 1], [2, 3]]) In [19]: B = np.full((2,2),10) B Out[19]: array([[10, 10], [10, 10]]) In [20]: A+B Out[20]: array([[10, 11], [12, 13]]) In [21]: A*B Out[21]: array([[ 0, 10], [20, 30]]) In [22]: A.dot(B) Out[22]: array([[10, 10], [50, 50]]) In [23]: A.T Out[23]: array([[0, 2], [1, 3]]) 向量和矩阵运算 In [24]: V = np.array([1,2]) V Out[24]: array([1, 2]) In [25]: V+A Out[25]: array([[1, 3], [3, 5]]) In [26]: np.vstack([V]*A.shape[0]) Out[26]: array([[1, 2], [1, 2]]) In [27]: np.vstack([V]*A.shape[0]) + A Out[27]: array([[1, 3], [3, 5]]) In [28]: np.tile(V,(2,1)) # 堆叠,行叠成2次,列叠成1次 Out[28]: array([[1, 2], [1, 2]]) In [29]: np.tile(V,A.shape) Out[29]: array([[1, 2, 1, 2], [1, 2, 1, 2]]) In [30]: np.tile(V,(2,1)) + A Out[30]: array([[1, 3], [3, 5]]) In [31]: V.dot(A) Out[31]: array([4, 7]) In [32]: A.dot(V) Out[32]: array([2, 8]) 矩阵的逆 In [33]: A Out[33]: array([[0, 1], [2, 3]]) In [34]: np.linalg.inv(A) Out[34]: array([[-1.5, 0.5], [ 1. , 0. ]]) In [35]: invA = np.linalg.inv(A) In [36]: A.dot(invA) Out[36]: array([[1., 0.], [0., 1.]]) In [37]: invA.dot(A) Out[37]: array([[1., 0.], [0., 1.]]) In [38]: X Out[38]: array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]) In [39]: invX = np.linalg.inv(X) invX --------------------------------------------------------------------------- LinAlgError Traceback (most recent call last) <ipython-input-39-870393757875> in <module> ----> 1 invX = np.linalg.inv(X) 2 invX ~/anaconda3/lib/python3.7/site-packages/numpy/linalg/linalg.py in inv(a) 525 a, wrap = _makearray(a) 526 _assertRankAtLeast2(a) --> 527 _assertNdSquareness(a) 528 t, result_t = _commonType(a) 529 ~/anaconda3/lib/python3.7/site-packages/numpy/linalg/linalg.py in _assertNdSquareness(*arrays) 213 m, n = a.shape[-2:] 214 if m != n: --> 215 raise LinAlgError('Last 2 dimensions of the array must be square') 216 217 def _assertFinite(*arrays): LinAlgError: Last 2 dimensions of the array must be square 矩阵的伪逆 In [40]: invpX = np.linalg.pinv(X) invpX Out[40]: array([[-2.46666667e-01, -6.66666667e-02, 1.13333333e-01], [-1.33333333e-01, -3.33333333e-02, 6.66666667e-02], [-2.00000000e-02, -2.51534904e-17, 2.00000000e-02], [ 9.33333333e-02, 3.33333333e-02, -2.66666667e-02], [ 2.06666667e-01, 6.66666667e-02, -7.33333333e-02]]) In [41]: X.dot(invpX) Out[41]: array([[ 0.83333333, 0.33333333, -0.16666667], [ 0.33333333, 0.33333333, 0.33333333], [-0.16666667, 0.33333333, 0.83333333]]) In [42]: invpX.dot(X) Out[42]: array([[ 6.00000000e-01, 4.00000000e-01, 2.00000000e-01, -1.33226763e-15, -2.00000000e-01], [ 4.00000000e-01, 3.00000000e-01, 2.00000000e-01, 1.00000000e-01, -7.63278329e-16], [ 2.00000000e-01, 2.00000000e-01, 2.00000000e-01, 2.00000000e-01, 2.00000000e-01], [ 1.73472348e-16, 1.00000000e-01, 2.00000000e-01, 3.00000000e-01, 4.00000000e-01], [-2.00000000e-01, 4.99600361e-16, 2.00000000e-01, 4.00000000e-01, 6.00000000e-01]]) 矩阵的伪逆又被称为“广义逆矩阵”,有兴趣的同学可以翻看线性教材课本查看更多额广义逆矩阵相关的性质。中文wiki链接: https://zh.wikipedia.org/wiki/%E5%B9%BF%E4%B9%89%E9%80%86%E9%98%B5 Numpy 中的比较和Fancy Indexing In [43]: X Out[43]: array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]) In [44]: x = np.arange(16) x Out[44]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) In [45]: ind = [3, 5, 7] x[ind] Out[45]: array([3, 5, 7]) In [46]: ind = np.array([[0, 2], [1, 3],[5,7]]) x[ind] Out[46]: array([[0, 2], [1, 3], [5, 7]]) In [47]: row = np.array([0, 1, 2]) col = np.array([1, 2, 3]) X[row, col] Out[47]: array([ 2, 8, 14]) In [48]: X[[0,1,2],[1,2,3]] Out[48]: array([ 2, 8, 14]) In [49]: X[[1,2],[2,3]] # X里面只能放两个坐标点,放3个报错,需要用到上面的方法,把行列都分开存放 Out[49]: array([ 8, 14]) In [50]: X[row] Out[50]: array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]) In [51]: X[-1,row] # -1表示取最后一行 Out[51]: array([11, 12, 13]) In [53]: X[:,row] # :表示取所有行 Out[53]: array([[ 1, 2, 3], [ 6, 7, 8], [11, 12, 13]]) In [54]: chs = [True, False, True, True, False] In [55]: X[:,chs] # 取1,3,4列 Out[55]: array([[ 1, 3, 4], [ 6, 8, 9], [11, 13, 14]]) numpy.array 的比较 In [56]: x Out[56]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) In [57]: x <= 6 Out[57]: array([ True, True, True, True, True, True, True, False, False, False, False, False, False, False, False, False]) In [58]: x != 3 Out[58]: array([ True, True, True, False, True, True, True, True, True, True, True, True, True, True, True, True]) In [59]: X < 6 Out[59]: array([[ True, True, True, True, True], [False, False, False, False, False], [False, False, False, False, False]]) 使用 numpy.array 的比较结果 In [60]: np.count_nonzero( x <= 3) # 统计小于等于3的数量--有4个满足,所以是4个true,true用表示1,所以结果为4 Out[60]: 4 In [65]: np.sum(x <= 3) # 统计小于等于3的数量 Out[65]: 4 In [66]: X Out[66]: array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]) In [67]: np.sum(X % 2 == 0, axis=0) # 沿着行的方向  即每列偶数的数量 Out[67]: array([1, 2, 1, 2, 1]) In [71]: np.sum(X % 2 == 0, axis=1) # 沿着列的方向  即每行偶数的数量 Out[71]: array([2, 3, 2]) In [91]: np.any(x == 0) # any 查找所有值,只要有一个满足条件 为true,则返回True Out[91]: True In [92]: np.any(x < 0) Out[92]: False In [93]: np.all(x<0) # all 查找所有值,只要有一个不满足条件 为false,则返回False Out[93]: False In [94]: np.all(x>=0) Out[94]: True In [95]: np.all(X>0) # 整体判断,只返回一个布尔值 Out[95]: True In [96]: np.all(X>3,axis=1) # 沿着列的方向, 即每行的值是否满足条件,有一个不满足,则返回false Out[96]: array([False, True, True]) In [97]: np.all(X>3,axis=0) # 沿着行的方向, 即每列的值是否满足条件,有一个不满足,则返回false Out[97]: array([False, False, False, True, True]) In [98]: np.any(X>14) # 整体判断,只返回一个布尔值 Out[98]: True In [99]: np.any(X>14,axis=0) Out[99]: array([False, False, False, False, True]) In [100]: np.any(X>14,axis=1) Out[100]: array([False, False, True]) In [101]: np.sum((x>3)&(x<10)) # 查询3<x<10的数量 Out[101]: 6 In [102]: np.sum((x%2==0)|(x>10)) # 查询x为偶数或大于10的数量 Out[102]: 11 In [103]: np.sum(~(x==0)) # 查询x中不等于0的值的数量 Out[103]: 15 In [104]: x[x<5] # 返回满足条件的数组 Out[104]: array([0, 1, 2, 3, 4]) In [105]: x[x%2==0] Out[105]: array([ 0, 2, 4, 6, 8, 10, 12, 14]) In [106]: X Out[106]: array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]) In [107]: X[X[:,-1]%3==0,:] # 返回最后一列中能被3整除的行 Out[107]: array([[11, 12, 13, 14, 15]]) In [108]: X[X[:,-1]%3==0] Out[108]: array([[11, 12, 13, 14, 15]]) In [109]: X[:,X[-1,:]%3==0] # 返回最后一行中能被3整除的列 Out[109]: array([[ 2, 5], [ 7, 10], [12, 15]]) In [110]: X[-1,:]%3==0 Out[110]: array([False, True, False, False, True])

---恢复内容结束---

posted on 2019-03-26 10:03  一剑风徽  阅读(401)  评论(0编辑  收藏  举报

导航