Python中numpy模块常见用法demo实例小结
本文实例总结了Python中numpy模块常见用法。分享给大家供大家参考,具体如下:
importnumpyasnp
arr=np.array([[1,2,3],[2,3,4]]) print(arr) print(type(arr)) print('numberofdim:',arr.ndim) print('shape:',arr.shape) print('size:',arr.size)
[[123]
[234]]
numberofdim:2
shape:(2,3)
size:6
a32=np.array([1,23,456],dtype=np.int) print(a32.dtype) a64=np.array([1,23,456],dtype=np.int64) print(a64.dtype) f64=np.array([1,23,456],dtype=np.float) print(f64.dtype)
int32
int64
float64
z=np.zeros((3,4)) print(z) print(z.dtype) print() one=np.ones((3,4),dtype=int) print(one) print(one.dtype) print() emt=np.empty((3,4),dtype=int) print(emt) print(emt.dtype) print() ran=np.arange(12).reshape((3,4)) print(ran) print(ran.dtype) print() li=np.linspace(1,10,6).reshape(2,3) print(li) print(li.dtype)
[[0.0.0.0.]
[0.0.0.0.]
[0.0.0.0.]]
float64
[[1111]
[1111]
[1111]]
int32
[[ 0 1072693248 1717986918 1074161254]
[1717986918 1074947686-1717986918 1075419545]
[1717986918 1075865190 0 1076101120]]
int32
[[0 1 2 3]
[4 5 6 7]
[8 91011]]
int32
[[1. 2.8 4.6]
[6.4 8.210.]]
float64
a=np.array([10,20,30,40]) b=np.arange(4) print(a) print(b) print() print(a+b) print(a-b) print(a*b) print() print(a**b) print() print(10*np.sin(a)) print() print(b<3) print()
[10203040]
[0123]
[10213243]
[10192837]
[ 0 20 60120]
[ 1 20 90064000]
[-5.44021111 9.12945251-9.88031624 7.4511316]
[True True TrueFalse]
a=np.array([[1,2],[3,4]]) b=np.arange(4).reshape(2,2) print(a) print(b) print() print(a*b) print(np.dot(a,b))#矩阵乘法,或下面: print(a.dot(b)) print()
[[12]
[34]]
[[01]
[23]]
[[0 2]
[612]]
[[4 7]
[815]]
[[4 7]
[815]]
a=np.random.random((2,4)) print(a) print(np.sum(a)) print(np.min(a)) print(np.max(a)) print() print(np.sum(a,axis=1))#返回每一行的和。axis=1代表行 print(np.min(a,axis=0))#返回每一列的最小值。axis=0代表列 print(np.mean(a,axis=1))#返回每一行的平均值
[[0.044567040.994816790.965995610.48590905]
[0.565128520.628877140.788291150.32759434]]
4.8011796551183945
0.04456704487406293
0.9948167913629338
[2.4912885 2.30989116]
[0.044567040.628877140.788291150.32759434]
[0.622822120.57747279]
A=np.arange(2,14).reshape(3,4) print(A) print(np.argmin(A))#最小索引 print(np.argmax(A))#最大索引 print() print(A.mean()) print(np.median(A))#中位数 print(A.cumsum())#累加值 print(np.diff(A))#相邻差值 print()
[[2 3 4 5]
[6 7 8 9]
[10111213]]
0
11
7.5
7.5
[2 5 9142027354454657790]
[[111]
[111]
[111]]
(array([0,0,0,0,1,1,1,1,2,2,2,2],dtype=int32),array([0,1,2,3,0,1,2,3,0,1,2,3],dtype=int32))
A=np.array([[1,0],[0,3]]) print(A) print(A.nonzero())#分别输出非零元素的行和列值 print(np.sort(A))#逐行排序后的矩阵 print(np.sort(A,axis=0))#逐列排序的矩阵 print(np.sort(A).nonzero()) print() B=np.arange(14,2,-1).reshape(3,4) print(B) print(B.transpose())#转置 print((B.T).dot(B))#转置 print() print(np.clip(B,5,9))#B中将范围限定,大于9的数都为9,小于5的都为5,之间的数不变 print()
[[10]
[03]]
(array([0,1],dtype=int32),array([0,1],dtype=int32))
[[01]
[03]]
[[00]
[13]]
(array([0,1],dtype=int32),array([1,1],dtype=int32))
[[14131211]
[10 9 8 7]
[6 5 4 3]]
[[1410 6]
[13 9 5]
[12 8 4]
[11 7 3]]
[[332302272242]
[302275248221]
[272248224200]
[242221200179]]
[[9999]
[9987]
[6555]]
A=np.arange(3,7) print(A) print(A[2]) print() B=np.arange(3,15).reshape(3,4) print(B) print(B[2]) print(B[2][1]) print(B[2,1]) print() print(B[2,2:]) print(B[1:,2:]) print() forrowinB: print(row) print() forcolinB.T: print(col) print() print(B.flatten()) forelminB.flat: print(elm)
[3456]
5
[[3 4 5 6]
[7 8 910]
[11121314]]
[11121314]
12
12
[1314]
[[910]
[1314]]
[3456]
[7 8 910]
[11121314]
[3 711]
[4 812]
[5 913]
[61014]
[3 4 5 6 7 8 91011121314]
3
4
5
6
7
8
9
10
11
12
13
14
#矩阵合并 A=np.array([1,1,1]) B=np.array([2,2,2]) C=np.vstack((A,B,A,B)) print(C) print(A.shape,(A.T).shape) print(C.shape) print() D=np.hstack((A,B)) print(D) print() print(A[np.newaxis,:]) print(A[:,np.newaxis]) print(np.hstack((A[:,np.newaxis],B[:,np.newaxis]))) print() print(np.stack((A,B),axis=0)) print(np.stack((A,B),axis=1)) #print(np.concatenate((A,B,B,A),axis=0)) #print(np.concatenate((A,B,B,A),axis=1))
[[111]
[222]
[111]
[222]]
(3,)(3,)
(4,3)
[111222]
[[111]]
[[1]
[1]
[1]]
[[12]
[12]
[12]]
[[111]
[222]]
[[12]
[12]
[12]]
A=np.arange(12).reshape(3,4) print(A) print(np.split(A,2,axis=1)) print(np.split(A,3,axis=0)) print() print(np.array_split(A,3,axis=1))#不等分割 print() print(np.hsplit(A,2)) print(np.vsplit(A,1))
[[0 1 2 3]
[4 5 6 7]
[8 91011]]
[array([[0,1],
[4,5],
[8,9]]),array([[2, 3],
[6, 7],
[10,11]])]
[array([[0,1,2,3]]),array([[4,5,6,7]]),array([[8, 9,10,11]])]
[array([[0,1],
[4,5],
[8,9]]),array([[2],
[6],
[10]]),array([[3],
[7],
[11]])]
[array([[0,1],
[4,5],
[8,9]]),array([[2, 3],
[6, 7],
[10,11]])]
[array([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9,10,11]])]
A=np.arange(4) B=A C=B D=A.copy() print(A,B,C,D) A[0]=5 print(A,B,C,D) print(id(A),id(B),id(C),id(D))#id返回指针的值(内存地址) print()
[0123][0123][0123][0123]
[5123][5123][5123][0123]
172730832172730832172730832172730792
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。