开发者

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

开发者 https://www.devze.com 2023-03-28 04:25 出处:网络
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 v开发者_开发技巧alue in the 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 v开发者_开发技巧alue 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).


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号