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

ParameterTypeDescription
x1, x2array_likeArrays containing elements to be compared. If shapes differ, they must be broadcastable.
outndarray, None, or tuple of ndarray and None, optionalOptional output array to store the result. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying where to compute the minimum. Elsewhere, the original value is retained.
castingstr, optionalDefines the casting behavior when computing the minimum function.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines 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]