Numpy argmax() Function



The Numpy argmax() function is used to find the index of the maximum value in an array along a specified axis. It is particularly useful for tasks that require identifying the position of the highest value in data structures, such as arrays or matrices.

The Numpy argmax() function works on both one-dimensional and multi-dimensional arrays. When the funtion is used with multi-dimensional arrays, we need to specify the axis along which to find the maximum index.

In an array, if the maximum element appears multiple times, the numpy.argmax() function will return the index of the first occurrence of the maximum value.

Syntax

Following is the syntax of the Numpy argmax() function −

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

Parameters

Following are the parameters of the Numpy argmax() function −

  • a: The input array from which the maximum value index is to be found.
  • out (optional): If provided, the result will be placed in this output array. Must be of the same shape and type as the expected output.
  • keepdims: If set to True, the reduced axes are retained in the result as dimensions of size 1, meaning they are not removed. This ensures the result has the same number of dimensions as the original array, which is useful for broadcasting.
  • axis (optional): The axis along which to find the index of the maximum value.
  • None (default): Finds the index of the maximum value in the flattened array.
  • Axis can be an integer (e.g., 0 for rows, 1 for columns in a 2D array).

Return Type

This function returns an integer (for 1D arrays) or an array of integers (for multi-dimensional arrays), indicating the indices of the maximum values along the specified axis.

Example

Following is a basic example to find the index of the maximum value in a one-dimensional array using Numpy argmax() function −

import numpy as np
array = np.array([10, 20, 5, 30, 15])
max_index = np.argmax(array)
print("Array:", array)
print("Index of Maximum Value:", max_index)

Output

Array: [10 20  5 30 15]
Index of Maximum Value: 3

Example: Using axis Parameter

The axis parameter is used to find the maximum value indices along a specific axis in multi-dimensional arrays.

In the following example, we find the maximum element along rows and columns by setting the axis parameter to 1 (row-wise) and 0 (column-wise), respectively, in the numpy.argmax() function −

import numpy as np

array_2d = np.array([[85, 3, 7],
                     [2, 8, 6],
                     [66, 50, 9]])
# Finding max indices along rows
max_index_row = np.argmax(array_2d, axis=1)  
# Finding max indices along columns
max_index_col = np.argmax(array_2d, axis=0)  
print("2D Array:")
print(array_2d)
print("Indices of Maximum Values Along Rows:", max_index_row)
print("Indices of Maximum Values Along Columns:", max_index_col)

Output

2D Array:
[[85  3  7]
 [ 2  8  6]
 [66 50  9]]
Indices of Maximum Values Along Rows: [0 1 0]
Indices of Maximum Values Along Columns: [0 2 2]

Example: Flattened Array

When axis=None, the array is flattened, and the index of the maximum value in the flattened array is returned.

Here is an example where we find the maximum element in a 2D array by flattening it first −

import numpy as np

array_2d = np.array([[10, 20, 30],
                     [5, 25, 15],
                     [40, 35, 50]])

max_index_flat = np.argmax(array_2d)

print("2D Array:")
print(array_2d)
print("Index of Maximum Value in Flattened Array:", max_index_flat)

Output

2D Array:
[[10 20 30]
 [ 5 25 15]
 [40 35 50]]
Index of Maximum Value in Flattened Array: 8

Example: Using out Parameter

The out parameter can be used to store the result in a pre-allocated array. The output array must have the appropriate shape and data type.

import numpy as np

array = np.array([4, 12, 7, 18, 9])
out_array = np.empty((), dtype=int)
np.argmax(array, out=out_array)

print("Array:", array)
print("Index of Maximum Value (using out):", out_array)

Output

Array: [ 4 12  7 18  9]
Index of Maximum Value (using out): 3

Example: Mutiple Occurence of Maximum Value

The Numpy argmax() function returns the index of the first occurrence of the maximum value if it appears multiple times in the array.

In the following example, the value 100 occurs twice, but the numpy.argmax() function returns the index of its first occurrence −

import numpy as np

array = np.array([4, 59, 100, 76, 19, 100])
max_Value = np.argmax(array,)

print("Array:", array)
print("Index of Maximum Value:", max_Value)

Output

Array: [  4  59 100  76  19 100]
Index of Maximum Value: 2
numpy_array_manipulation.htm
Advertisements