简体   繁体   English

3维ndarray最后一维上的棘手numpy argmax

[英]Tricky numpy argmax on last dimension of 3-dimensional ndarray

if have an array of shape (9,1,3). 如果具有形状数组(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]]])

I want to find the argmax index of the third dimension, in this case it would be 185, so index 7. 我想找到第三维的argmax索引,在这种情况下,它将是185,因此索引为7。

I guess the solution is linked to reshaping but I can't wrap my head around it. 我猜该解决方案与重塑有关,但是我无法解决这个问题。 Thanks for any help! 谢谢你的帮助!

You may have to do it like this: 您可能需要这样做:

data = np.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]]])

np.argmax(data[:,0][:,2])
 7

I'm not sure what's tricky about it. 我不确定这有什么棘手的问题。 But, one way to get the index of the greatest element along the last axis would be by using np.max and np.argmax like: 但是,获取沿最后一个轴的最大元素的索引的一种方法是使用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

Alternatively, as @PaulPanzer suggested in his comments , you could use: 或者,如@PaulPanzer在其评论中建议的那样 ,您可以使用:

In [63]: np.unravel_index(np.argmax(arr), arr.shape)
Out[63]: (7, 0, 2)

In [64]: arr[(7, 0, 2)]
Out[64]: 185

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM