6

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

Is it possible to find the index of the last occurrence of the maximum value by using something like numpy.argmax?

Or, even better, is it possible to get a list of indices of all the occurrences of the maximum value in the array?

Liw
  • 347
  • 2
  • 4
  • 12

1 Answers1

10
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
  • 194,053
  • 32
  • 286
  • 259
  • 5
    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