NumPy max()

The max() method returns the largest element of an array along an axis.

import numpy as np

array1 = np.array([10, 12, 14, 11, 5])

# return the largest element maxValue= np.max(array1)
print(maxValue) # Output: 14

max() Syntax

The syntax of max() is:

numpy.max(array, axis = None, out = None, keepdims = <no value>, initial=<no value>, where=<no value>)

max() Arguments

The max() method takes six arguments:

  • array - input array
  • axis (optional) - axis along which maximum value is returned (int)
  • out (optional) - array to store the output
  • keepdims (optional) - whether to preserve the input array's dimension (bool)
  • initial (optional) - the minimum value of an output element (scalar)
  • where (optional) - elements to include in the maximum value calculation(array of bool)

max() Return Value

The max() method returns the largest element.

Note: If at least one element of the input array in NaN, max() will return NaN.


Example 1: max() With 2D Array

The axis argument defines how we can handle the largest element in a 2D array.

  • If axis = None, the array is flattened and the maximum of the flattened array is returned.
  • If axis = 0, the maximum of the largest element in each column is returned.
  • If axis = 1, the maximum of the largest element in each row is returned.
import numpy as np

array = np.array([[10, 17, 25], 
                  [15, 11, 22]])
                  
# return the largest element of the flattened array maxValue = np.max(array)
print('The largest element in the flattened array: ', maxValue)
# return the largest element in each column maxValue = np.max(array, axis = 0)
print('The largest element in each column (axis 0): ', maxValue)
# return the largest element in each row maxValue = np.max(array, axis = 1)
print('The largest element in each row (axis 1): ', maxValue)

Output

The largest element in the flattened array:  25
The largest element in each column (axis 0):  [15 17 25]
The largest element in each row (axis 1):  [25 22]

Example 2: Use out to Store the Result in Desired Location

In our previous examples, the max() function generated a new output array.

However, we can use an existing array to store the output using the out argument.

import numpy as np

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

# create an empty array
array2= np.array([0, 0, 0])

# pass the 'out' argument to store the result in array2 np.max(array1, axis = 0, out = array2)
print(array2)

Output

[15 19 25]

Example 3: max() With keepdims

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

import numpy as np

array1 = np.array([[10, 17, 25], 
                   [15, 11, 22]])
print('Dimensions of original array: ', array1.ndim)

maxValue = np.max(array1, axis = 1)
print('\n Without keepdims: \n', maxValue) print('Dimensions of array: ', maxValue.ndim)
# set keepdims to True to retain the dimension of the input array maxValue = np.max(array1, axis = 1, keepdims = True)
print('\n With keepdims: \n', maxValue) print('Dimensions of array: ', maxValue.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 of indices.

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


Example 4: max() With initial

We use initial to define the minimum value max() can return. If the maximum value of the array is smaller than the initial value, initial is returned.

import numpy as np

# max value > initial, returns max value
array1 = np.array([[10, 25, 17, 16, 14]])
maxValue = np.max(array1, initial = 6)
print(maxValue) # max value < initial, returns initial array2 = np.array([[10, 25, 17, 16, 14]])
maxValue = np.max(array2, initial = 26)
print(maxValue) # in case of an empty array, initial value is returned array3 = np.array([])
maxValue = np.max(array3, initial = 5)
print(maxValue)

Output

25
26
5.0

Example 5: Use where to Find Maximum of Filtered Array

The optional argument where specifies elements to include to calculate the maximum value.

import numpy as np

arr = np.array([[12, 25, 32],
              	[47, 50, 36]])

# take max of entire array result1 = np.max(arr) # max of only odd elements result2 = np.max(arr, initial = 0, where = (arr%2==1)) # max of numbers less than 30 result3 = np.max(arr, initial = 0,where = (arr < 30))
print('Max of entire array:', result1) print('Max of only odd elements:', result2) print('Max of numbers less than 30:', result3)

Output

Max of entire array: 50
Max of only odd elements: 47
Max of numbers less than 30: 25