python - 3 维 ndarray 的最后一个维度上的棘手 numpy argmax

标签 python arrays numpy argmax

如果有一个形状为 (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.maxnp.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/

相关文章:

python - TensorFlow - 从模板中创建张量

python - "Expanding Mode"类似于 Pandas "expanding_mean"

python - 使用 biopython 将 dna 对齐转换为 numpy 数组

python - 如何查找两个DataFrame之间具有相同ID的所有记录?

javascript - 如何使用angularJS过滤选中的复选框

python - 使用 Python 向 mailchimp API 发送 POST 请求

python - 有没有办法从python执行jq

php - OO + PHP + 数组 iSSUE

python - ReGex:从重复序列中获取多个匹配

python - 如何在kivy中制作文件选择器以支持不同语言的文件(英语,希伯来语)