NumPy常见运算之min、max、mean、sum、exp、sqrt、sort、乘法、点积、拼接、切分

基本运算

  • ndarray.min() / np.min(ndarray)
  • ndarray.max() / np.max(ndarray)
  • ndarray.mean() / np.mean(ndarray)
  • ndarray.sum() / np.sum(ndarray)
  • ndarray.argmax() 最大值的索引
  • np.exp()
  • np.sqrt() 开根号
  • np.sort() 排序
  • np.argsort() 排序的索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def numpy():
n = np.array([[5, 10, 15],
[20, 25, 30],
[35, 40, 45]])
print(n.min()) # 5
print(n.max()) # 45
print(n.mean()) # 25.0
print(n.sum()) # 225
# 指定所操作的维度,axis=0 按列,axis=1 按行
print(n.sum(axis=0)) # [60 75 90]
print(n.sum(axis=1)) # [ 30 75 120]

print('- ' * 20)
n1 = np.arange(3)
print(n1) # [0 1 2]
print(np.exp(n1)) # [1. 2.71828183 7.3890561 ]
print(np.sqrt(n1)) # [0. 1. 1.41421356]

print('- ' * 20)
n2 = np.random.random((2, 3)) * 10
n3 = np.floor(n2)
print(n2)
'''
[[8.99880764 4.00463427 0.419855 ]
[6.25824191 3.02688928 3.98404113]]
'''
print(n3)
'''
[[8. 4. 0.]
[6. 3. 3.]]
'''

print('- ' * 20)
a = np.array([[4, 3, 5], [1, 6, 1]])
print(a)
a1 = np.sort(a, axis=1)
print(a1)
'''
[[3 4 5]
[1 1 6]]
'''
a.sort(axis=1)
print(a)
'''
[[3 4 5]
[1 1 6]]
'''

print('- ' * 20)
b = np.array([4, 3, 1, 2])
i = np.argsort(b)
print(i) # [2 3 1 0]
print(b[i]) # [1 2 3 4]

矩阵运算

(1)矩阵乘法

  • ndarray1 * ndarray2
  • ndarray1.dot(ndarray2)
  • np.dot(ndarray1, ndarray2)

(2)拼接 ndarray 对象

  • np.hstack() # 横着拼接
  • np.vstack() # 竖着拼接

(3)切分 ndarray 对象

  • np.hsplit() # 横着切
  • np.vsplit() # 竖着切
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def numpyMatrix():
n1 = np.array([[1, 1],
[0, 1]])
n2 = np.array([[2, 0],
[3, 4]])
print(n1 * n2)
'''
[[2 0]
[0 4]]
'''
print(n1.dot(n2))
'''
[[5 4]
[3 4]]
'''
print(np.dot(n1, n2))
'''
[[5 4]
[3 4]]
'''
n1 = np.array([[1, 2],
[3, 4]])
n2 = np.array([[5, 6],
[7, 8]])

hstack = np.hstack((n1, n2)) # 横着拼接
print(hstack)
'''
[[1 2 5 6]
[3 4 7 8]]
'''
vstack = np.vstack((n1, n2)) # 竖着拼接
print(vstack)
'''
[[1 2]
[3 4]
[5 6]
[7 8]]
'''

print('- ' * 20)
a = np.floor(10 * np.random.random((2, 12)))
print(a)
'''
[[9. 8. 4. 4. 5. 1. 2. 0. 7. 0. 7. 8.]
[9. 4. 3. 5. 5. 5. 0. 4. 9. 1. 2. 3.]]
'''
print(np.hsplit(a, 3)) # 横着切
'''
[array([[9., 8., 4., 4.],
[9., 4., 3., 5.]]), array([[5., 1., 2., 0.],
[5., 5., 0., 4.]]), array([[7., 0., 7., 8.],
[9., 1., 2., 3.]])]
'''
print(np.hsplit(a, (3, 5))) # 指定切分位置
'''
[array([[9., 8., 4.],
[9., 4., 3.]]), array([[4., 5.],
[5., 5.]]), array([[1., 2., 0., 7., 0., 7., 8.],
[5., 0., 4., 9., 1., 2., 3.]])]
'''
b = a.T
print(b)
'''
[[9. 9.]
[8. 4.]
[4. 3.]
[4. 5.]
[5. 5.]
[1. 5.]
[2. 0.]
[0. 4.]
[7. 9.]
[0. 1.]
[7. 2.]
[8. 3.]]
'''
print(np.vsplit(b, 3)) # 竖着切
'''
[array([[9., 9.],
[8., 4.],
[4., 3.],
[4., 5.]]), array([[5., 5.],
[1., 5.],
[2., 0.],
[0., 4.]]), array([[7., 9.],
[0., 1.],
[7., 2.],
[8., 3.]])]
'''

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :