numpy argmax是如何工作的?

时间:2018-05-22 06:17:37

标签: python numpy

所以我知道numpy argmax沿轴检索最大值。因此,

x = np.array([[12,11,10,9],[16,15,14,13],[20,19,18,17]])
print(x)
print(x.sum(axis=1))
print(x.sum(axis=0))

会输出,

[[12 11 10  9]
 [16 15 14 13]
 [20 19 18 17]]


[42 58 74]

[48 45 42 39]

这是有道理的,因为沿轴1(行)的和为[42 58 74],轴0(列)为[48 45 42 39]。 但是,我对argmax的工作原理感到困惑。根据我的理解,argmax应该沿轴返回最大数量。下面是我的代码和输出。

代码:print(np.argmax(x,axis=1))。输出:[0 0 0]

代码:print(np.argmax(x,axis=0))。输出:[2 2 2 2]

02来自哪里?我故意使用一组更复杂的整数值(9..20)来区分02以及数组内的整数值。

2 个答案:

答案 0 :(得分:2)

np.argmax(x,axis=1)返回每行中最大值的索引

axis=1表示"沿轴1",即行。

[[12 11 10  9]    <-- max at index 0
 [16 15 14 13]    <-- max at index 0
 [20 19 18 17]]   <-- max at index 0

因此其输出为[0 0 0]

它与np.argmax(x,axis=0)类似,但现在它返回每列中最大值的索引

答案 1 :(得分:0)

更正: axis=0是指行,而不是列。 axis=1是指列,而不是行。

x = np.array([[12,11,10,9],[16,15,14,13],[20,19,18,17]])
  print(x)

[[12 11 10  9]
[16 15 14 13]
[20 19 18 17]]

np.argmax(x, axis=0)
array([2, 2, 2, 2] # third row, index 2 of each of the 4 columns

np.argmax(x, axis=1)
array([0, 0, 0]  # first column, index 0 of each of the three rows.
相关问题