Advertisement
Finding the highest value in a list is easy. But when you need to know where that value is, especially in larger or multi-dimensional arrays, NumPy’s argmax() function becomes useful. This isn’t a fancy tool, but it’s one that shows up in a lot of practical scenarios. It tells you the index of the maximum value, not the value itself.
This matters when position is more important than content—like when choosing the most likely prediction in a machine learning model. With one function call, you get quick, clean results without needing to loop through data by hand.
The argmax() function doesn’t return the largest number—it returns where that number is located. For one-dimensional arrays, it gives back a single index. In multi-dimensional arrays, you can use the axis argument to decide how you want NumPy to search. For example, argmax(axis=0) checks down each column, and argmax(axis=1) checks across rows.
This is very helpful when your data is structured, like in tables or grids. You don’t have to flatten arrays unless you want a single index across the entire dataset. This flexibility is what makes argmax() widely used in different fields, from data science to physics simulations.
Let’s say you’re classifying images. The model might return probabilities for each category. The highest number tells you the most likely result, but the index of that number tells you which class it is. That’s where argmax() fits in—it gives you that position right away.
The function is simple:
numpy.argmax(a, axis=None, out=None)
The return depends on the shape of the input and the axis. If you don’t specify the axis, you’ll get a single index. If you search along a particular axis in a multi-dimensional array, the result is an array of indices for each slice.
Keep in mind that if the maximum value appears more than once, argmax() only returns the first occurrence. This is expected behavior. If you want to get all locations, you’ll need another method, like combining np.max() with np.where().
Let’s start with a simple example using a one-dimensional array:
import numpy as np
arr = np.array([4, 6, 8, 2, 10, 1])
index = np.argmax(arr)
print(index) # Output: 4
This gives you the position of the highest number, not the number itself. If you need the value, you can fetch it with arr[index].
In a two-dimensional case:
arr2d = np.array([
[1, 7, 3],
[4, 2, 9]
])
print(np.argmax(arr2d, axis=0)) # Output: [1 0 1]
print(np.argmax(arr2d, axis=1)) # Output: [1 2]
Here, axis=0 checks each column and tells you the row index of the highest value in that column. axis=1 checks across rows and returns the index of the max in each row.
In machine learning, after a softmax layer, you’ll often see this:
probs = np.array([0.2, 0.5, 0.3])
label = np.argmax(probs)
print(label) # Output: 1
This means the predicted class is the one at index 1.
You might also see argmax() used in tracking top performers or best results in datasets. For example, in retail sales:
sales = np.array([
[100, 200, 150],
[120, 210, 180]
])
best_products = np.argmax(sales, axis=1)
print(best_products) # Output: [1 1]
Each row could represent monthly sales, and this code tells you which product performed best each month.
In some cases, you may want to apply conditions. For instance, finding the max value only above a threshold:
arr = np.array([5, 12, 3, 17, 8])
mask = np.where(arr > 10, arr, -np.inf)
print(np.argmax(mask)) # Output: 3
Values below or equal to 10 are ignored by replacing them with negative infinity. argmax() then selects the highest of the remaining.
While argmax() is quick and generally reliable, there are a few things to keep in mind. It doesn’t work with empty arrays—you’ll get an error. Always check that the array isn’t empty before calling it.
Another detail is how it behaves when there are ties. If the highest value appears more than once, it will return the index of the first match. If you care about all instances, you’ll need something like:
arr = np.array([2, 9, 9, 4])
indices = np.where(arr == np.max(arr))[0]
print(indices) # Output: [1 2]
This lets you see every position where the max value appears, though it takes more processing.
For large datasets, avoid flattening unless necessary. Using an axis is usually faster and more memory-efficient than flattening and searching across the entire array.
You usually won’t need the out parameter, but in performance-critical tasks where allocations matter, it can help avoid unnecessary memory use. This is more common in scientific computing or when running operations in tight loops.
Lastly, argmax() works on arrays with any shape but not on masked arrays directly. If you're working with masked values, convert or filter your data first.
NumPy’s argmax() is one of those tools you don’t think about much until you need it—and then it saves time and effort. Instead of searching manually or writing loops, you can just call one function to find where the highest value lives. That’s especially useful when position matters more than the value itself, such as in predictions or comparisons. It works well across different types of arrays and scales smoothly from small data to large datasets. As long as you understand what it returns and how it handles edge cases like ties, duplicates, special values, and empty arrays, it's a reliable, everyday part of the NumPy toolkit.
Advertisement
How Binary and Scalar Embedding Quantization for Significantly Faster and Cheaper Retrieval helps reduce memory use, lower costs, and improve search speed—without a major drop in accuracy
Explore Idefics2, an advanced 8B vision-language model offering open access, high performance, and flexibility for developers, researchers, and the AI community
How the NumPy argmax() function works, when to use it, and how it helps you locate maximum values efficiently in any NumPy array
Learn the regulatory impact of Google and Meta antitrust lawsuits and what it means for the future of tech and innovation.
Learn everything about mastering LeNet, from architectural insights to practical implementation. Understand its structure, training methods, and why it still matters today
Learn how machine learning predicts product failures, improves quality, reduces costs, and boosts safety across industries
CyberSecEval 2 is a robust cybersecurity evaluation framework that measures both the risks and capabilities of large language models across real-world tasks, from threat detection to secure code generation
What sets Meta’s LLaMA 3.1 models apart? Explore how the 405B, 70B, and 8B variants deliver better context memory, balanced multilingual performance, and smoother deployment for real-world applications
Explore Google's AI supercomputer performance and Nvidia's MLPerf 3.0 benchmark win in next-gen high-performance AI systems
Explore Llama 3 by Meta, the latest open LLM designed for high performance and transparency. Learn how this model supports developers, researchers, and open AI innovation
Learn what ChatGPT Search is and how to use it as a smart, AI-powered search engine
How to merge two dictionaries in Python using different methods. This clear and simple guide helps you choose the best way to combine Python dictionaries for your specific use case