10000 Merge pull request #26716 from JuliaPoo/cont-15128-topk-feat · numpy/numpy@c21ac10 · GitHub
[go: up one dir, main page]

Skip to content

Commit c21ac10

Browse files
authored
Merge pull request #26716 from JuliaPoo/cont-15128-topk-feat
DOC: Add clarifications np.argpartition
2 parents 2efae2b + 456c4a9 commit c21ac10

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

numpy/_core/fromnumeric.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,8 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
823823
the real parts except when they are equal, in which case the order
824824
is determined by the imaginary parts.
825825
826+
The sort order of ``np.nan`` is bigger than ``np.inf``.
827+
826828
Examples
827829
--------
828830
>>> a = np.array([7, 1, 7, 7, 1, 5, 7, 2, 3, 2, 6, 2, 3, 0])
@@ -918,7 +920,14 @@ def argpartition(a, kth, axis=-1, kind='introselect', order=None):
918920
919921
Notes
920922
-----
921-
See `partition` for notes on the different selection algorithms.
923+
The returned indices are not guaranteed to be sorted according to
924+
the values. Furthermore, the default selection algorithm ``introselect``
925+
is unstable, and hence the returned indices are not guaranteed
926+
to be the earliest/latest occurrence of the element.
927+
928+
`argpartition` works for real/complex inputs with nan values,
929+
see `partition` for notes on the enhanced sort order and
930+
different selection algorithms.
922931
923932
Examples
924933
--------

numpy/_core/tests/test_multiarray.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10211,6 +10211,16 @@ def test_partition_fp(N, dtype):
1021110211
assert_arr_partitioned(np.sort(arr)[k], k,
1021210212
arr[np.argpartition(arr, k, kind='introselect')])
1021310213

10214+
# Check that `np.inf < np.nan`
10215+
# This follows np.sort
10216+
arr[0] = np.nan
10217+
arr[1] = np.inf
10218+
o1 = np.partition(arr, -2, kind='introselect')
10219+
o2 = arr[np.argpartition(arr, -2, kind='introselect')]
10220+
for out in [o1, o2]:
10221+
assert_(np.isnan(out[-1]))
10222+
assert_equal(out[-2], np.inf)
10223+
1021410224
def test_cannot_assign_data():
1021510225
a = np.arange(10)
1021610226
b = np.linspace(0, 1, 10)

0 commit comments

Comments
 (0)
0