Python’s numpy.nanmax()
computes the maximum of an array or the maximum of an array along a specified axis, ignoring
numpy.nanmax()
is declared as follows:
numpy.nanmax(a, axis=None, out=None, keepdims=<no value>)
In the syntax above, a
is the non-optional parameter, and the rest are optional parameters.
numpy.nanmax()
takes the following non-optional parameter:
a
[array-like]: input array.numpy.nanmax()
takes the following optional parameters:
axis
[None, int, tuples of int]: Axis along which we want the maximum value to be computed. The default is a
out
[ndarray]: Represents the location into which the output is stored.
keepdims
[boolean]: A true
value ensures that the reduced axes are left as dimensions with size one in the output.
This ensures that the output is broadcasted correctly against the input array. If a non-default value is passed, keepdims
will be passed through to the nanmax()
method of sub-classes of ndarray
.
In the case of the default value, this will not be done.
numpy.nanmax()
returns the maximum of an array with the same shape as a
. As a result, the return type is scalar or ndarray, depending on the input.
If the nanmax()
function encounters all NaN
slices, a RuntimeWarning
is raised.
NaN
is not equivalent to positive or negative infinity.
If the input array consists of only integer values, the numpy.nanmax()
function is equivalent to the numpy.max()
function in Python.
The following example outputs the maximum value of the array arr
, where the axis
parameter is not specified.
import numpy as nparr = np.array([np.nan,0,10,100])print (np.nanmax(arr))
The following example outputs the maximum of the array arr2
that contains infinity values.
import numpy as nparr2 = np.array([np.inf, 50, np.nan, 100, -np.inf])print (np.nanmax(arr2))
The following example outputs the maximum of the array arr1
, where axis
is specified as 0 and 1.
import numpy as nparr1 = np.array([[2,4,np.inf], [np.nan,9,10]])#Maximum values along the first axisprint (np.nanmax(arr1, axis = 0))#Maximum values along the second axisprint (np.nanmax(arr1, axis = 1))