
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy nanmin() Function
The NumPy nanmin() function computes the minimum value of an array, ignoring any NaN (Not a Number) values. If all values in the array or along the specified axis are NaN, it encountered a RuntimeWarning is raised and the result will be NaN.
When the positive infinity(inf) and negative infinity(-inf) is present in an array the nanmin() behaves similarly but ignores NaN values. However, it still considers -inf as the smallest possible value if present, as long as the array has no NaN elements that mask the calculation.
The nanmin() function returns a float data type when any element in the input array is a float, even if all values are integers. This ensures compatibility with arrays that may contain NaN, infinities, or fractional numbers. If the input array contains only integers and no NaN values, the return type matches the input's data type.
Following is the syntax of the NumPy nanmin() function −
numpy.nanmin(a, axis=None, out=None, keepdims=False, initial=None, where=True)
Parameters
Following are the parameters of the NumPy nanmin() function −
- a: Input array. The array can be of any shape or data type and may include NaN values.
- axis (optional): Axis along which to compute the minimum. If None, the minimum is computed over the flattened array.
- out (optional): Alternate output array to place the result. It must have the same shape as the expected output.
- keepdims (optional): If True, the reduced dimensions are retained as dimensions of size one in the output. Default is False.
- initial (optional): Initial value to start the comparison. If not provided, the default is the maximum possible value for the data type.
- where (optional): A Boolean array. If True, include the corresponding element in the computation; otherwise, ignore it.
Return Values
This function returns a scalar value or a NumPy array containing the minimum values along the specified axis, ignoring NaN values.
Example
Following is a basic example of finding the minimum value in an array using the NumPy nanmin() function, while ignoring NaN values −
import numpy as np # input array with NaN values array = np.array([3, np.nan, 1, 7, 9]) # finding the minimum value, ignoring NaN min_value = np.nanmin(array) print("Minimum Value (ignoring NaN):", min_value)
Output
Following is the output of the above code −
Minimum Value (ignoring NaN): 1.0
Example: Minimum Along an Axis
The nanmin() function can find the minimum values along a specified axis of a multi-dimensional array, while ignoring NaN values. In the following example, we have computed the minimum values along rows and columns −
import numpy as np # 2D input array with NaN values array = np.array([[3, 7, np.nan], [8, np.nan, 2], [6, 1, 9]]) # minimum along rows (axis=1) min_along_rows = np.nanmin(array, axis=1) print("Minimum along rows:", min_along_rows) # minimum along columns (axis=0) min_along_columns = np.nanmin(array, axis=0) print("Minimum along columns:", min_along_columns)
Output
Following is the output of the above code −
Minimum along rows: [ 3. 2. 1.] Minimum along columns: [ 3. 1. 2.]
Example: Minimum value with 'keepdims'
The keepdims parameter retains the reduced dimension as a size-one dimension in the output. This means that when we pass a multi-dimensional array and set this parameter to True, the reduced dimension's size is kept as 1, preserving the original dimensionality of the array. In the following example, we have demonstrated its use −
import numpy as np # 2D input array with NaN values array = np.array([[3, np.nan, 5], [8, 4, np.nan], [6, 1, 9]]) # minimum along columns with keepdims=True min_with_keepdims = np.nanmin(array, axis=0, keepdims=True) print("Minimum with keepdims:\n", min_with_keepdims)
Output
Following is the output of the above code −
Minimum with keepdims: [[3. 1. 5.]]
Example: Minimum with Where Condition
The where parameter allows computation of the minimum value based on a condition. In the following example, we have computed the minimum element greater than 7, while ignoring NaN values −
import numpy as np # input array with NaN values array = np.array([3, 5, np.nan, 7, 9]) min_without_condition = np.nanmin(array) print("Minimum without condition (ignoring NaN):", min_without_condition) # where condition (only include values greater than 7) min_with_condition = np.nanmin(array, where=array > 7, initial=10) print("Minimum with condition:", min_with_condition)
Output
Following is the output of the above code −
Minimum without condition (ignoring NaN): 3.0 Minimum with condition: 9.0
Example: Graphical Representation of 'nanmin()'
In the following example, we have visualized the minimum value along rows and columns of a 2D array with NaN values. To achieve this, we need to import the numpy and matplotlib.pyplot modules −
import numpy as np import matplotlib.pyplot as plt # 2D input array with NaN values array = np.array([[3, np.nan, 5], [8, 4, np.nan], [6, 1, 9]]) # minimum along rows min_rows = np.nanmin(array, axis=1) # minimum along columns min_columns = np.nanmin(array, axis=0) plt.plot(range(len(min_rows)), min_rows, label="Minimum along rows (ignoring NaN)") plt.plot(range(len(min_columns)), min_columns, label="Minimum along columns (ignoring NaN)") plt.title("Visualization of nanmin() Results") plt.xlabel("Index") plt.ylabel("Minimum Value") plt.legend() plt.grid() plt.show()
Output
The plot visualizes the minimum values along rows and columns of the array, ignoring NaN values −
