Andreas Mueller Andreas Mueller - 1 year ago 162
Python Question

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

. My problem is, that I would like to find the biggest element in the whole array and get the indices of that.

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
returns the flat index when I want the multi-dimensional index.

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

Answer Source

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()