NumPy amax()

The amax() function computes maximum value along a specified axis in an array.

Example

import numpy as np

array1 = np.array([5, 2, 8, 1, 9])

# find maximum value in array1 maxValue = np.amax(array1)
print(maxValue) # Output: 9

amax() Syntax

The syntax of amax() is:

numpy.amax(a, axis = None, keepdims = False)

amax() Arguments

The amax() function takes following arguments:

  • a - the input array
  • axis (optional) - the axis along which the maximum value is computed
  • keepdims (optional) - whether to preserve the input array's dimension (bool)

amax() Return Value

The amax() function returns the maximum element from an array or along a specified axis.


Example 1: amax() With 2-D Array

The axis argument defines how we can find the maximum element in a 2-D array.

  • If axis = None, the array is flattened and the maximum value of the flattened array is returned.
  • If axis = 0, the maximum value is calculated column-wise.
  • If axis = 1, the maximum value is calculated row-wise.
import numpy as np

array1 = np.array([[10, 17, 25], 
                              [15, 11, 22]])
                  
# calculate the maximum value of the flattened array result1 = np.amax(array1)
print('The maximum value of the flattened array:', result1)
# calculate the column-wise maximum values result2 = np.amax(array1, axis=0)
print('Column-wise maximum values (axis 0):', result2)
# calculate the row-wise maximum values result3 = np.amax(array1, axis=1)
print('Row-wise maximum values (axis 1):', result3)

Output

The maximum value of the flattened array: 25
Column-wise maximum values (axis 0): [15 17 25]
Row-wise maximum values (axis 1): [25 22]

Here,

  1. np.amax(array1) calculates the maximum value of the flattened array. It returns the largest element in the entire array.
  2. np.amax(array1, axis=0) calculates the column-wise maximum values. It returns an array containing the maximum value for each column.
  3. np.amax(array1, axis=1) calculates the row-wise maximum values. It returns an array containing the maximum value for each row

Example 2: amax() With keepdims

When keepdims = True, the dimensions of the resulting array matches the dimension of the input array.

import numpy as np

array1 = np.array([[10, 17, 25], 
                                [15, 11, 22]])

print('Dimensions of original array:', array1.ndim)

result = np.amax(array1, axis=1)

print('\nWithout keepdims:')
print(result)
print('Dimensions of array:', result.ndim) 

# set keepdims to True to retain the dimension of the input array result = np.amax(array1, axis=1, keepdims=True)
print('\nWith keepdims:') print(result) print('Dimensions of array:', result.ndim)

Output

Dimensions of original array: 2

Without keepdims:
[25 22]
Dimensions of array: 1

With keepdims:
[[25]
 [22]]
Dimensions of array: 2

Without keepdims, the result is simply a one-dimensional array containing the maximum values along the specified axis.

With keepdims, the resulting array has the same number of dimensions as the input array.