NumPy nanmax() Function



The NumPy nanmax() function computes the maximum value of an array, ignoring any NaN (Not a Number) values. If all values in the array or along the specified axis are NaN, a RuntimeWarning is raised and the result will be NaN.

When positive infinity (inf) and negative infinity (-inf) are present in the array, the nanmax() behaves similarly by ignoring NaN values. However, it still considers inf as the largest possible value if present, as long as the array has no NaN elements that mask the calculation.

The nanmax() function returns a float data type when any element in the input array is a float, even if all values are integers. This ensures compatibility with arrays that may contain NaN, infinities, or fractional numbers. If the input array contains only integers and no NaN values, the return type matches the input's data type.

Following is the syntax of the NumPy nanmax() function −

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

Parameters

Following are the parameters of the NumPy nanmax() function −

  • a: Input array. The array can be of any shape or data type and may include NaN values.
  • axis (optional): Axis along which to compute the maximum. If None, the maximum is computed over the flattened array.
  • out (optional): Alternate output array to place the result. It must have the same shape as the expected output.
  • keepdims (optional): If True, the reduced dimensions are retained as dimensions of size one in the output. Default is False.
  • initial (optional): Initial value to start the comparison. If not provided, the default is the minimum possible value for the data type.
  • where (optional): A Boolean array. If True, include the corresponding element in the computation; otherwise, ignore it.

Return Values

This function returns a scalar value or a NumPy array containing the maximum values along the specified axis, ignoring NaN values.

Example

Following is a basic example of finding the maximum value in an array using the NumPy nanmax() function, while ignoring NaN values −

import numpy as np  
# input array with NaN values  
array = np.array([3, np.nan, 1, 7, 9])  
# finding the maximum value, ignoring NaN  
max_value = np.nanmax(array)  
print("Maximum Value (ignoring NaN):", max_value)  

Output

Following is the output of the above code −

Maximum Value (ignoring NaN): 9.0  

Example: Maximum Along an Axis

The nanmax() function can find the maximum values along a specified axis of a multi-dimensional array, while ignoring NaN values. In the following example, we have computed the maximum values along rows and columns −

import numpy as np  
# 2D input array with NaN values  
array = np.array([[3, 7, np.nan], [8, np.nan, 2], [6, 1, 9]])  
# maximum along rows (axis=1)  
max_along_rows = np.nanmax(array, axis=1)  
print("Maximum along rows:", max_along_rows)  
# maximum along columns (axis=0)  
max_along_columns = np.nanmax(array, axis=0)  
print("Maximum along columns:", max_along_columns)  

Output

Following is the output of the above code −

Maximum along rows: [7. 8. 9.]  
Maximum along columns: [8. 7. 9.]  

Example: Maximum value with 'keepdims'

The keepdims parameter retains the reduced dimension as a size-one dimension in the output. This means that when we pass a multi-dimensional array and set this parameter to True, the reduced dimension's size is kept as 1, preserving the original dimensionality of the array. In the following example, we have demonstrated its use −

import numpy as np  
# 2D input array with NaN values  
array = np.array([[3, np.nan, 5], [8, 4, np.nan], [6, 1, 9]])  
# maximum along columns with keepdims=True  
max_with_keepdims = np.nanmax(array, axis=0, keepdims=True)  
print("Maximum with keepdims:\n", max_with_keepdims)  

Output

Following is the output of the above code −

Maximum with keepdims:
 [[8. 4. 9.]]  

Example: Maximum Value with 'where' Condition

The where parameter allows computation of the maximum value based on a condition. In the following example, we have computed the maximum element greater than 5, while ignoring NaN values −

import numpy as np  
# input array with NaN values  
array = np.array([3, 5, np.nan, 7, 9])  
max_without_condition = np.nanmax(array)  
print("Maximum without condition (ignoring NaN):", max_without_condition)  

# where condition (only include values greater than 5)  
max_with_condition = np.nanmax(array, where=array > 5, initial=0)  
print("Maximum with condition:", max_with_condition)  

Output

Following is the output of the above code −

Maximum without condition (ignoring NaN): 9.0  
Maximum with condition: 9.0  

Example: Graphical Representation of 'nanmax()'

In the following example, we have visualized the maximum value along rows and columns of a 2D array with NaN values. To achieve this, we need to import the numpy and matplotlib.pyplot modules −

import numpy as np  
import matplotlib.pyplot as plt  

# 2D input array with NaN values  
array = np.array([[3, np.nan, 5], [8, 4, np.nan], [6, 1, 9]])  
# maximum along rows  
max_rows = np.nanmax(array, axis=1)  
# maximum along columns  
max_columns = np.nanmax(array, axis=0)  

plt.plot(range(len(max_rows)), max_rows, label="Maximum along rows (ignoring NaN)")  
plt.plot(range(len(max_columns)), max_columns, label="Maximum along columns (ignoring NaN)")  
plt.title("Visualization of nanmax() Results")  
plt.xlabel("Index")  
plt.ylabel("Maximum Value")  
plt.legend()  
plt.grid()  
plt.show()  

Output

The plot visualizes the maximum values along rows and columns of the array, ignoring NaN values −

nanmax Visualization
numpy_statistical_functions.htm
Advertisements