numpy.partition#
- numpy.partition(a,kth,axis=-1,kind='introselect',order=None)[source]#
Return a partitioned copy of an array.
Creates a copy of the array and partially sorts it in such a way thatthe value of the element in k-th position is in the position it would bein a sorted array. In the output array, all elements smaller than the k-thelement are located to the left of this element and all equal or greaterare located to its right. The ordering of the elements in the twopartitions on the either side of the k-th element in the output array isundefined.
- Parameters:
- aarray_like
Array to be sorted.
- kthint or sequence of ints
Element index to partition by. The k-th value of the elementwill be in its final sorted position and all smaller elementswill be moved before it and all equal or greater elements behindit. The order of all elements in the partitions is undefined. Ifprovided with a sequence of k-th it will partition all elementsindexed by k-th of them into their sorted position at once.
Deprecated since version 1.22.0:Passing booleans as index is deprecated.
- axisint or None, optional
Axis along which to sort. If None, the array is flattened beforesorting. The default is -1, which sorts along the last axis.
- kind{‘introselect’}, optional
Selection algorithm. Default is ‘introselect’.
- orderstr or list of str, optional
Whena is an array with fields defined, this argumentspecifies which fields to compare first, second, etc. A singlefield can be specified as a string. Not all fields need bespecified, but unspecified fields will still be used, in theorder in which they come up in the dtype, to break ties.
- Returns:
- partitioned_arrayndarray
Array of the same type and shape asa.
See also
ndarray.partitionMethod to sort an array in-place.
argpartitionIndirect partition.
sortFull sorting
Notes
The various selection algorithms are characterized by their averagespeed, worst case performance, work space size, and whether they arestable. A stable sort keeps items with the same key in the samerelative order. The available algorithms have the followingproperties:
kind
speed
worst case
work space
stable
‘introselect’
1
O(n)
0
no
All the partition algorithms make temporary copies of the data whenpartitioning along any but the last axis. Consequently,partitioning along the last axis is faster and uses less space thanpartitioning along any other axis.
The sort order for complex numbers is lexicographic. If both thereal and imaginary parts are non-nan then the order is determined bythe real parts except when they are equal, in which case the orderis determined by the imaginary parts.
The sort order of
np.nanis bigger thannp.inf.Examples
>>>importnumpyasnp>>>a=np.array([7,1,7,7,1,5,7,2,3,2,6,2,3,0])>>>p=np.partition(a,4)>>>parray([0, 1, 2, 1, 2, 5, 2, 3, 3, 6, 7, 7, 7, 7]) # may vary
p[4]is 2; all elements inp[:4]are less than or equaltop[4], and all elements inp[5:]are greater than orequal top[4]. The partition is:[0,1,2,1],[2],[5,2,3,3,6,7,7,7,7]
The next example shows the use of multiple values passed tokth.
>>>p2=np.partition(a,(4,8))>>>p2array([0, 1, 2, 1, 2, 3, 3, 2, 5, 6, 7, 7, 7, 7])
p2[4]is 2 andp2[8]is 5. All elements inp2[:4]are less than or equal top2[4], all elements inp2[5:8]are greater than or equal top2[4]and less than or equal top2[8], and all elements inp2[9:]are greater than orequal top2[8]. The partition is:[0,1,2,1],[2],[3,3,2],[5],[6,7,7,7,7]