Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.argmax

Returns the integer indices of maximum values in an array, either globally or along a chosen axis. Ties select the first occurrence, and keepdims can preserve reduced dimensions.

How it works

Pass an array-like object to select the position of its largest value. With no axis, NumPy conceptually flattens the input; with an axis, it returns one index per slice, and optional keepdims preserves reduced axes as length-one dimensions.

Syntax

numpy.argmax(a, axis=None, out=None, *, keepdims=<no value>)

Parameters

NameTypeDescription
aarray_likeInput array.
axisint, optionalAxis along which to search; when omitted, indexing is relative to the flattened array.
outarray, optionalDestination array for the result; it must have an appropriate shape and dtype.
keepdimsbool, optionalWhether reduced axes remain as dimensions of size one.

Returns

index_array: ndarray of ints. It contains indices into the input, with the selected axis removed, or retained at size one when keepdims=True.

Key points

  • argmax locates the positions of maximum values rather than returning those maximum values.
  • Leaving axis as None searches the flattened input; setting an axis performs the search along that dimension.
  • The out parameter lets callers place computed indices into a supplied destination array.
  • keepdims=True retains reduced dimensions as singleton axes.
  • The result contains integer indices, not the corresponding maximum values.

Common mistakes

  • When the maximum occurs more than once, the result identifies the first occurrence rather than all matching positions.

Example: Find maximum positions

import numpy as np

x = np.array([[1, 9, 3], [8, 2, 7]])
print(np.argmax(x))
print(np.argmax(x, axis=0))
print(np.argmax(x, axis=1, keepdims=True))

Computes the flattened index, column-wise indices, and row-wise indices while retaining the reduced dimension.

When to use it

Choose per-row maxima

Selecting the winning class or option for each row of a score matrix.

Locate a global maximum

Finding the location of the largest value in an n-dimensional array.

Preserve dimensions

Producing shape-compatible index arrays for later broadcasting.

Interview questions

QWhat does np.argmax return when axis is omitted?

With the default axis=None, argmax returns an integer index into the flattened array. Supplying an axis computes indices independently along that dimension.

Follow-up: How can the multidimensional coordinates of the global maximum be recovered? Use np.unravel_index on the flattened argmax result and the input shape.

QHow do axis and keepdims affect the result shape?

The output shape removes the selected axis, so each remaining position receives the index of its maximum along that axis. With keepdims=True, the reduced axis remains with size one.

Follow-up: Why might keepdims=True be useful? It preserves dimensional alignment, allowing the result to broadcast against the original array.

QWhat are the return type and constraints on out?

The function returns an ndarray of integer indices. An optional out array can receive the result, but it must have the appropriate shape and dtype.

Follow-up: What happens with tied maxima? NumPy returns the index corresponding to the first occurrence.

  • numpy.ndarray.argmax
  • numpy.argmin
  • numpy.amax
  • numpy.unravel_index

Source: numpy.argmax — NumPy v2.4 Manual. Checked 2026-10-15.