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

I have a two-dimensional NumPy array. I know how to get the maximum values ​​along the 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 to get the output of array([1,1,0])

+62
python max numpy
Mar 29 '11 at 7:35
source share
4 answers
 >>> a.argmax(axis=0) array([1, 1, 0]) 
+81
Mar 29 '11 at 7:39
source share
 >>> 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 
+66
Nov 23 '12 at 20:52
source share

argmax() returns only the first occurrence for each row. http://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html

If you need to do this for an array in the form, this works better than unravel :

 import numpy as np a = np.array([[1,2,3], [4,3,1]]) # Can be of any shape indices = np.where(a == a.max()) 

You can also change your conditions:

 indices = np.where(a >= 1.5) 

The above results give the form in the requested form. In addition, you can convert to a list of x, y coordinates by:

 x_y_coords = zip(indices[0], indices[1]) 
+23
May 08 '14 at 10:58
source share
 v = alli.max() index = alli.argmax() x, y = index/8, index%8 
+5
Jul 10 '12 at 3:20
source share



All Articles