1

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: [1,2], how can I do that?

(Actually I tried np.argmax(myarray), but it only return the first occurrence [1])

2
  • Use max() to find the maximum value, then numpy.where() to find all the indexes with that value. Commented Sep 10, 2016 at 0:17
  • Bet there is a better way, but np.where(myarray==myarray[np.argmax(myarray)]) works... Commented Sep 10, 2016 at 0:17

1 Answer 1

3

Given:

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

You can do:

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

or,

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

or,

>>> np.nonzero(myarray==max(myarray))
(array([1, 2]),)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.