8000 Merge pull request #22149 from WarrenWeckesser/doc-partition · numpy/numpy@58ee8a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 58ee8a8

Browse files
authored
Merge pull request #22149 from WarrenWeckesser/doc-partition
DOC: Copy-edit the 'partition' docstring.
2 parents 75ae7be + 2d1984a commit 58ee8a8

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

numpy/core/fromnumeric.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -678,10 +678,11 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
678678
Return a partitioned copy of an array.
679679
680680
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
685686
partitions is undefined.
686687
687688
.. versionadded:: 1.8.0
@@ -749,13 +750,30 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
749750
750751
Examples
751752
--------
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])
755757
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::
758775
776+
[0, 1, 2, 1], [2], [3, 3, 2], [5], [6, 7, 7, 7, 7]
759777
"""
760778
if axis is None:
761779
# flatten returns (1, N) for np.matrix, so always use the last axis

0 commit comments

Comments
 (0)
0