NumPy minimum()
The numpy.minimum()
function computes the element-wise minimum of two input arrays. If one of the elements being compared is NaN, that element is returned. If both elements are NaNs, the first one is returned.
Syntax
</>
Copy
numpy.minimum(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x1, x2 | array_like | Arrays containing elements to be compared. If shapes differ, they must be broadcastable. |
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 where to compute the minimum. Elsewhere, the original value is retained. |
casting | str, optional | Defines the casting behavior when computing the minimum function. |
order | str, optional | 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 in the output. |
Return Value
Returns an array or scalar containing the element-wise minimum values from x1
and x2
. If both inputs are scalars, a scalar is returned.
Examples
1. Element-wise Minimum of Two Arrays
Computing the minimum between two arrays element-wise.
</>
Copy
import numpy as np
# Define two input arrays
x1 = np.array([3, 7, 2, 9])
x2 = np.array([4, 5, 1, 10])
# Compute the element-wise minimum
result = np.minimum(x1, x2)
# Print the result
print("Element-wise minimum:", result)
Output:
Element-wise minimum: [3 5 1 9]

2. Handling NaN Values
NaN values are preserved when comparing elements.
</>
Copy
import numpy as np
# Define two arrays with NaN values
x1 = np.array([3, np.nan, 5])
x2 = np.array([4, 2, np.nan])
# Compute element-wise minimum
result = np.minimum(x1, x2)
# Print the result
print("Element-wise minimum with NaNs:", result)
Output:
Element-wise minimum with NaNs: [ 3. nan nan]

3. Using the out
Parameter
Storing results in an existing array instead of creating a new one.
</>
Copy
import numpy as np
# Define two input arrays
x1 = np.array([3, 8, 2])
x2 = np.array([4, 5, 1])
# Create an output array with the same shape
output_array = np.empty_like(x1)
# Compute element-wise minimum and store in output_array
np.minimum(x1, x2, out=output_array)
# Print the results
print("Stored in output array:", output_array)
Output:
Stored in output array: [3 5 1]

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

The element-wise minimum is computed only where mask=True
.