NumPy nanmax()

The numpy.nanmax() function computes the maximum of an array or along a specified axis while ignoring NaN (Not a Number) values. If all values in a slice are NaN, a RuntimeWarning is raised, and NaN is returned for that slice.

Syntax

</>
Copy
numpy.nanmax(a, axis=None, out=None, keepdims=False, initial=None, where=True)

Parameters

ParameterTypeDescription
aarray_likeInput array containing numbers. If not an array, it is converted.
axisint, tuple of int, or None, optionalAxis along which the maximum is computed. Default is to compute the maximum of the entire array.
outndarray, optionalOptional output array where the result is stored. Must have the same shape as the expected output.
keepdimsbool, optionalIf True, retains reduced dimensions with size one for broadcasting.
initialscalar, optionalSpecifies the minimum value of an output element. Required when computing on an empty slice.
wherearray_like of bool, optionalElements to consider when computing the maximum. Newly added in NumPy 1.22.0.

Return Value

Returns an array with the maximum values, with the specified axis removed. If the input is a scalar, a scalar is returned.


Examples

1. Computing Maximum While Ignoring NaN

In this example, we compute the maximum of an array containing NaN values.

</>
Copy
import numpy as np

# Define an array with NaN values
arr = np.array([3, np.nan, 7, 1, np.nan, 5])

# Compute the maximum while ignoring NaN values
result = np.nanmax(arr)

# Print the result
print("Maximum value ignoring NaN:", result)

Output:

Maximum value ignoring NaN: 7.0

2. Computing Maximum Along an Axis

We compute the maximum along each column while ignoring NaN values.

</>
Copy
import numpy as np

# Define a 2D array with NaN values
arr = np.array([[3, np.nan, 5], 
                [7, 8, np.nan]])

# Compute the maximum along axis 0 (columns)
result = np.nanmax(arr, axis=0)

# Print the result
print("Maximum values along columns:", result)

Output:

Maximum values along columns: [7. 8. 5.]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array with NaN values
arr = np.array([1, np.nan, 4, 6, np.nan])

# Create an output array
output_array = np.empty_like(arr[0])

# Compute the maximum and store it in output_array
np.nanmax(arr, out=output_array)

# Print the result
print("Computed maximum stored in output:", output_array)

Output:

Computed maximum stored in output: 6.0

4. Using the where Parameter

The where parameter allows specifying which elements should be considered.

</>
Copy
import numpy as np

# Define an array with NaN values
arr = np.array([2, np.nan, 8, 5, np.nan])

# Define a condition (only consider specific indices)
mask = np.array([True, False, True, False, True])

# Compute the maximum considering only elements where mask is True
result = np.nanmax(arr, where=mask, initial=0)

# Print the result
print("Maximum value with mask:", result)

Output:

Maximum value with mask: 8.0

The maximum is computed only for the elements where mask=True, ignoring the rest.