如何获取NumPy数组中N个最大值的索引?

本文翻译自:How do I get indices of N maximum values in a NumPy array?

NumPy proposes a way to get the index of the maximum value of an array via np.argmax . NumPy提出了一种通过np.argmax数组最大值的索引的np.argmax

I would like a similar thing, but returning the indexes of the N maximum values. 我想要类似的事情,但是返回N最大值的索引。

For instance, if I have an array, [1, 3, 2, 4, 5] , function(array, n=3) would return the indices [4, 3, 1] which correspond to the elements [5, 4, 3] . 例如,如果我有一个数组[1, 3, 2, 4, 5] ,则function(array, n=3)将返回对应于元素[5, 4, 3]的索引[4, 3, 1] 4,3,1 [4, 3, 1] [5, 4, 3]


#1楼

参考:https://stackoom.com/question/Szlx/如何获取NumPy数组中N个最大值的索引


#2楼

Use: 采用:

>>> import heapq
>>> import numpy
>>> a = numpy.array([1, 3, 2, 4, 5])
>>> heapq.nlargest(3, range(len(a)), a.take)
[4, 3, 1]

For regular Python lists: 对于常规的Python列表:

>>> a = [1, 3, 2, 4, 5]
>>> heapq.nlargest(3, range(len(a)), a.__getitem__)
[4, 3, 1]

If you use Python 2, use xrange instead of range . 如果使用Python 2,请使用xrange而不是range

Source: heapq — Heap queue algorithm 来源: heapq —堆队列算法


#3楼

Newer NumPy versions (1.8 and up) have a function called argpartition for this. 较新的NumPy版本(1.8及更高版本)具有一个称为argpartition的函数。 To get the indices of the four largest elements, do 要获取四个最大元素的索引,请执行

>>> a = np.array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])
>>> a
array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])
>>> ind = np.argpartition(a, -4)[-4:]
>>> ind
array([1, 5, 8, 0])
>>> a[ind]
array([4, 9, 6, 9])

Unlike argsort , this function runs in linear time in the worst case, but the returned indices are not sorted, as can be seen from the result of evaluating a[ind] . argsort不同,此函数在最坏的情况下以线性时间运行,但是返回的索引未排序,从评估a[ind]的结果可以看出。 If you need that too, sort them afterwards: 如果您也需要它,请对它们进行排序:

>>> ind[np.argsort(a[ind])]
array([1, 8, 5, 0])

To get the top- k elements in sorted order in this way takes O( n + k log k ) time. 要以这种方式获得排序前k个元素,需要O( n + k log k )时间。


#4楼

Simpler yet: 更简单了:

idx = (-arr).argsort()[:n]

where n is the number of maximum values. 其中, n是最大值的数量。


#5楼

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. 现在result列表将包含N个元组( indexvalue ),其中value最大化。


#6楼

If you don't care about the order of the K-th largest elements you can use argpartition , which should perform better than a full sort through argsort . 如果您不关心第K个最大元素的顺序 ,则可以使用argpartition ,它的性能要比通过argsort进行完整排序argsort

K = 4 # We want the indices of the four largest values
a = np.array([0, 8, 0, 4, 5, 8, 8, 0, 4, 2])
np.argpartition(a,-K)[-K:]
array([4, 1, 5, 6])

Credits go to this question . 学分到这个问题 。

I ran a few tests and it looks like argpartition outperforms argsort as the size of the array and the value of K increase. 我进行了一些测试,随着数组的大小和K值的增加, argpartition表现优于argsort

你可能感兴趣的:(如何获取NumPy数组中N个最大值的索引?)