numpy.argmax:如果多次出现最大值,如何获取与*last*出现相对应的索引

Liw*_*Liw 4 python arrays indexing numpy list

我有一个数字数组,最大值可能会出现不止一次.

是否可以通过使用像numpy.argmax这样的东西找到最后一次出现的最大值的索引?

或者,更好的是,是否可以获得数组中所有最大值出现的索引列表?

eum*_*iro 8

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])
Run Code Online (Sandbox Code Playgroud)

  • 我不确定这是否适用于更新的版本,但是对于v1.4.0,应该有`occurences = np.where(a == a.max())[0]`这样你得到一个数组而不是出现的元组. (4认同)