NumPy minimum()

The minimum() function is used to find the minimum value between the corresponding elements of two arrays.

import numpy as np

array1 = np.array([1, 2, 3, 4, 5])  
array2 = np.array([2, 4, 1, 5, 3])  

# find the element-wise minimum of array1 and array2 result = np.minimum(array1, array2)
print(result) # Output: [1 2 1 4 3]

minimum() Syntax

The syntax of minimum() is:

numpy.minimum(array1, array2, out = None)

minimum() Arguments

The minimum() function takes following arguments:

  • array1 and array2 - two input arrays to be compared
  • out (optional) - the output array where the result will be stored

minimum() Return Value

The minimum() function returns an array containing the minimum value between the corresponding elements of two arrays.


Example 1: minimum() With 2-D Array

import numpy as np

# create two 2-D arrays
array1 = np.array([[1, 2, 3], [4, 5, 6]])  
array2 = np.array([[2, 4, 1], [5, 3, 2]])  

# find the element-wise minimum of array1 and array2 result = np.minimum(array1, array2)
print(result)

Output

[[1 2 1]
 [4 3 2]]

Here, minimum() compares the corresponding elements of array1 and array2 and returns a new array result with the minimum value at each position.


Example 2: Use of out Argument in minimum()

import numpy as np

array1 = np.array([1, 3, 5, 7, 9])
array2 = np.array([2, 4, 6, 8, 10])

# create an empty array with the same shape as array1
output = np.empty_like(array1)  

# compute the element-wise minimum and store the result in output np.minimum(array1, array2, out=output)
print(output)

Output

[1 3 5 7 9]

Here, after specifying out=output, the result of the element-wise minimum operation is stored in the output array.