NumPy argpartition() | Explained with examples

Hello geeks, and welcome! In this article, we will cover the NumPy argpartition(). Along with that, for an overall better understanding, we will also look at its syntax and parameters. Then, we will see the application of all the theory part through a couple of examples. But first, let us try to get a brief understanding of the function through its definition.

The function Numpy argpartition() helps us by creating an indirect partition along the given axis. It does this with the help of the kth parameter, which will be discussed later. It retains the shape of the parent array. In the next section, we will cover its syntax.

Syntax

numpy.argpartition(a, kth)

This is the general syntax for our function. In the next section, we will discuss the various parameters associated with it.

Parameters

1. a: array_like

This parameter represents the input parameter to which the function needs to be applied.

2. kth: int or sequence if int

As discussed a bit in the definition, this parameter is responsible for the function’s indirect partition. Here, the kth element will be in its final sorted position, and all smaller elements compared to it move before it and move behind the larger elements.

3. axis: int or none

It is an optional parameter. By default, it has a value equal to -1, and if defined as none, the flattened array is used for operating.

4. Kind

It is another optional parameter and represents the selection algorithm. By default, it is set to be introselect.

5. order: str or list of str

Another optional parameter, if defined, this argument specifies which fields to compare first, second, etc. 

Return

- Index_array: ndarray, int

Upon completing the program, the function returns the index number with the partition along the specified axis.

Examples

We have covered all the necessary theories associated with our function. Also, we have looked at its definition and developed a brief understanding of our function. This section will look at various examples that will help us better understand our function.

1. Basic example for numPy Argpartition()

#input
import numpy as ppool
a=[1,3,5,4,2]
print(ppool.argpartition(a,3))
b=[10,23,50,41,28]
print(ppool.argpartition(b,2))
#output
[0 4 1 3 2]
[0 1 4 3 2]

In the above example, at first, we have imported the NumPy module. Then, we have defined our array. Following this, we used a print statement along with our syntax to achieve the desired output.

For the first example, we defined the kth value as 3, the index number. Here, with the index, the number 3 value 4 is associated, so accordingly, all adjustments are made. So then our input array becomes [1,2,3,4,5], and our rightly justifies it when compared to their index numbers in the parent array.

2. Example with optional parameters

import numpy as ppool
a=[[1,3,5,4,2],
   [3,45,5,6,7]]
print(ppool.argpartition(a,3,axis=1))

Output:

[[0 4 1 3 2]
 [0 2 3 4 1]]

Above, we can see another example of our function. Here, instead, we have used an optional parameter called the axis. Also here we have considered a 2-dimensional array.

Besides that, all the steps are similar to the first example. Here, we can notice what difference the axis parameter makes. Rather than flattening the array, the operation has been performed on 2 separate arrays, and the shape is also retained.

Numpy argpartition() Vs. argsort()

In this section, we will compare the 2 different NumPy functions. As we have discussed earlier, the definition of the NumPy is a partition. Now let us look at the definition of NumPy argsort(); this function helps us by doing an indirect sort along the given axis using the algorithm specified by the kind keyword. The definition might sound familiar, but the 2 functions perform differently. The next example will clear all of your doubts.

NumPy argpartition()NumPy argsort()
import numpy as ppool
a=[11,2,23,84,55]
print(ppool.argpartition(a,1))
import numpy as ppool
a=[11,2,23,84,55]
print(ppool.argsort(a))
[1 0 2 3 4][1 0 2 4 3]

Here, both functions return us a table of the index number of the parent array. But we can see the outputs are a bit different from each other. This helps us conclude that both functions work differently from each other.

Must Read

Conclusion

In this article, we covered the Numpy argpartition(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples.

We varied the syntax and looked at the output for each case. In the end, we can conclude that the NumPy argpartition() function is used to perform an indirect partition.

I hope this article was able to clear all doubts. But if you have any unsolved queries, feel free to write them down below in the comment section.

If you’re done reading this, why not read about the isin function next?

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments