Numpy是一个非常强大的库,具有大量线性代数以及大规模科学计算的方法。
#-*- coding:utf-8 -*- import numpy as np #Numpy生成一维数组 a=np.array([1,2,3]) print type(a) print a.shape print a[0],a[1],a[2] a[0]=5 print a print '-'*100 # 输出 # <type 'numpy.ndarray'> # (3L,) # 1 2 3 # [5 2 3] #Numpy生成二维数组 b=np.array([[1,2,3],[4,5,6]]) print b print b.shape print b[0,0],b[0,1],b[1,0] print '-'*100 # 输出 # [[1 2 3] # [4 5 6]] # (2L, 3L) # 1 2 4 #numpy创建数组 a=np.zeros((2,2))#创建2x2的全0数组 print a b=np.ones((1,2))#创建1x2的全1数组 print b c=np.full((2,2),7)#创建2x2的全为7的数组 print c d=np.eye(2)#创建单位数组 print d e=np.random.random((2,2))#创建2x2的随机数组 print e print '-'*100 # 输出 # [[ 0. 0.] # [ 0. 0.]] # [[ 1. 1.]] # [[7 7] # [7 7]] # [[ 1. 0.] # [ 0. 1.]] # [[ 0.22054647 0.57186555] # [ 0.79464255 0.90896572]] #numpy的多种访问数组的方法 a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) b = a[:2, 1:3]#0,1行 1,2列 print b print a[0, 1]#第0行 第1列 b[0, 0] = 77 print a[0, 1] print '-'*100 # 输出 # [[2 3] # [6 7]] # 2 # 77 a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) row_r1 = a[1, :]#取第二行,4列 row_r2 = a[1:2, :]#取第二行,1行X4列 print row_r1, row_r1.shape print row_r2, row_r2.shape print '-'*100 # 输出 # [5 6 7 8] (4L,) # [[5 6 7 8]] (1L, 4L) col_r1 = a[:, 1] #取第二列,3列 col_r2 = a[:, 1:2]#取第二列,3行X1列 print col_r1, col_r1.shape print col_r2, col_r2.shape print '-'*100 # 输出 # [ 2 6 10] (3L,) # [[ 2] # [ 6] # [10]] (3L, 1L) a = np.array([[1,2], [3, 4], [5, 6]]) print a[[0, 1, 2], [0, 1, 0]] #输出a[0,0] a[1,1] a[2,0] print np.array([a[0, 0], a[1, 1], a[2, 0]]) print '-'*100 # 输出 # [1 4 5] # [1 4 5] a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]]) print a b = np.array([0, 2, 0, 1]) print a[np.arange(4), b]#输出a[0,0] a[1,2] a[2,0] a[3,1] a[np.arange(4), b] += 10 print a print '-'*100 # 输出 # [[ 1 2 3] # [ 4 5 6] # [ 7 8 9] # [10 11 12]] # [ 1 6 7 11] # [[11 2 3] # [ 4 5 16] # [17 8 9] # [10 21 12]] a = np.array([[1,2], [3, 4], [5, 6]]) bool_idx = (a > 2) #当a大于2时为True,否则为False print bool_idx print a[bool_idx] #true输出,false不输出 print a[a > 2] #符合a>2时输出 print '-'*100 # 输出 # [[False False] # [ True True] # [ True True]] # [3 4 5 6] # [3 4 5 6] x = np.array([1, 2]) print x.dtype x = np.array([1.0, 2.0]) print x.dtype x = np.array([1, 2], dtype=np.int64) print x.dtype print '-'*100 # 输出 # int32 # float64 # int64 x = np.array([[1,2],[3,4]], dtype=np.float64) y = np.array([[5,6],[7,8]], dtype=np.float64) print x + y print np.add(x, y) print x - y print np.subtract(x, y) print x * y print np.multiply(x, y) print x / y print np.divide(x, y) print np.sqrt(x) print '-'*100 # 输出 # [[ 6. 8.] # [ 10. 12.]] # [[ 6. 8.] # [ 10. 12.]] # [[-4. -4.] # [-4. -4.]] # [[-4. -4.] # [-4. -4.]] # [[ 5. 12.] # [ 21. 32.]] # [[ 5. 12.] # [ 21. 32.]] # [[ 0.2 0.33333333] # [ 0.42857143 0.5 ]] # [[ 0.2 0.33333333] # [ 0.42857143 0.5 ]] # [[ 1. 1.41421356] # [ 1.73205081 2. ]] x = np.array([[1,2],[3,4]]) y = np.array([[5,6],[7,8]]) v = np.array([9,10]) w = np.array([11, 12]) print v.dot(w) print np.dot(v, w)#9x11+10x12 print x.dot(v) print np.dot(x, v) print x.dot(y)#矩阵X x 矩阵Y print np.dot(x, y) print '-'*100 # 输出 # 219 # 219 # [29 67] # [29 67] # [[19 22] # [43 50]] # [[19 22] # [43 50]] x = np.array([[1,2],[3,4]]) print np.sum(x) print np.sum(x, axis=0)#行相加 print np.sum(x, axis=1)#列相加 print '-'*100 # 输出 # 10 # [4 6] # [3 7] #矩阵的逆 x = np.array([[1,2], [3,4]]) print x print x.T v = np.array([1,2,3]) print v print v.T print '-'*100 # 输出 # [[1 2] # [3 4]] # [[1 3] # [2 4]] # [1 2 3] # [1 2 3] #广播Broadcasting x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]]) v = np.array([1, 0, 1]) y = np.empty_like(x) for i in range(4): y[i, :] = x[i, :] + v#每行与v相加 print y y = x + v print y vv = np.tile(v, (4, 1)) print vv y = x + vv print y print '-'*100 # 输出 # [[ 2 2 4] # [ 5 5 7] # [ 8 8 10] # [11 11 13]] # [[ 2 2 4] # [ 5 5 7] # [ 8 8 10] # [11 11 13]] # [[1 0 1] # [1 0 1] # [1 0 1] # [1 0 1]] # [[ 2 2 4] # [ 5 5 7] # [ 8 8 10] # [11 11 13]] v = np.array([1,2,3]) w = np.array([4,5]) print np.reshape(v, (3, 1))#将1行x3列的v转换成3行x1列矩阵 print np.reshape(v, (3, 1)) * w x = np.array([[1,2,3], [4,5,6]]) print x + v print (x.T + w).T print x + np.reshape(w, (2, 1)) print x * 2 # 输出 # [[1] # [2] # [3]] # [[ 4 5] # [ 8 10] # [12 15]] # [[2 4 6] # [5 7 9]] # [[ 5 6 7] # [ 9 10 11]] # [[ 5 6 7] # [ 9 10 11]] # [[ 2 4 6] # [ 8 10 12]]