xirururu xirururu - 1 year ago 115
Python Question

How to return the indices of maximum value from an array with python?

I have an array and I want to find the indices of the maximum values.

For example:

myarray = np.array([1,8,8,3,2])

I want to get the result:
, how can I do that?

(Actually I tried
, but it only return the first occurrence

Answer Source


>>> myarray = np.array([1,8,8,3,2])

You can do:

>>> np.where(myarray==myarray[np.argmax(myarray)])
(array([1, 2]),)


>>> np.where(myarray==max(myarray))
(array([1, 2]),)


>>> np.nonzero(myarray==max(myarray))
(array([1, 2]),)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download