当前位置:首页 >> 脚本专栏

Python中numpy模块常见用法demo实例小结

本文实例总结了Python中numpy模块常见用法。分享给大家供大家参考,具体如下:

import numpy as np

arr = np.array([[1,2,3], [2,3,4]])
print(arr)
print(type(arr))
print('number of dim:', arr.ndim)
print('shape:', arr.shape)
print('size:', arr.size)

[[1 2 3]
 [2 3 4]]
number of dim: 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
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
int32
[[          0  1072693248  1717986918  1074161254]
 [ 1717986918  1074947686 -1717986918  1075419545]
 [ 1717986918  1075865190           0  1076101120]]
int32
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
int32
[[ 1.   2.8  4.6]
 [ 6.4  8.2 10. ]]
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()

[10 20 30 40]
[0 1 2 3]
[10 21 32 43]
[10 19 28 37]
[  0  20  60 120]
[    1    20   900 64000]
[-5.44021111  9.12945251 -9.88031624  7.4511316 ]
[ True  True  True False]

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()

[[1 2]
 [3 4]]
[[0 1]
 [2 3]]
[[ 0  2]
 [ 6 12]]
[[ 4  7]
 [ 8 15]]
[[ 4  7]
 [ 8 15]]

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.04456704 0.99481679 0.96599561 0.48590905]
 [0.56512852 0.62887714 0.78829115 0.32759434]]
4.8011796551183945
0.04456704487406293
0.9948167913629338
[2.4912885  2.30989116]
[0.04456704 0.62887714 0.78829115 0.32759434]
[0.62282212 0.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]
 [10 11 12 13]]
0
11
7.5
7.5
[ 2  5  9 14 20 27 35 44 54 65 77 90]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
(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()

[[1 0]
 [0 3]]
(array([0, 1], dtype=int32), array([0, 1], dtype=int32))
[[0 1]
 [0 3]]
[[0 0]
 [1 3]]
(array([0, 1], dtype=int32), array([1, 1], dtype=int32))
[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]
[[14 10  6]
 [13  9  5]
 [12  8  4]
 [11  7  3]]
[[332 302 272 242]
 [302 275 248 221]
 [272 248 224 200]
 [242 221 200 179]]
[[9 9 9 9]
 [9 9 8 7]
 [6 5 5 5]]

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()
for row in B:
  print(row)
print()
for col in B.T:
  print(col)
print()
print(B.flatten())
for elm in B.flat:
  print(elm)

[3 4 5 6]
5
[[ 3  4  5  6]
 [ 7  8  9 10]
 [11 12 13 14]]
[11 12 13 14]
12
12
[13 14]
[[ 9 10]
 [13 14]]
[3 4 5 6]
[ 7  8  9 10]
[11 12 13 14]
[ 3  7 11]
[ 4  8 12]
[ 5  9 13]
[ 6 10 14]
[ 3  4  5  6  7  8  9 10 11 12 13 14]
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))

[[1 1 1]
 [2 2 2]
 [1 1 1]
 [2 2 2]]
(3,) (3,)
(4, 3)
[1 1 1 2 2 2]
[[1 1 1]]
[[1]
 [1]
 [1]]
[[1 2]
 [1 2]
 [1 2]]
[[1 1 1]
 [2 2 2]]
[[1 2]
 [1 2]
 [1 2]]

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  9 10 11]]
[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()

[0 1 2 3] [0 1 2 3] [0 1 2 3] [0 1 2 3]
[5 1 2 3] [5 1 2 3] [5 1 2 3] [0 1 2 3]
172730832 172730832 172730832 172730792

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。