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 NaNNot a Number values.

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 flattenedinput converted from multi-dimensional to a one-dimensional array. array.

  • 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.

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.

  1. If the nanmax() function encounters all NaN slices, a RuntimeWarning is raised.

  2. NaN is not equivalent to positive or negative infinity.

  3. If the input array consists of only integer values, the numpy.nanmax() function is equivalent to the numpy.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 np
arr = 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 np
arr2 = 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 np
arr1 = np.array([[2,4,np.inf], [np.nan,9,10]])
#Maximum values along the first axis
print (np.nanmax(arr1, axis = 0))
#Maximum values along the second axis
print (np.nanmax(arr1, axis = 1))

Free Resources