np.argmax()是numpy中获取array的某一个维度中数值最大的那个元素的索引,实例如下:
import numpy as np
x = np.random.normal(1,4,(3,5))
y = np.argmax(x,axis=1)
print(x)
print(x.shape)
print(y)
print(y.shape)
输出结果为:
[[ 0.21732346 -3.48525172 -2.19230097 1.53942774 1.51051946]
[-4.298163 1.24736953 -1.24371032 -1.0929181 -2.04791179]
[ 2.29861605 2.14217121 -2.61879844 5.49840773 2.11360768]]
(3, 5)
[3 1 3]
(3,)
其中x=np.random.normal(1,4,(3,5)),代表生成的x是平均值为1,均方差4,尺寸为[3,5]的array。
y = np.argmax(x,axis=1),中的axis=1指定代表我要查找的最大元素在第1维中的索引值。
另一个实例如下:
import numpy as np
x = np.random.normal(1,4,(3,5))
y = np.argmax(x,axis=0)
print(x)
print(x.shape)
print(y)
print(y.shape)
输出结果为:
[[ 2.9624991 7.85484938 1.45422659 5.76279443 -4.96967595]
[ 5.23517858 4.15894847 8.83419527 4.76619198 9.31344739]
[-1.73967324 3.03142305 -3.28402655 4.01823193 2.01979302]]
(3, 5)
[1 0 1 0 1]
(5,)
axis=0,代表我要查找的最大元素在第0维中的索引值。