Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 313
Liw Avatar asked Jan 18 '23 23:01

Liw


1 Answers

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])
like image 70
eumiro Avatar answered Jan 31 '23 07:01

eumiro