[英]Tricky numpy argmax on last dimension of 3-dimensional ndarray
如果具有形狀數組(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。
我猜該解決方案與重塑有關,但是我無法解決這個問題。 謝謝你的幫助!
您可能需要這樣做:
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
我不確定這有什么棘手的問題。 但是,獲取沿最后一個軸的最大元素的索引的一種方法是使用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在其評論中建議的那樣 ,您可以使用:
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.