How do I get indices of N maximum values in a NumPy array?

后端 未结 18 1517
长情又很酷
长情又很酷 2020-11-22 04:25

NumPy proposes a way to get the index of the maximum value of an array via np.argmax.

I would like a similar thing, but returning the indexes of the

18条回答
  •  感动是毒
    2020-11-22 05:02

    Use:

    from operator import itemgetter
    from heapq import nlargest
    result = nlargest(N, enumerate(your_list), itemgetter(1))
    

    Now the result list would contain N tuples (index, value) where value is maximized.

提交回复
热议问题