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 parameter. Then we will see the application of all the theory part through a couple of examples. But at 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.
This is the general syntax for our function. In the next section we will discuss the various parameters associated with it.
1. a: array_like
This parameter represents the input parameter on which the function needs to be applied.
2. kth: int or sequence if int
As discussed a bit about 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 moves 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.
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.
- Index_array: ndarray, int
Upon completing the program, the function returns the index number with the partition along the specified axis.
We have covered all the necessary theory associated with our function. Also, we have looked at its definition and developed a brief understanding of our function. This section will be looking 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 which we have used a print statement along with our syntax to achieve the desired output. For the first example, we have defined the kth value to be 3 the index number. Now 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 as when compared to their index numbers in 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))
[[0 4 1 3 2] [0 2 3 4 1]]
Above we can see another example for 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 that of the first example. Here we can notice that 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 argpartition. Now let us look at the definition of NumPy argsort(), this function helps us by doing 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()|
Here both the function return us a table of the index number of parent array. But we can see the output is a bit different from each other. This helps us conclude that both functions work differently from each other.
- Python Help Function | How to make use of Python Help
- Numpy Mean: Implementation and Importance
- METHODS TO CONVERT TUPLE TO STRING IN PYTHON
- Python list index out of range: Error and Resolution
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 function NumPy argpartition() is used to perform an indirect partition.
I hope this article was able to clear all doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this, why not read about the isin function next.