Liw
Liw

Reputation: 377

How to get all indices for the maximum value in a numpy array?

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

Upvotes: 8

Views: 11576

Answers (1)

eumiro
eumiro

Reputation: 213075

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

Upvotes: 11

Related Questions