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 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 thenanmin()method of sub-classes ofndarray. 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 allNaNslices, aRuntimeWarningis 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 tonumpy.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 nparr = 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 nparr2 = 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 nparr1 = np.array([[2,4,np.inf], [np.nan,9,10]])#Minimum values along the first axisprint (np.nanmin(arr1, axis = 0))#Minimum values along the second axisprint (np.nanmin(arr1, axis = 1))