What is numpy.nanmin() in Python?

Python’s numpy.nanmin() computes the minimum of an array or the minimum of an array along a specified axis, ignoring NaN values.

Syntax

numpy.nanmin(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.nanmin() takes the following non-optional parameter:

  • a [array-like]: input array.

numpy.nanmin() takes the following optional parameters:

  • axis [None, int, tuples of int]: axis along which we want the minimum 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 nanmin() method of sub-classes of ndarray. In the case of default value, this will not be done.

Return value

numpy.nanmin() returns the minimum of an array with the same shape as a. Hence, the return type is scalar or ndarray depending on the input.

  • If the nanmin() function encounters all NaN slices, a RuntimeWarning is raised.

  • NaN (Not a Number) is not equivalent to positive or negative infinity.

  • If the input array consists of only integer values, the numpy.nanmin() function is equivalent to numpy.min() function in Python.

Examples

The following example outputs the minimum value of the arr array where the axis parameter is not specified.

import numpy as np
arr = np.array([np.nan,0,10,100])
print (np.nanmin(arr))

The following example outputs the minimum of the arr2 array that contains infinity values.

import numpy as np
arr2 = np.array([np.inf, 50, np.nan, 100, -np.inf])
print (np.nanmin(arr2))

The following example outputs the minimum of the array arr1 and arr2 where axis is specified as 0 and 1.

import numpy as np
arr1 = np.array([[2,4,np.inf], [np.nan,9,10]])
#Minimum values along the first axis
print (np.nanmin(arr1, axis = 0))
#Minimum values along the second axis
print (np.nanmin(arr1, axis = 1))

Free Resources