Skip to content

Commit

Permalink
Merge pull request #22149 from WarrenWeckesser/doc-partition
Browse files Browse the repository at this point in the history
DOC: Copy-edit the 'partition' docstring.
  • Loading branch information
charris committed Aug 18, 2022
2 parents 75ae7be + 2d1984a commit 58ee8a8
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions numpy/core/fromnumeric.py
Expand Up @@ -678,10 +678,11 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
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 it would be in a sorted array. All elements smaller than
the k-th element are moved before this element and all equal or
greater are moved behind it. The ordering of the elements in the two
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 are less than or equal to that
element, and all the elements after the k-th element are greater than
or equal to that element. The ordering of the elements in the two
partitions is undefined.
.. versionadded:: 1.8.0
Expand Down Expand Up @@ -749,13 +750,30 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
Examples
--------
>>> a = np.array([3, 4, 2, 1])
>>> np.partition(a, 3)
array([2, 1, 3, 4])
>>> 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, 2, 1, 2, 5, 2, 3, 3, 6, 7, 7, 7, 7])
>>> np.partition(a, (1, 3))
array([1, 2, 3, 4])
``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, 2, 1], [2], [5, 2, 3, 3, 6, 7, 7, 7, 7]
The next example shows the use of multiple values passed to `kth`.
>>> p2 = np.partition(a, (4, 8))
>>> p2
array([0, 1, 2, 1, 2, 3, 3, 2, 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, 2, 1], [2], [3, 3, 2], [5], [6, 7, 7, 7, 7]
"""
if axis is None:
# flatten returns (1, N) for np.matrix, so always use the last axis
Expand Down

0 comments on commit 58ee8a8

Please sign in to comment.