NumPy
argmax()
返回矩阵/数组的最大值的索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import numpy as np
a = np.arange(12).reshape(3,4) a
np.argmax(a)
np.argmax(a, axis=0)
np.argmax(a, axis=1)
|
argmin()
返回矩阵/数组的最小值的索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import numpy as np
a = np.arange(12).reshape(3,4) a
np.argmin(a, axis=0)
np.argmin(a, axis=1)
a[2, 2] = 2 a
np.argmin(a, axis=0)
|
mean()
求均值
1 2 3 4 5
| import numpy as np
lst = [2,3,4,5,6] np.mean(lst)
|
求中位数
1 2 3 4 5
| import numpy as np
lst = [2,1,4,5,2,5,7,8] np.median(lst)
|
newaxis
用于扩展数组的维度
https://stackoverflow.com/questions/29241056/how-does-numpy-newaxis-work-and-when-to-use-it
中的图片简单易懂
ptp()
最大值与最小值的差
reshape()
改变矩阵/数组的维数
1 2 3 4 5 6 7
| import numpy as np
a = np.arange(12).reshape(3, 4) a
|
reshape(m,-1)
将数组形状变为m行(列数相应确定);元素个数必须是m的整数倍
1 2 3 4 5 6 7 8 9
| a = np.arange(12).reshape(1, -1) a
a = np.arange(12).reshape(3, -1) a
|
reshape(-1,n)
将数组形状变为n列(行数相应确定);元素个数必须是n的整数倍
1 2 3 4 5
| a = np.arange(12).reshape(-1, 4) a
|
tile()
tile(A, B)
:重复A B次
1 2 3 4 5 6 7 8 9 10 11 12 13
| import numpy as np
np.tile([1, 2],5)
np.tile([3, 4],(2,1))
np.tile([1, 2], (3, 4))
|
参考资料