Numpy
Numpy是数据处理的利器
import numpy as np array = np.zeros((3,4)) #生成零矩阵 array1 = np.ones((3,4))#生成单位矩阵 array2 = np.empty((3,4))#生成近似零的矩阵 print(array) print(array1) print(array2)
array = np.arange(10,20,2) #从10到20间隔2 array = np.arange(12).reshape((3,4)) #分成12个,生产3x4的矩阵 array = np.linspace(1,10,6) #生成线段数列 6个点5段 array = np.linspace(1,10,6).reshape((2,3)) array = np.array([1,2,3],dtype=np.int64) print(array.dtype) #显示位数 print(array.ndim) #维度 print(array.shape) #形状 print(array.size) #大小
a = np.array([[1,1], [0,1]]) b = np.arange(4).reshape((2,2)) print(a) print(b) c = a*b print(c) c_dot = np.dot(a,b) print(c_dot) c_dot_2 = a.dot(b) print(c_dot_2)
A = np.random.random((2,4)) print(A) print(np.sum(A)) print(np.min(A)) print(np.max(A)) print(np.mean(A)) print(np.average(A)) print(np.median(A) )#中位数 print(np.cumsum(A)) #累加 print(np.diff(A)) #累差 print(np.nonzero(A)) #非零的数 print(np.sort(A)) #逐行排序 print(np.transpose(A)) # 转置 print(np.clip(A,5,9)) #所有小于5的数都等于5,大于9的数等于9 print(np.sum(A,axis=0)) #在列中 print(np.sum(A,axis=1) ) #在行中 print(np.argmin(A) )# 最小值的索引
B = np.arange(3,15).reshape(3,4) print(B) for row in B: print(row)
C= np.arange(3,15).reshape(3,4) print(C) for column in C.T: print(column)
D = np.arange(3,15).reshape((3,4)) print(D) for item in D.flat: print(item)
E = np.array([1,1,1]) F = np.array([2,2,2]) G = np.vstack((E,F)) H = np.hstack((E,F)) print(G) print(H) print(G.shape,H.shape)
E = np.array([1,1,1]) F = np.array([2,2,2]) G = np.vstack((E,F))[:,np.newaxis] H = np.hstack((E,F))[:,np.newaxis] print(G) print(H) print(G.shape,H.shape)
J = np.arange(12).reshape((3,4)) print(J) print(np.split(J,3,axis=0))
J = np.arange(12).reshape((3,4)) print(J) print(np.array_split(J,3,axis=0))