Getting indices of N minimum values in NumPy
Start your free 7-days trial now!
To get the indices of N miniumum values in NumPy in an optimal way, use the argpartition(~)
method.
In short, the argpartition(~)
method allows you to select an index by which to partition, and ensures that all values at indices smaller than the value at this index appear before it, and all values at indices larger than this index value appear after it.
Example
To get the indices of the 4 smallest values:
n = 4
min_values = x[min_indices]sorted_min_values = x[sorted_min_indices]
indices of min values: [1 5 2 6]min values: [3 2 4 5]sorted indices of min values: [5 1 2 6]sorted min values: [2 3 4 5]
Explanation
The method argpartition(~)
returns the following:
array([1, 5, 2, 6, 7, 4, 3, 0])
Here, n-1=3
means that all values from index 0 and up to and including index 2 of the returned array will be guaranteed to be smaller than the value at index 3. This means that values at index 1, 5 and 2, which are 3, 2 and 4 are smaller than the value at index 6, which is 5.
We then use slicing syntax to extract the first 4 values from this indices:
min_indices
array([1, 5, 2, 6])
The four numbers here represent the indices of the four smallest values in the original array x
. However, the trap here is that they are not indices of the sorted smallest values - this is clear when we extract the actual values:
min_values = x[min_indices]min_values
array([3, 2, 4, 5])
As you can see, the smallest values are not sorted.
In a more practical setting, we often want the indices of smallest values that are sorted in ascending order. We can do this like so:
sorted_min_indices
array([5, 1, 2, 6])
Here, we are first using NumPy's argsort(~)
method to obtain the indices of the minimum values sorted in ascending order:
array([1, 0, 2, 3])
We then rearrange the indices of minimum values using this new order:
array([5, 1, 2, 6])
Two-dimensional arrays
The case covered above is for one-dimensional arrays. To extend this to two-dimensional arrays, we need to leverage fancy indexing:
def extract_with_indices(x, idx):
n = 4 [2,6,9,5,7,4,8], [3,9,7,2,6,4,5]])
min_values = extract_with_indices(x, min_indices)sorted_min_values = extract_with_indices(x, sorted_min_indices)
min_indices [[5 0 3 1] [3 0 5 6]]min_values [[4 2 5 6] [2 3 4 5]]sorted_min_indices [[0 5 3 1] [3 0 5 6]]sorted_min_values [[2 4 5 6] [2 3 4 5]]
To understand what extract_with_indices(~)
does, check our article here.