Add a top_k function to NumPy
Hello all, Following several discussions (linked below) about the proposal, it was decided to add a top_k function to NumPy in issue #15128 <https://github.com/numpy/numpy/issues/15128>. I have recently made a PR ( https://github.com/numpy/numpy/pull/31659) to revive this feature which is now ready to review, and wanted to ping here for awareness and if anyone has comments. Thank you, Maanas Arora --- Links: - Mailing list discussions: - https://mail.python.org/archives/list/numpy-discussion@python.org/thread/F4P... - https://mail.python.org/archives/list/numpy-discussion@python.org/thread/TCR... - NumPy: - https://github.com/numpy/numpy/issues/15128 - https://github.com/numpy/numpy/pull/26666 - https://github.com/numpy/numpy/pull/31659 - Array API: - https://github.com/data-apis/array-api/issues/629 - https://github.com/data-apis/array-api/pull/722
On Wed, 2026-06-17 at 14:55 -0400, Maanas Arora via NumPy-Discussion wrote:
Hello all,
Following several discussions (linked below) about the proposal, it was decided to add a top_k function to NumPy in issue #15128 <https://github.com/numpy/numpy/issues/15128>. I have recently made a PR ( https://github.com/numpy/numpy/pull/31659) to revive this feature which is now ready to review, and wanted to ping here for awareness and if anyone has comments.
Thanks for this, we'll probably merge this very soon. I think there was long an agreement around a `top_k` addition. But I don't think the final API proposal hit the list so here is a very brief summary: top_k(a, k, /, *, axis=-1, mode="largest"|"smallest", sorted=True) returning a tuple: `(topk_values, topk_indices)` Besides from the above, one detail is that the the result would generally omit NaNs so that the result only contains NaNs if there are fewer than `k` non-NaN values. Omitting NaNs seemed more useful and matching to sorting but of course differs from other reduction defaults including min/max as these have `nanmin/nanmax`. Happy to hear final thoughts even if the above is the default by now. Cheers, Sebastian
Thank you, Maanas Arora
---
Links:
- Mailing list discussions: - https://mail.python.org/archives/list/numpy-discussion@python.org/thread/F4P... - https://mail.python.org/archives/list/numpy-discussion@python.org/thread/TCR... - NumPy: - https://github.com/numpy/numpy/issues/15128 - https://github.com/numpy/numpy/pull/26666 - https://github.com/numpy/numpy/pull/31659 - Array API: - https://github.com/data-apis/array-api/issues/629 - https://github.com/data-apis/array-api/pull/722 _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3//lists/numpy-discussion.python.org Member address: sebastian@sipsolutions.net
participants (2)
-
Maanas Arora -
Sebastian Berg