numpy
属性
import numpy as np#矩阵运算array=np.array([[1,2,3],[2,3,4]])print (array)[[1 2 3][2 3 4]]
维度
print('number of dim',array.ndim)number of dim 2
形状
print('shape',array.shape)shape (2, 3)
大小
print('size',array.size)size 6
创建
创建类型
a=np.array([2,23,4],dtype=np.int)print(a.dtype)
int32位数小,空间小,精度问题
零矩阵 1矩阵
a=np.zeros((3,4))print(a)[[0. 0. 0. 0.][0. 0. 0. 0.][0. 0. 0. 0.]]#ones empty一样用法
arange
a=np.arange(10,20,2)print(a)[10 12 14 16 18]a=np.arange(12).reshape((3,4))print(a)[[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]]
线段linspace
a=np.linspace(1,10,20)print(a)[ 1. 1.47368421 1.94736842 2.42105263 2.89473684 3.368421053.84210526 4.31578947 4.78947368 5.26315789 5.73684211 6.210526326.68421053 7.15789474 7.63157895 8.10526316 8.57894737 9.052631589.52631579 10. ]
基础运算
a=np.array([10,20,30,40])b=np.arange(4)
加减乘除
c=a-bprint(a,b,c)[10 20 30 40] [0 1 2 3] [10 19 28 37]#平方b**2
三角函数
c=10*np.sin(a)print(c)[-5.44021111 9.12945251 -9.88031624 7.4511316 ]
比较运算
print(b<3)[ True True True False]
矩阵运算
a=np.array([[1,1],[0,1]])b=np.arange(4).reshape((2,2))#矩阵乘法np.dot(a,b)Out[32]: array([[2, 4],[2, 3]])a.dot(b)Out[33]: array([[2, 4],[2, 3]])
随机数组
d=np.random.random((2,3))print(d)[[0.46635805 0.01213244 0.47653125][0.01615214 0.34600836 0.72292145]]np.sum(d)Out[37]: 2.0401036780019526np.min(d)Out[38]: 0.01213243701371769np.max(d)Out[39]: 0.7229214475603181np.max(d,axis=1)Out[40]: array([0.47653125, 0.72292145])np.max(d,axis=0)Out[41]: array([0.46635805, 0.34600836, 0.72292145])
求索引
A=np.arange(2,14).reshape(3,4)print(A)[[ 2 3 4 5][ 6 7 8 9][10 11 12 13]]np.argmin(A)Out[44]: 0np.argmax(A)Out[44]: 11
平均值 中位数
np.mean(A)
Out[45]: 7.5
np.average(A)
Out[46]: 7.5np.median(A)Out[47]: 7.5
累加
print(A),[[ 2 3 4 5][ 6 7 8 9][10 11 12 13]]Out[48]: (None,)np.cumsum(A)Out[49]: array([ 2, 5, 9, 14, 20, 27, 35, 44, 54, 65, 77, 90], dtype=int32)
累差
np.diff(A)Out[50]: array([[1, 1, 1],[1, 1, 1],[1, 1, 1]])
输出非0行列值
np.nonzeros(A)
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64),array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
排序
np.sort(A)Out[52]: array([[ 2, 3, 4, 5],[ 6, 7, 8, 9],[10, 11, 12, 13]])
转置
np.transpose(A)Out[53]: array([[ 2, 6, 10],[ 3, 7, 11],[ 4, 8, 12],[ 5, 9, 13]])(A.T).dot(A)Out[54]: array([[140, 158, 176, 194],[158, 179, 200, 221],[176, 200, 224, 248],[194, 221, 248, 275]])
clip对矩阵小于5变5,大于9变9
np.clip(A,5,9)Out[55]: array([[5, 5, 5, 5],[6, 7, 8, 9],[9, 9, 9, 9]])
索引
A=np.arange(3,15)print(A)[ 3 4 5 6 7 8 9 10 11 12 13 14]print(A[3])6
二维
for row in A:print(row)
A=np.arange(3,15).reshape(3,4)print(A[2])[11 12 13 14]print(A[1][1])8print(A[:,1])[ 4 8 12]
打印项目
A=np.arange(3,15).reshape(3,4)print(A.flatten())[ 3 4 5 6 7 8 9 10 11 12 13 14]for item in A.flat:print(item)
合并
A=np.array([1,1,1])B=np.array([2,2,2])#上下合并print(np.vstack((A,B)))[[1 1 1][2 2 2]]#左右合并print(np.hstack((A,B)))[1 1 1 2 2 2]#加维度print(A[np.newaxis,:])[[1 1 1]]print(A[:,np.newaxis])[[1][1][1]]#总和并A=A[:,np.newaxis]B=B[:,np.newaxis]np.concatenate((A,B,B,A),axis=1)Out[17]: array([[1, 2, 2, 1],[1, 2, 2, 1],[1, 2, 2, 1]])np.concatenate((A,B,B,A),axis=0)Out[18]: array([[1],[1],[1],[2],[2],[2],[2],[2],[2],[1],[1],[1]])
分割
A=np.arange(12).reshape(3,4)np.split(A,2,axis=1)Out[20]: [array([[0, 1],[4, 5],[8, 9]]), array([[ 2, 3],[ 6, 7],[10, 11]])]print(A)[[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]]#不对等分割np.array_split(A,3,axis=1)Out[22]: [array([[0, 1],[4, 5],[8, 9]]), array([[ 2],[ 6],[10]]), array([[ 3],[ 7],[11]])]
copy和deep copy