Python numpy argmax是如何工作的?
所以我知道numpy argmax沿着一个轴检索最大值。因此,Python numpy argmax是如何工作的?,python,numpy,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],轴
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应该返回沿轴的最大值。下面是我的代码和输出
代码:打印(np.argmax(x,轴=1))
。输出:[0]
代码:打印(np.argmax(x,轴=0))
。输出:[2]
0
和2
来自哪里?我特意使用了一组更复杂的整数值(9..20),以便区分0
和2
以及数组中的整数值 argmax(x,轴=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
[[12 11 10 9]更正:
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.
我可以知道为什么它被下载了吗?你说的最大值索引是什么意思?我在谷歌上搜索了它,但我不明白。我甚至试着对它进行编码,以了解它是如何工作的。你认为我会经历发布一篇长文章并在不谷歌的情况下尝试它的麻烦吗?np.max
返回沿相应轴的最大值;np.argmax
返回这些值出现的位置,索引
。感谢您的回复。很高兴看到人们以实物形式回复,这与OP最初被其他用户否决和指责的方式不同。