Compare two Numpy arrays and return the element-wise minimum with fmin()


To compare two arrays and return the element-wise minimum, use the numpy.fmin() method in Python Numpy. Return value is either True or False.

Compare two arrays and returns a new array containing the element-wise maxima. If one of the elements being compared is a NaN, then the non-nan element is returned. If both elements are NaNs then the first is returned. The latter distinction is important for complex NaNs, which are defined as at least one of the real or imaginary parts being a NaN. The net effect is that NaNs are ignored when possible.

NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.

Steps

At first, import the required library −

import numpy as np

Creating two 2D numpy array using the array() method. We have inserted elements −

arr1 = np.array([[17, 9, 13],[25, 11, 21]])
arr2 = np.array([[15, 12, 18],[22, 19, 26]])

Display the arrays −

print("Array 1...
", arr1) print("
Array 2...
", arr2)

Get the type of the arrays −

print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype)

Get the dimensions of the Arrays −

print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim)

Get the shape of the Arrays −

print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape)

To compare two arrays and return the element-wise minimum, use the numpy.fmin() method in Python Numpy. Return value is either True or False −

print("
Result (minimum)...
",np.fmin(arr1, arr2))

Example

import numpy as np

# Creating two 2D numpy array using the array() method
# We have inserted elements
arr1 = np.array([[17, 9, 13],[25, 11, 21]])
arr2 = np.array([[15, 12, 18],[22, 19, 26]])

# Display the arrays
print("Array 1...
", arr1) print("
Array 2...
", arr2) # Get the type of the arrays print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype) # Get the dimensions of the Arrays print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim) # Get the shape of the Arrays print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape) # To compare two arrays and return the element-wise minimum, use the numpy.fmin() method in Python Numpy # Return value is either True or False print("
Result (minimum)...
",np.fmin(arr1, arr2))

Output

Array 1...
[[17 9 13]
[25 11 21]]

Array 2...
[[15 12 18]
[22 19 26]]

Our Array 1 type...
int64

Our Array 2 type...
int64

Our Array 1 Dimensions...
2

Our Array 2 Dimensions...
2

Our Array 1 Shape...
(2, 3)

Our Array 2 Shape...
(2, 3)

Result (minimum)...
[[15 9 13]
[22 11 21]]

Updated on: 08-Feb-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements