numpy.ndarray.partition#
method
- ndarray.partition(kth, axis=-1, kind='introselect', order=None, descending=None)#
Partially sorts the array in such a way that the value of the element in the k-th position is in the position it would be in a sorted array. In the output array, all elements that would be to the left of the k-th element in a sorted array are located to the left of this element and all that would be to the right are located to its right. The ordering of the elements in the two partitions on the either side of the k-th element in the output array is undefined.
- Parameters:
- kthint or sequence of ints
Element index to partition by. 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.
Deprecated since version 1.22.0: Passing booleans as index is deprecated.
- axisint, optional
Axis along which to sort. Default is -1, which means sort along the last axis.
- 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 to 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.
See also
numpy.partitionReturn a partitioned copy of an array.
argpartitionIndirect partition.
sortFull sort.
Notes
See
np.partitionfor notes on the different algorithms.Examples
>>> import numpy as np >>> a = np.array([3, 4, 2, 1]) >>> a.partition(3) >>> a array([2, 1, 3, 4]) # may vary
>>> a.partition((1, 3)) >>> a array([1, 2, 3, 4])