NumPy | argpartition method
Start your free 7-days trial now!
NumPy's argparition(~)
method is used to partition an array based on ascending order. Explaining how it works in words is rather difficult, so let's go through a quick example.
Examples
Suppose we have the following unsorted array:
The sorted version of x
is:
[5,7,8,9,10]
The original indices of this sorted version is:
[1,2,0,4,3]
Instead of returning this sorted indices, the argpartition()
method asks you to select an index by which to partition, and ensures that all values of indices smaller than the value at this index appear before it, and all values of indices larger than this index value appear after it.
Here's an example:
np.argpartition(x,3)
array([4, 5, 1, 2, 3, 0])
Here, we set kth=1
, which means that we want all values up to and including index 3
to be in its rightful place when sorted. In other words, the 3rd index, or the fourth (3+1) smallest value of x
(7 in this case), will be in its rightful place when sorted, with all values smaller than 7 coming before it, and all those larger than 7 to come after it. Therefore, some of the valid sorted versions are as follows:
[4, 5, 1, 2, 3, 0][4, 1, 2, 5, 3, 0][4, 5, 1, 2, 3, 0]...
The ordering of the values before after the k
th index is undefined. This is why the value 4 can potentially come before the value 1. The argpartition
chooses one out of the above, and returns its indices. As such, there is no guarantee that the returned array of indices represents a perfectly sorted array.
In our specific example, the returned indices were:
y
array([4, 5, 1, 2, 3, 0])
Which represents the following sorted version:
x[y]
array([6, 2, 5, 7, 9, 8])
Here, note the following:
the values before the k-th index (index 3 in this case) are smaller than the value at the k-th index, but they are not necessarily sorted.
the values after the k-th index are larger than the value at the k-th index