Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argmax of numpy array returning non-flat indices

I'm trying to get the indices of the maximum element in a Numpy array. This can be done using numpy.argmax. My problem is, that I would like to find the biggest element in the whole array and get the indices of that.

numpy.argmax can be either applied along one axis, which is not what I want, or on the flattened array, which is kind of what I want.

My problem is that using numpy.argmax with axis=None returns the flat index when I want the multi-dimensional index.

I could use divmod to get a non-flat index but this feels ugly. Is there any better way of doing this?

like image 403
Andreas Mueller Avatar asked Feb 28 '12 13:02

Andreas Mueller


People also ask

What does argmax do in NumPy?

The numpy. argmax() function returns indices of the max element of the array in a particular axis. Return : Array of indices into the array with same shape as array.

What does the argmax function return?

The argmax function returns the argument or arguments (arg) for the target function that returns the maximum (max) value from the target function.

Can argmax return multiple values?

argmax() function returns the indices of the maximum values along an axis. In case of multiple occurrences of the maximum values, the indices corresponding to the first occurrence will be returned.


1 Answers

You could use numpy.unravel_index() on the result of numpy.argmax():

>>> a = numpy.random.random((10, 10)) >>> numpy.unravel_index(a.argmax(), a.shape) (6, 7) >>> a[6, 7] == a.max() True 
like image 99
Sven Marnach Avatar answered Sep 20 '22 19:09

Sven Marnach