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]
