【问题标题】:numpy.argmax: how to get the index corresponding to the *last* occurrence, in case of multiple occurrences of the maximum valuesnumpy.argmax:如何获取与 *last* 出现对应的索引,以防多次出现最大值
【发布时间】:2011-10-25 17:19:40
【问题描述】:

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

是否可以通过使用 numpy.argmax 之类的方法找到最后一次出现最大值的索引?

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

【问题讨论】:

    标签: python arrays list numpy indexing


    【解决方案1】:
    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])
    

    【讨论】:

    • 我不确定这是否适用于更新的版本,但对于 v1.4.0,应该有 occurences = np.where(a == a.max())[0] 以便你得到一个数组而不是一个元组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多