# NumPy argpatition()

Rate this post
numpy.argpartition(a, kth, axis=-1, kind='introselect', order=None)

The NumPy argpatition function performs an indirect partition along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as a that index data along the given axis in partitioned order.

The following table shows the return value of the function:

Related: See partition for notes on the different selection algorithms.

Let’s dive into some examples to show how the function is used in practice:

### Examples

One-dimensional array:

import numpy as np

x = np.array([3, 4, 2, 1])

print(x[np.argpartition(x, 3)])
# [2 1 3 4]

print(x[np.argpartition(x, (1, 3))])
# [1 2 3 4]

Multi-dimensional array:

import numpy as np

x = np.array([3, 4, 2, 1])

print(x[np.argpartition(x, 3)])
# [2 1 3 4]

print(x[np.argpartition(x, (1, 3))])
# [1 2 3 4]

x = [3, 4, 2, 1]
print(np.array(x)[np.argpartition(x, 3)])
# [2 1 3 4]

Any master coder has a “hands-on” mentality with a bias towards action. Try it yourself—play with the function in the following interactive code shell:

Exercise: Change the parameters of your polynomials and print them without the comparisons. Do you understand where they come from?

Master NumPy—and become a data science pro: