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)`
python
  • 55
  • 9

1 Answers1

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)
Sianur
  • 639
  • 6
  • 13
  • After applying condition a==x what is the purpose of [0]? Documentation says if condition is true generate x otherwise y – python Jun 30 '18 at 05:06
  • 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. – Sianur Jun 30 '18 at 05:14
  • sorry i didn't get your point. Can you please explain what [0] stands for? Is it stand for row? – python Jun 30 '18 at 05:16
  • `b = np.where(a == x)` yields `(array([2, 3]),)`. Therefore, `b[0]` is `array([2, 3])` that you need. – Sianur Jun 30 '18 at 05:19