bundles / numpy 2.4.4 / numpy / partition
_ArrayFunctionDispatcher
numpy:partition
source: /numpy/_core/fromnumeric.py :732
Signature
def partition ( a , kth , axis = -1 , kind = introselect , order = None ) Summary
Return a partitioned copy of an array.
Extended Summary
Creates a copy of the array and partially sorts it in such a way that the value of the element in k-th position is in the position it would be in a sorted array. In the output array, all elements smaller than the k-th element are located to the left of this element and all equal or greater 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
a: array_likeArray to be sorted.
kth: int or sequence of intsElement index to partition by. The k-th value of the element will be in its final sorted position and all smaller elements will be moved before it and all equal or greater elements behind it. The order of all elements in the partitions 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.
axis: int or None, optionalAxis along which to sort. If None, the array is flattened before sorting. The default is -1, which sorts along the last axis.
kind: {'introselect'}, optionalSelection algorithm. Default is 'introselect'.
order: str or list of str, optionalWhen
ais an array with fields defined, this argument specifies which fields to compare first, second, etc. A single field can be specified as a string. 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.
Returns
partitioned_array: ndarrayArray of the same type and shape as
a.
Notes
The various selection algorithms are characterized by their average speed, worst case performance, work space size, and whether they are stable. A stable sort keeps items with the same key in the same relative order. The available algorithms have the following properties:
================= ======= ============= ============ ======= kind speed worst case work space stable ================= ======= ============= ============ ======= 'introselect' 1 O(n) 0 no ================= ======= ============= ============ =======
All the partition algorithms make temporary copies of the data when partitioning along any but the last axis. Consequently, partitioning along the last axis is faster and uses less space than partitioning along any other axis.
The sort order for complex numbers is lexicographic. If both the real and imaginary parts are non-nan then the order is determined by the real parts except when they are equal, in which case the order is determined by the imaginary parts.
The sort order of np.nan is bigger than np.inf.
Examples
import numpy as np a = np.array([7, 1, 7, 7, 1, 5, 7, 2, 3, 2, 6, 2, 3, 0]) p = np.partition(a, 4)✓
p
✗p2 = np.partition(a, (4, 8)) p2✓
See also
- argpartition
Indirect partition.
- ndarray.partition
Method to sort an array in-place.
- sort
Full sorting
Aliases
-
numpy.partition