[Python] Efficiently Finding the Indices of the N Largest Values in a NumPy Array Without...

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working with very large NumPy arrays (millions to billions of elements) and need to find the indices of the N largest values in the array. Using np.argsort() followed by slicing to get the last N elements is too slow and memory-intensive for my use case, as it sorts the entire array.

    Is there a more efficient way to find the indices of the N largest values without sorting the entire array? Ideally, the solution would have a time complexity better than O(n log n) and avoid creating large intermediate arrays.

    For example, given the array:

    import numpy as np
    arr = np.array([1, 5, 2, 8, 3, 9, 4, 7, 6])
    N = 3


    The desired output would be the indices of the 3 largest values (9, 8, 7):

    [5, 3, 7] # Indices of 9, 8, and 7

    Continue reading...

Compartilhe esta Página