如果有一个形状为 (9,1,3) 的数组。
array([[[ 6, 12, 108]],
[[122, 112, 38]],
[[ 57, 101, 62]],
[[119, 76, 177]],
[[ 46, 62, 2]],
[[127, 61, 155]],
[[ 5, 6, 151]],
[[ 5, 8, 185]],
[[109, 167, 33]]])
我想找到第三维的 argmax 索引,在本例中为 185,因此索引为 7。
我想解决方案与 reshape 有关,但我无法理解它。感谢您的帮助!
最佳答案
我不确定它有什么棘手之处。但是,获取沿最后一个轴的最大元素索引的一种方法是使用 np.max
和 np.argmax
喜欢:
# find `max` element along last axis
# and get the index using `argmax` where `arr` is your array
In [53]: np.argmax(np.max(arr, axis=2))
Out[53]: 7
或者,作为 @PaulPanzer suggested in his comments ,你可以使用:
In [63]: np.unravel_index(np.argmax(arr), arr.shape)
Out[63]: (7, 0, 2)
In [64]: arr[(7, 0, 2)]
Out[64]: 185
关于python - 3 维 ndarray 的最后一个维度上的棘手 numpy argmax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48360189/