How do I get indices of N maximum values in a NumPy array?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding the indices of the top N values in a NumPy array is a common ranking task in analytics, machine learning, and scientific code. The best method depends on whether you need the results fully sorted or only need the correct top group as efficiently as possible. NumPy gives you both options, and choosing the right one matters for large arrays.
Use argsort for the Straightforward Version
If readability matters more than raw performance, np.argsort is the simplest answer. It returns the indices that would sort the array.
Output:
This works because argsort orders the entire array from smallest to largest. Slicing the last n indices gives the largest values, and reversing that slice puts them in descending order.
The downside is that it sorts everything, which is more work than necessary when the array is large and n is small.
Use argpartition for Better Performance
When you only need the top N elements, np.argpartition is usually the better tool. It does not fully sort the array. Instead, it partitions the values so the largest N elements end up in the last N positions.
The second argsort is important. argpartition guarantees membership in the top group, but not ordering inside that group. If output order matters, sort the selected subset afterward.
For large arrays, this is often much faster than sorting the entire input.
Handle Multi-Dimensional Arrays Deliberately
If the array has more than one dimension, decide what kind of answer you want.
For global top values across the whole array, flatten first and then convert the flat indices back to coordinates:
This approach is useful when you want the largest values in the entire matrix regardless of row or column.
If instead you need the top N values per row or per column, use axis-aware logic rather than flattening the whole array.
Top Values Along an Axis
Here is a row-wise example using argsort:
This returns the top column indices for each row. Axis-based problems often need a slightly different shape from the global-index case, so be explicit about the requirement before choosing the method.
Think About Edge Cases
Robust helpers should decide what happens when:
- '
n <= 0' - '
nis larger than the array size' - the array contains duplicate maximum values
- the array is empty
A slightly more defensive helper looks like this:
Using a stable sort for the final ordering helps keep ties predictable within the selected top group.
Why Not Repeatedly Call max or index
A naive approach is to repeatedly find the maximum, record its index, remove it, and repeat. That is usually slower, more awkward with duplicates, and less clear than using NumPy's built-in indexing tools.
NumPy is designed for vectorized array operations. Lean into that design instead of fighting it with repeated Python-level loops.
Common Pitfalls
One common mistake is assuming argpartition returns sorted indices. It does not. You still need to sort the selected slice if order matters.
Another mistake is forgetting that flattened indices are not row and column coordinates. Use np.unravel_index when you need positions back in the original shape.
Developers also sometimes use list.index(max_value) in a loop. That is inefficient and wrong when duplicate values exist.
Finally, define how you want ties handled. If several elements share the same value, more than one answer can be valid unless your application requires a deterministic tie-break rule.
Summary
- Use
np.argsortwhen you want the clearest full-sort solution. - Use
np.argpartitionwhen performance matters andNis small relative to the array size. - Sort the selected
argpartitionresult afterward if output order matters. - Flatten first and use
np.unravel_indexfor global top values in multi-dimensional arrays. - Handle edge cases such as empty arrays, oversized
n, and ties on purpose.

