NumPy fmin()
The numpy.fmin()
function computes the element-wise minimum of two input arrays, ignoring NaNs when possible. If one of the elements is NaN, it returns the non-NaN element. If both elements are NaNs, the first NaN is returned.
Syntax
</>
Copy
numpy.fmin(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x1, x2 | array_like | Input arrays to compare. They must be broadcastable to a common shape. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array to store the result. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior. |
order | str, optional | Specifies the memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved. |
Return Value
Returns an array with the element-wise minimum values of x1
and x2
. If both inputs are scalars, a scalar is returned.
Examples
1. Element-wise Minimum of Two Arrays
Computing the element-wise minimum values between two arrays.
</>
Copy
import numpy as np
# Define two arrays
x1 = np.array([2, 5, 8, 3])
x2 = np.array([4, 3, 7, 6])
# Compute the element-wise minimum
result = np.fmin(x1, x2)
# Print the results
print("Array 1:", x1)
print("Array 2:", x2)
print("Element-wise minimum:", result)
Output:
Array 1: [2 5 8 3]
Array 2: [4 3 7 6]
Element-wise minimum: [2 3 7 3]

2. Handling NaN Values with fmin()
numpy.fmin()
ignores NaN values and returns the non-NaN element if available.
</>
Copy
import numpy as np
# Define arrays containing NaN values
x1 = np.array([3, np.nan, 5, np.nan])
x2 = np.array([np.nan, 4, np.nan, 2])
# Compute element-wise minimum, handling NaNs
result = np.fmin(x1, x2)
# Print the results
print("Array 1:", x1)
print("Array 2:", x2)
print("Element-wise minimum ignoring NaNs:", result)
Output:
Array 1: [ 3. nan 5. nan]
Array 2: [nan 4. nan 2.]
Element-wise minimum ignoring NaNs: [ 3. 4. 5. 2.]

3. Using the out
Parameter
Storing the result in an output array instead of creating a new one.
</>
Copy
import numpy as np
# Define two input arrays
x1 = np.array([1, 7, 9, 4])
x2 = np.array([2, 5, 6, 8])
# Create an output array
out_array = np.empty_like(x1)
# Compute the minimum and store it in out_array
np.fmin(x1, x2, out=out_array)
# Print the results
print("Element-wise minimum stored in output array:", out_array)
Output:
Element-wise minimum stored in output array: [1 5 6 4]

4. Using the where
Parameter
Computing the element-wise minimum only for selected elements based on a condition.
</>
Copy
import numpy as np
# Define two input arrays
x1 = np.array([5, 9, 3, 6])
x2 = np.array([7, 2, 8, 4])
# Define a condition mask
mask = np.array([True, False, True, False])
# Compute element-wise minimum where mask is True
result = np.fmin(x1, x2, where=mask)
# Print the results
print("Computed minimum with condition:", result)
Output:
Computed minimum with condition: [5 0 3 0]

The minimum values are computed only for elements where mask=True
. The other elements remain unchanged.