NumPy fmin – Element-wise minimum of array elements

NumPy Fmin Cover Image

Hello and welcome to this tutorial on Numpy fmin. In this tutorial, we will be learning about the NumPy fmin() method and also seeing a lot of examples regarding the same. So let us begin!

Also read: NumPy fmax – Element-wise maximum of array elements


What is NumPy fmin?

fmin() is a function in NumPy that compares two arrays and returns an array that contains the element-wise minimum of these two arrays.


Syntax of NumPy fmin

Let us have a look at the syntax of the fmin() function.

numpy.fmin(x1, x2, out=None)
ParameterDescriptionRequired/Optional
x1Input array 1.Required
x2Input array 2.Required
outAn alternative output array in which to place the result. It must have the same shape as the expected output.Optional

Returns:
A new array containing the element-wise maximum of x1 and x2.

  • If x1 and x2 are both scalars, then the output is also a scalar.
  • If any of x1 or x2 contains a NaN value, then the output for that element-wise comparison is the non-NaN value.
  • If both elements in the comparison are NaNs, then NaN is returned as the minimum element.

Examples

Let’s now look at a few examples to understand the fmin() function better.

When both inputs are scalars

import numpy as np 

a = 2
b = 6
# using fmin function to calculate the element-wise minimum
ans = np.fmin(a, b)
print("a =", a, "\nb =", b)
print("Result =", ans)

Output:

a = 2 
b = 6
Result = 2

Since 2<6, 2 is the minimum element here.


Element-wise minimum of 1-d arrays

import numpy as np 

a = [5, 3, -5, 8, -2]
b = [1, 8, -2, 12, -13]
# using fmin function to calculate the element-wise minimum
ans = np.fmin(a, b)
print("a =", a, "\nb =", b)
print("Result =", ans)

Output:

a = [5, 3, -5, 8, -2] 
b = [1, 8, -2, 12, -13]
Result = [  1   3  -5   8 -13]

The resulting array is computed as:

ans[0]  = min(a[0], b[0]) = min(5, 1) = 1
ans[1]  = min(a[1], b[1]) = min(3, 8) = 3
ans[2]  = min(a[2], b[2]) = min(-5, -2) = -5
ans[3]  = min(a[3], b[3]) = min(8, 12) = 8
ans[4]  = min(a[4], b[4]) = min(-2, -13) = -13

Element-wise minimum of 2-d arrays

import numpy as np 

a = [[13, 8], [10, 7]]
b = [[5, 15], [30, 4]]
# using fmin function to calculate the element-wise minimum
ans = np.fmin(a, b)
print("a =", a, "\nb =", b)
print("Result =\n", ans)

Output:

a = [[13, 8], [10, 7]] 
b = [[5, 15], [30, 4]]
Result =
 [[ 5  8]
 [10  4]]

Here, both the input arrays are 2×2 arrays, hence the resulting array is also a 2×2 computed as:

ans[0][0] = min(a[0][0], b[0][0]) = min(13, 5) = 5
ans[0][1] = min(a[0][1], b[0][1]) = min(8, 15) = 8

ans[1][0] = min(a[1][0], b[1][0]) = min(10, 30) = 10
ans[1][1] = min(a[1][1], b[1][1]) = min(7, 4) = 4

Element-wise minimum of arrays containing NaNs

Let us now see how the numpy.fmin() method handles NaNs.

import numpy as np 

a = [4, 3, 10, np.nan, np.nan]
b = [2, np.nan, 5, 8, np.nan]
# using fmin function to calculate the element-wise minimum
ans = np.fmin(a, b)
print("a =", a, "\nb =", b)
print("Result =", ans)

Output:

a = [4, 3, 10, nan, nan] 
b = [2, nan, 5, 8, nan]
Result = [ 2.  3.  5.  8. nan]

Here,

ans[0]  = min(a[0], b[0]) = min(4, 2) = 2
ans[1]  = min(a[1], b[1]) = min(3, nan) = 3
ans[2]  = min(a[2], b[2]) = min(10, 5) = 5
ans[3]  = min(a[3], b[3]) = min(nan, 8) = 8
ans[4]  = min(a[4], b[4]) = min(nan, nan) = nan

In the above arrays, one of the elements at indices 1 and 3 is NaN, so the minimum is the non-nan value. Also, the element at index 4 in both the input arrays are NaN, so the resulting minimum value is also NaN as mentioned earlier in this tutorial.


Summary

That’s all! In this tutorial, we learned about the Numpy fmin method and practiced different types of examples using the same.  
If you want to learn more about NumPy, feel free to go through our NumPy tutorials.


Reference