What is numpy.nanmax() in Python?
Python’s numpy.nanmax() computes the maximum of an array or the maximum of an array along a specified axis, ignoring
Syntax
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.
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 array.flattened input converted from multi-dimensional to a one-dimensional array. -
out[ndarray]: Represents the location into which the output is stored. -
keepdims[boolean]: Atruevalue 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,
keepdimswill be passed through to thenanmax()method of sub-classes ofndarray.In the case of the default value, this will not be done.
Return value
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 allNaNslices, aRuntimeWarningis raised. -
NaNis not equivalent to positive or negative infinity. -
If the input array consists of only integer values, the
numpy.nanmax()function is equivalent to thenumpy.max()function in Python.
Code
Example 1
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))
Example 2
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))
Example 3
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))