Get the position of the largest value in a multi-dimensional NumPy array

kame picture kame · Aug 27, 2010 · Viewed 73.3k times · Source

How can I get get the position (indices) of the largest value in a multi-dimensional NumPy array?

Answer

Manoj Govindan picture Manoj Govindan · Aug 27, 2010

The argmax() method should help.

Update

(After reading comment) I believe the argmax() method would work for multi dimensional arrays as well. The linked documentation gives an example of this:

>>> a = array([[10,50,30],[60,20,40]])
>>> maxindex = a.argmax()
>>> maxindex
3

Update 2

(Thanks to KennyTM's comment) You can use unravel_index(a.argmax(), a.shape) to get the index as a tuple:

>>> from numpy import unravel_index
>>> unravel_index(a.argmax(), a.shape)
(1, 0)