NumPy Nanmin: A Comprehensive Guide
When analyzing data, it is common to encounter missing values denoted by NaN (Not a Number) or infinity. This makes it impossible to perform operations like minimum or maximum values, arithmetic operations, or statistical analysis.
In such cases, NumPy nanmin comes to the rescue. NumPy nanmin is a function that computes the minimum value in an array, ignoring NaN values.
This article will cover the basics of NumPy nanmin, its syntax, returns, and examples to help you understand how it works.
What is NumPy Nanmin?
NumPy nanmin is a function that returns the minimum value of an array or a specific axis along which to operate, ignoring NaN values. The function is part of the numpy module, a Python package used for numerical computations.
Syntax of NumPy Nanmin
The syntax for numpy.nanmin() is as follows:
numpy.nanmin(arr, axis=None, out=None, keepdims=, **kwargs)
Parameter Description
arr
: This is the input array.axis
: This is an optional parameter that specifies the axis along which to operate.out
: This is an optional parameter that specifies the output array.keepdims
: This is an optional parameter that specifies whether to keep dimensions or not.
Returns of NumPy Nanmin
The function returns the minimum value of the array, ignoring NaN values.
Examples of NumPy Nanmin
NumPy Nanmin of a 1D array
Suppose you have a 1D array that contains NaN values. You can use numpy.nanmin() to find the minimum value of the array, ignoring NaN values.
import numpy as np
arr_1d = np.array([1, np.nan, 2, 3, np.nan, 4, 5])
min_val = np.nanmin(arr_1d)
print("Minimum value of the array:", min_val)
Output:
Minimum value of the array: 1.0
NumPy Nanmin of a 2D array
Suppose you have a 2D array that contains NaN values. You can use np.nanmin() to find the minimum value of the array, ignoring NaN values.
arr_2d = np.array([[1, 2, 3],
[4, np.nan, 6],
[7, 8, np.nan]])
min_val = np.nanmin(arr_2d)
print("Minimum value of the array:", min_val)
Output:
Minimum value of the array: 1.0
NumPy Nanmin along an axis of the array
You can also use np.nanmin() with the axis parameter to find the minimum value along a specific axis.
Axis=0
When axis=0, np.nanmin() finds the minimum value for each column, ignoring NaN values.
arr_2d = np.array([[1, 2, 3],
[4, np.nan, 6],
[7, 8, np.nan]])
min_val = np.nanmin(arr_2d, axis=0)
print("Minimum value along axis=0:", min_val)
Output:
Minimum value along axis=0: [1. 2. 3.]
Axis=1
When axis=1, np.nanmin() finds the minimum value for each row, ignoring NaN values.
arr_2d = np.array([[1, 2, 3],
[4, np.nan, 6],
[7, 8, np.nan]])
min_val = np.nanmin(arr_2d, axis=1)
print("Minimum value along axis=1:", min_val)
Output:
Minimum value along axis=1: [1. 4. 7.]
NumPy Nanmin of an array containing infinity
Suppose you have an array that contains infinity. You can use np.nanmin() to find the minimum value of the array, ignoring infinity.
inf_arr = np.array([1, 2, np.inf, 3, np.nan, 4, 5])
min_val = np.nanmin(inf_arr)
print("Minimum value of the array:", min_val)
Output:
Minimum value of the array: 1.0
Conclusion
In conclusion, NumPy nanmin is a useful function that returns the minimum value in an array, ignoring NaN and infinite values. By calculating the minimum value for a given array, it provides valuable insights about the data.
With the examples explained above, it is now easier to compute the minimum value of an array in Python. NumPy nanmin is a powerful tool for data analysis that can help overcome the challenges of working with missing values such as NaN values or infinity.