Getting indices of N maximum values in NumPy
Start your free 7-days trial now!
To get the indices of N maximum 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 largest values:
n = 4
max_values = x[max_indices]sorted_max_values = x[sorted_max_indices]
indices of max values: [5 2 6 7]max values: [6 7 9 8]sorted indices of max values: [6 7 2 5]sorted max values: [9 8 7 6]
Explanation
The argpartition(~)
method here returns the following:
array([1, 3, 0, 4, 5, 2, 6, 7])
Here, the second argument takes on the value of -4
, which means that all values starting from the last 3 values of the returned array will be guaranteed to be larger than the fourth value from the end. This means that values at indices 2, 6 and 7, which are 7, 9 and 8 are larger than the value at index 5, which is 6.
We then extract the last 4 values using the slicing:
array([5, 2, 6, 7])
These values represent the indices of 4 largest values in the original array x
. However, the caveat here is that these are not sorted, which becomes clear when we extract the original values using these indices:
array([6, 7, 9, 8])
As you can see here, the maximum values are not sorted in any way.
In a practical setting, we may want to obtain indices of maximum values are sorted in descending order. We can do this like so:
sorted_max_indices
array([6, 7, 2, 5])
To break this down, we first use np.argsort(~)
to retrieve the indices of the sorted maximum values:
array([0, 1, 3, 2])
The problem with this is that argsort(~)
only returns the indices of values sorted in ascending order. In our case, we want the indices of values sorted in descending order. Therefore, we reverse the array using the syntax [::-1]
:
array([2, 3, 1, 0])
We then rearrange the our indices of maximum values using this new order:
array([6, 7, 2, 5])
Two-dimensional case
The case covered above was for one-dimensional case. For two-dimensional arrays, use the following code snippet:
def extract_with_indices(x, idx):
n = 4 [2,6,9,5,7,4,8], [3,9,7,2,6,4,5]])
max_values = extract_with_indices(x, max_indices)sorted_max_values = extract_with_indices(x, sorted_max_indices)
max_indices [[1 4 2 6] [6 4 1 2]]max_values [[6 7 9 8] [5 6 9 7]]sorted_max_indices [[2 6 4 1] [1 2 4 6]]sorted_max_values [[9 8 7 6] [9 7 6 5]]
Note the following:
For one-dimensional arrays, we used
[::-1]
to reverse the array, but for two-dimensional case, we usenp.fliplr(~)
.Refer to this article to understand the logic behind
extract_with_indicejs(~)
.