flopscope.numpy.argpartition
fnp.argpartition(a, kth, axis=-1, kind='introselect', order=None)[flopscope source][numpy 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.
Adapted from NumPy docs np.argpartition
Indirect partition; cost = n per slice.
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
- a:array_like
Array to sort.
- kth:int or sequence of ints
Element index to partition by. The k-th element will be in its final sorted position and all smaller elements will be moved before it and all larger 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 of them into their sorted position at once.
Deprecated since 1.22.0.- axis:int 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
Selection algorithm. Default is 'introselect'
- order:str or list of str, optional
When
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, 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.
Returns
- index_array:ndarray, int
Array of indices that partition
aalong the specified axis. Ifais one-dimensional,a[index_array]yields a partitioneda. More generally, flops.take_along_axis(a, index_array, axis=axis) always yields the partitioneda, irrespective of dimensionality.
See also
- we.flops.partition Describes partition algorithms used.
- ndarray.partition Inplace partition.
- we.flops.argsort Full indirect sort.
- we.flops.take_along_axis Apply
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 introselect
is unstable, and hence the returned indices are not guaranteed
to be the earliest/latest occurrence of the element.
argpartition works for real/complex inputs with nan values, see partition for notes on the enhanced sort order and different selection algorithms.
Examples
One dimensional array:
>>> import flopscope.numpy as fnp
>>> x = flops.array([3, 4, 2, 1])
>>> x[flops.argpartition(x, 3)]
array([2, 1, 3, 4]) # may vary
>>> x[flops.argpartition(x, (1, 3))]
array([1, 2, 3, 4]) # may vary>>> x = [3, 4, 2, 1]
>>> flops.array(x)[flops.argpartition(x, 3)]
array([2, 1, 3, 4]) # may varyMulti-dimensional array:
>>> x = flops.array([[3, 4, 2], [1, 3, 1]])
>>> index_array = flops.argpartition(x, kth=1, axis=-1)
>>> # below is the same as flops.partition(x, kth=1)
>>> flops.take_along_axis(x, index_array, axis=-1)
array([[2, 3, 4],
[1, 1, 3]])