w3resource

NumPy: Test two arrays are equal (element wise) or not

NumPy: Basic Exercise-44 with Solution

Write a NumPy program to check whether two arrays are equal (element wise) or not.

Sample Solution :

Python Code :

# Importing the NumPy library with an alias 'np'
import numpy as np

# Creating NumPy arrays 'nums1' and 'nums2' with floating-point values
nums1 = np.array([0.5, 1.5, 0.2])
nums2 = np.array([0.4999999999, 1.500000000, 0.2])

# Setting print options to display floating-point precision up to 15 decimal places
np.set_printoptions(precision=15)

# Printing a message indicating the original arrays 'nums1' and 'nums2'
print("Original arrays:")
print(nums1)
print(nums2)

# Printing a message asking whether the two arrays are equal element-wise or not
print("\nTest said two arrays are equal (element wise) or not:?")
print(nums1 == nums2)

# Reassigning new values to arrays 'nums1' and 'nums2'
nums1 = np.array([0.5, 1.5, 0.23])
nums2 = np.array([0.4999999999, 1.5000000001, 0.23])

# Printing a message indicating the original arrays 'nums1' and 'nums2'
print("\nOriginal arrays:")
np.set_printoptions(precision=15)
print(nums1)
print(nums2)

# Printing a message asking whether the two arrays are equal element-wise or not
print("\nTest said two arrays are equal (element wise) or not:?")
print(np.equal(nums1, nums2)) 

Sample Output:

Original arrays:
[0.5 1.5 0.2]
[0.4999999999 1.5          0.2         ]

Test said two arrays are equal (element wise) or not:?
[False  True  True]

Original arrays:
[0.5  1.5  0.23]
[0.4999999999 1.5000000001 0.23        ]

Test said two arrays are equal (element wise) or not:?
[False False  True]

Explanation:

The above code shows how to compare two NumPy arrays element-wise for equality using both the == operator and the np.equal() function.

nums1 = np.array(...): This statement creates a NumPy array named 'nums1' containing three float values.

nums2 = np.array(...): This statement creates another NumPy array named 'nums2' containing three float values that are very close to those in 'nums1'.

np.set_printoptions(precision=15): This line sets the printing precision for NumPy arrays to 15 decimal places.

print(nums1 == nums2): This statement compares the two arrays 'nums1' and 'nums2' element-wise using the == operator and prints the resulting boolean array.

nums1 = np.array(...): This statement reassigns 'nums1' to a new NumPy array with slightly different float values.

nums2 = np.array(...): This statement reassigns 'nums2' to a new NumPy array with slightly different float values that are very close to those in 'nums1'.

print(np.equal(nums1, nums2)): This statement compares the two arrays 'nums1' and 'nums2' element-wise using the np.equal() function and prints the resulting boolean array.

Python-Numpy Code Editor:

Previous: NumPy program to find the missing data in a given array.
Next: NumPy program to create one-dimensional array of single, two and three digit numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.