【问题标题】:How does numpy argmax work?numpy argmax 是如何工作的?
【发布时间】:2018-05-22 06:17:36
【问题描述】:

所以我知道 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 以及数组内的整数值。

【问题讨论】:

  • 我可以知道为什么它被否决
  • 最大值索引是什么意思
  • 我用谷歌搜索了它,但我不明白。我什至尝试对其进行编码以了解它是如何工作的。你认为我会费力地发布长篇文章并在不使用谷歌搜索的情况下尝试一下吗?
  • np.max 返回沿相应轴的最大值; np.argmax 返回这些值出现的“位置”,index

标签: python numpy


【解决方案1】:

np.argmax(x,axis=1) 返回每​​一行中最大值的index

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) 类似,但现在它返回每列中最大值的索引

【讨论】:

  • 感谢您的回复。很高兴看到人们以实物回复,这与 OP 最初被其他用户否决和谴责的方式不同。
【解决方案2】:

更正: 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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    相关资源
    最近更新 更多