所以我知道 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))
Run Code Online (Sandbox Code Playgroud)
会输出,
[[12 11 10 9]
[16 15 14 13]
[20 19 18 17]]
[42 58 74]
[48 45 42 39]
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为沿轴 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]
哪里0以及2从何而来?我特意使用了一组更复杂的整数值 (9..20) 来区分数组中的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
Run Code Online (Sandbox Code Playgroud)
因此它的输出是[0 0 0]。
它与 for 类似np.argmax(x,axis=0),但现在它返回每列中最大值的索引。