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

后端 未结 18 1252
长情又很酷
长情又很酷 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:09

    The simplest I've been able to come up with is:

    In [1]: import numpy as np
    
    In [2]: arr = np.array([1, 3, 2, 4, 5])
    
    In [3]: arr.argsort()[-3:][::-1]
    Out[3]: array([4, 3, 1])
    

    This involves a complete sort of the array. I wonder if numpy provides a built-in way to do a partial sort; so far I haven't been able to find one.

    If this solution turns out to be too slow (especially for small n), it may be worth looking at coding something up in Cython.

    0 讨论(0)
  • 2020-11-22 05:09

    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 = 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.

    0 讨论(0)
  • 2020-11-22 05:10

    If you happen to be working with a multidimensional array then you'll need to flatten and unravel the indices:

    def largest_indices(ary, n):
        """Returns the n largest indices from a numpy array."""
        flat = ary.flatten()
        indices = np.argpartition(flat, -n)[-n:]
        indices = indices[np.argsort(-flat[indices])]
        return np.unravel_index(indices, ary.shape)
    

    For example:

    >>> xs = np.sin(np.arange(9)).reshape((3, 3))
    >>> xs
    array([[ 0.        ,  0.84147098,  0.90929743],
           [ 0.14112001, -0.7568025 , -0.95892427],
           [-0.2794155 ,  0.6569866 ,  0.98935825]])
    >>> largest_indices(xs, 3)
    (array([2, 0, 0]), array([2, 2, 1]))
    >>> xs[largest_indices(xs, 3)]
    array([ 0.98935825,  0.90929743,  0.84147098])
    
    0 讨论(0)
  • 2020-11-22 05:10

    This will be faster than a full sort depending on the size of your original array and the size of your selection:

    >>> A = np.random.randint(0,10,10)
    >>> A
    array([5, 1, 5, 5, 2, 3, 2, 4, 1, 0])
    >>> B = np.zeros(3, int)
    >>> for i in xrange(3):
    ...     idx = np.argmax(A)
    ...     B[i]=idx; A[idx]=0 #something smaller than A.min()
    ...     
    >>> B
    array([0, 2, 3])
    

    It, of course, involves tampering with your original array. Which you could fix (if needed) by making a copy or replacing back the original values. ...whichever is cheaper for your use case.

    0 讨论(0)
  • 2020-11-22 05:11

    Here's a more complicated way that increases n if the nth value has ties:

    >>>> def get_top_n_plus_ties(arr,n):
    >>>>     sorted_args = np.argsort(-arr)
    >>>>     thresh = arr[sorted_args[n]]
    >>>>     n_ = np.sum(arr >= thresh)
    >>>>     return sorted_args[:n_]
    >>>> get_top_n_plus_ties(np.array([2,9,8,3,0,2,8,3,1,9,5]),3)
    array([1, 9, 2, 6])
    
    0 讨论(0)
  • 2020-11-22 05:12

    I think the most time efficiency way is manually iterate through the array and keep a k-size min-heap, as other people have mentioned.

    And I also come up with a brute force approach:

    top_k_index_list = [ ]
    for i in range(k):
        top_k_index_list.append(np.argmax(my_array))
        my_array[top_k_index_list[-1]] = -float('inf')
    

    Set the largest element to a large negative value after you use argmax to get its index. And then the next call of argmax will return the second largest element. And you can log the original value of these elements and recover them if you want.

    0 讨论(0)
提交回复
热议问题