8

I have an array of numbers, and the maximum value might occurrence more than once.

How can we get a collection of indices of all the occurrences of the maximum value in the array?

For example, for the following array:

import numpy as np

a = np.array((1,2,3,2,3,2,1,3))

the result should be [2, 4, 7] (or an equivalent array or tuple).

outis
  • 75,655
  • 22
  • 151
  • 221
Liw
  • 377
  • 2
  • 4
  • 12

1 Answers1

11
import numpy as np

a = np.array((1,2,3,2,3,2,1,3))

occurences = np.where(a == a.max())

# occurences == array([2, 4, 7])
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • 6
    I'm not sure if this is true with more recent versions, but with v1.4.0, should have `occurences = np.where(a == a.max())[0]` such that you get an array rather than a tuple in occurences. – dtlussier Aug 15 '11 at 17:36