2

In the following array there are two maximum values 5 and 5. np.argmax command returns the index of first maximum value. But I want to get the index of all maximum values in array using np.argmax. How I can do that?

`a= np.array([1,2,5,5,0,3])
b= np.argmax(a)
print(b)`

1 Answer 1

2

You can use np.where() as suggested here:

a = np.array([1,2,5,5,0,3])
x = np.max(a)
b = np.where(a == x)[0]
print(b)
Sign up to request clarification or add additional context in comments.

4 Comments

After applying condition a==x what is the purpose of [0]? Documentation says if condition is true generate x otherwise y
This is another use: "If only condition is given, return the tuple condition.nonzero(), the indices where condition is True". You need the first element of the tuple.
sorry i didn't get your point. Can you please explain what [0] stands for? Is it stand for row?
b = np.where(a == x) yields (array([2, 3]),). Therefore, b[0] is array([2, 3]) that you need.

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.