问题描述
所以我知道 numpy argmax 检索沿轴的最大值.因此,
So I know that the numpy argmax retrieves the maximum value along an axis. Thus,
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 应该返回沿轴的最大数.下面是我的代码和输出.
This makes sense as the sum along axis 1 (row) is [42 58 74]
and axis 0 (column) is [48 45 42 39]
.However, i am confused of how argmax work. From my understanding, argmax is supposed to return the max number along the axis. Below is my code and output.
代码:print(np.argmax(x,axis=1))
.输出:[0 0 0]
代码:print(np.argmax(x,axis=0))
.输出:[2 2 2 2]
0
和 2
是从哪里来的?我特意使用了一组更复杂的整数值 (9..20) 来区分 0
和 2
以及数组中的整数值.
Where does 0
and 2
come from? I've deliberately used a set of more complex integer values (9..20) so as to distinguish between the 0
and 2
and the integer values inside the array.
推荐答案
np.argmax(x,axis=1)
返回每一行的最大值的index.
np.argmax(x,axis=1)
returns the index of maximum of in every row.
axis=1
表示沿轴 1",即行.
axis=1
means "along axis 1", i.e, row.
[[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)
类似,但现在它返回每列中最大值的 index.
It's similar for np.argmax(x,axis=0)
, but now it returns the index of maximum of in every column.
这篇关于numpy argmax 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!