MCPcopy Create free account
hub / github.com/numpy/numpy / partition

Function partition

numpy/core/fromnumeric.py:663–772  ·  view source on GitHub ↗

Return a partitioned copy of an array. Creates a copy of the array with its elements rearranged in such a way that the value of the element in k-th position is in the position the value would be in a sorted array. In the partitioned array, all elements before the k-th element

(a, kth, axis=-1, kind='introselect', order=None)

Source from the content-addressed store, hash-verified

661
662@array_function_dispatch(_partition_dispatcher)
663def partition(a, kth, axis=-1, kind='introselect', order=None):
664 """
665 Return a partitioned copy of an array.
666
667 Creates a copy of the array with its elements rearranged in such a
668 way that the value of the element in k-th position is in the position
669 the value would be in a sorted array. In the partitioned array, all
670 elements before the k-th element are less than or equal to that
671 element, and all the elements after the k-th element are greater than
672 or equal to that element. The ordering of the elements in the two
673 partitions is undefined.
674
675 .. versionadded:: 1.8.0
676
677 Parameters
678 ----------
679 a : array_like
680 Array to be sorted.
681 kth : int or sequence of ints
682 Element index to partition by. The k-th value of the element
683 will be in its final sorted position and all smaller elements
684 will be moved before it and all equal or greater elements behind
685 it. The order of all elements in the partitions is undefined. If
686 provided with a sequence of k-th it will partition all elements
687 indexed by k-th of them into their sorted position at once.
688
689 .. deprecated:: 1.22.0
690 Passing booleans as index is deprecated.
691 axis : int or None, optional
692 Axis along which to sort. If None, the array is flattened before
693 sorting. The default is -1, which sorts along the last axis.
694 kind : {'introselect'}, optional
695 Selection algorithm. Default is 'introselect'.
696 order : str or list of str, optional
697 When `a` is an array with fields defined, this argument
698 specifies which fields to compare first, second, etc. A single
699 field can be specified as a string. Not all fields need be
700 specified, but unspecified fields will still be used, in the
701 order in which they come up in the dtype, to break ties.
702
703 Returns
704 -------
705 partitioned_array : ndarray
706 Array of the same type and shape as `a`.
707
708 See Also
709 --------
710 ndarray.partition : Method to sort an array in-place.
711 argpartition : Indirect partition.
712 sort : Full sorting
713
714 Notes
715 -----
716 The various selection algorithms are characterized by their average
717 speed, worst case performance, work space size, and whether they are
718 stable. A stable sort keeps items with the same key in the same
719 relative order. The available algorithms have the following
720 properties:

Callers 1

_medianFunction · 0.90

Calls 4

asanyarrayFunction · 0.85
flattenMethod · 0.80
copyMethod · 0.45
partitionMethod · 0.45

Tested by

no test coverage detected