dpnp.partition

dpnp.partition(a, kth, axis=-1, kind='introselect', order=None)[source]

Return a partitioned copy of an array.

For full documentation refer to numpy.partition.

Parameters:
  • a ({dpnp.ndarray, usm_ndarray}) -- Array to be sorted.

  • kth ({int, sequence of ints}) -- Element 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 ({None, int}, optional) --

    Axis along which to sort. If None, the array is flattened before sorting. The default is -1, which sorts along the last axis.

    Default: -1.

Returns:

out -- Array of the same type and shape as a.

Return type:

dpnp.ndarray

Limitations

Parameters kind and order are only supported with its default value. Otherwise NotImplementedError exception will be raised.

See also

dpnp.ndarray.partition

Equivalent method.

dpnp.argpartition

Indirect partition.

dpnp.sort

Full sorting.

Examples

>>> import dpnp 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
array([0, 1, 1, 2, 2, 2, 3, 3, 5, 7, 7, 7, 7, 6]) # may vary

p[4] is 2; all elements in p[:4] are less than or equal to p[4], and all elements in p[5:] are greater than or equal to p[4]. The partition is:

[0, 1, 1, 2], [2], [2, 3, 3, 5, 7, 7, 7, 7, 6]

The next example shows the use of multiple values passed to kth.

>>> p2 = np.partition(a, (4, 8))
>>> p2
array([0, 1, 1, 2, 2, 2, 3, 3, 5, 6, 7, 7, 7, 7])

p2[4] is 2 and p2[8] is 5. All elements in p2[:4] are less than or equal to p2[4], all elements in p2[5:8] are greater than or equal to p2[4] and less than or equal to p2[8], and all elements in p2[9:] are greater than or equal to p2[8]. The partition is:

[0, 1, 1, 2], [2], [2, 3, 3], [5], [6, 7, 7, 7, 7]