@@ -678,10 +678,11 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
678
678
Return a partitioned copy of an array.
679
679
680
680
Creates a copy of the array with its elements rearranged in such a
681
- way that the value of the element in k-th position is in the
682
- position it would be in a sorted array. All elements smaller than
683
- the k-th element are moved before this element and all equal or
684
- greater are moved behind it. The ordering of the elements in the two
681
+ way that the value of the element in k-th position is in the position
682
+ the value would be in a sorted array. In the partitioned array, all
683
+ elements before the k-th element are less than or equal to that
684
+ element, and all the elements after the k-th element are greater than
685
+ or equal to that element. The ordering of the elements in the two
685
686
partitions is undefined.
686
687
687
688
.. versionadded:: 1.8.0
@@ -749,13 +750,30 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
749
750
750
751
Examples
751
752
--------
752
- >>> a = np.array([3, 4, 2, 1])
753
- >>> np.partition(a, 3)
754
- array([2, 1, 3, 4])
753
+ >>> a = np.array([7, 1, 7, 7, 1, 5, 7, 2, 3, 2, 6, 2, 3, 0])
754
+ >>> p = np.partition(a, 4)
755
+ >>> p
756
+ array([0, 1, 2, 1, 2, 5, 2, 3, 3, 6, 7, 7, 7, 7])
755
757
756
- >>> np.partition(a, (1, 3))
757
- array([1, 2, 3, 4])
758
+ ``p[4]`` is 2; all elements in ``p[:4]`` are less than or equal
759
+ to ``p[4]``, and all elements in ``p[5:]`` are greater than or
760
+ equal to ``p[4]``. The partition is::
761
+
762
+ [0, 1, 2, 1], [2], [5, 2, 3, 3, 6, 7, 7, 7, 7]
763
+
764
+ The next example shows the use of multiple values passed to `kth`.
765
+
766
+ >>> p2 = np.partition(a, (4, 8))
767
+ >>> p2
768
+ array([0, 1, 2, 1, 2, 3, 3, 2, 5, 6, 7, 7, 7, 7])
769
+
770
+ ``p2[4]`` is 2 and ``p2[8]`` is 5. All elements in ``p2[:4]``
771
+ are less than or equal to ``p2[4]``, all elements in ``p2[5:8]``
772
+ are greater than or equal to ``p2[4]`` and less than or equal to
773
+ ``p2[8]``, and all elements in ``p2[9:]`` are greater than or
774
+ equal to ``p2[8]``. The partition is::
758
775
776
+ [0, 1, 2, 1], [2], [3, 3, 2], [5], [6, 7, 7, 7, 7]
759
777
"""
760
778
if axis is None :
761
779
# flatten returns (1, N) for np.matrix, so always use the last axis
0 commit comments