Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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? I would like as output array([1,1,0]) instead.

like image 256
Peter Smit Avatar asked Mar 29 '11 07:03

Peter Smit


People also ask

How do you find the max value in an array in Python?

Python Numpy – Get Maximum Value of Array Given a numpy array, you can find the maximum value of all the elements in the array. To get the maximum value of a Numpy Array, you can use numpy function numpy. max() function.


2 Answers

>>> a.argmax(axis=0)  array([1, 1, 0]) 
like image 136
eumiro Avatar answered Oct 18 '22 22:10

eumiro


>>> 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 
like image 21
blaz Avatar answered Oct 18 '22 21:10

blaz