How to get the index of a maximum element in a numpy array along one axis


Question

I have a 2 dimensional NumPy array. I know how to get the maximum values over axes:

>>> a = array([[1,2,3],[4,3,1]])
>>> amax(a,axis=0)
array([4, 3, 3])

How can I get the indices of the maximum elements? So I would like as output array([1,1,0])

1
105
9/2/2015 7:19:49 PM

Accepted Answer

>>> a.argmax(axis=0)

array([1, 1, 0])
126
3/29/2011 7:39:43 AM

>>> import numpy as np
>>> a = np.array([[1,2,3],[4,3,1]])
>>> i,j = np.unravel_index(a.argmax(), a.shape)
>>> a[i,j]
4

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon