numpy.argpartition#
- numpy.argpartition(a, kth, axis=-1, kind=<no value>, order=None, descending=<no value>)[source]#
Perform 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.
- Parameters:
- aarray_like
Array to sort.
- kthint or sequence of ints
Element index to partition by. In the returned array, the k-th value of the array will be in the position it would be in a sorted array, all elements that are less than this element (or greater if descending is True) will be moved before it, and all elements that are greater than or equal to this element (or less than or equal if descending is True) will be moved after it. The order of all elements within each partition is undefined. If provided with a sequence of k-th it will partition all elements indexed by k-th of them into their sorted position at once.
- axisint or None, optional
Axis along which to sort. The default is -1 (the last axis). If None, the flattened array is used.
- kind{‘introselect’}, optional
NumPy currently offers only one selection algorithm, ‘introselect’, and this parameter provides no additional functionality. Default is
None.- orderstr or list of str, optional
When a is an array with fields defined, this argument specifies which fields to compare first, second, etc. A single field can be specified as a string, and not all fields need be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties.
- descendingbool, optional
Sort order. If
True, the array will be partitioned in descending order. IfFalseorNone, the array will be partitioned in ascending order. Values that are NaN are partitioned towards the end of the array regardless of order. Default:None.New in version 2.6.0.
- Returns:
- index_arrayndarray, int
Array of indices that partition a along the specified axis. If a is one-dimensional,
a[index_array]yields a partitioned a. More generally,np.take_along_axis(a, index_array, axis=axis)always yields the partitioned a, irrespective of dimensionality.
See also
partitionDescribes partition algorithms used.
ndarray.partitionInplace partition.
argsortFull indirect sort.
take_along_axisApply
index_arrayfrom argpartition to an array as if by calling partition.
Notes
The returned indices are not guaranteed to be sorted according to the values. Furthermore, the default selection algorithm
introselectis unstable, and hence the returned indices are not guaranteed to be the earliest/latest occurrence of the element.argpartitionworks for real/complex inputs with nan values, seepartitionfor notes on the enhanced sort order and different selection algorithms.Examples
One dimensional array:
>>> import numpy as np >>> x = np.array([3, 4, 2, 1]) >>> x[np.argpartition(x, 3)] array([2, 1, 3, 4]) # may vary >>> x[np.argpartition(x, (1, 3))] array([1, 2, 3, 4]) # may vary
>>> x = [3, 4, 2, 1] >>> np.array(x)[np.argpartition(x, 3)] array([2, 1, 3, 4]) # may vary
Multi-dimensional array:
>>> x = np.array([[3, 4, 2], [1, 3, 1]]) >>> index_array = np.argpartition(x, kth=1, axis=-1) >>> # below is the same as np.partition(x, kth=1) >>> np.take_along_axis(x, index_array, axis=-1) array([[2, 3, 4], [1, 1, 3]])