numpy.argmax: how to get the index corresponding to the *last* occurrence, in case of multiple occurrences of the maximum values

10,345
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])
Share:
10,345
Liw
Author by

Liw

Updated on June 11, 2022

Comments

  • Liw
    Liw about 2 years

    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?

  • dtlussier
    dtlussier almost 13 years
    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.