NUMPY BASIC EXERCISE 11


Checks two array whether they are equal and equal in tolerance. 

In this exercise, we are going to compare two arrays and check two cases of equality and equality with tolerance.

Code

import numpy as np
arr1 = np.array([10,20,30,40,50,-40,-10, 200])
arr2 = np.array([10,20,30,40,50,-40,-10, 200.002])
print("Initial Arrays")
print(arr1)
print(arr2)
print("Checks the equality")
print(np.equal(arr1, arr2))
print("Checks thw equality within a tolerance:")
print(np.allclose(arr1, arr2))

Output

Initial Arrays
[ 10  20  30  40  50 -40 -10 200]
[ 10.     20.     30.     40.     50.    -40.    -10.    200.002]
Checks the equality
[ True  True  True  True  True  True  True False]
Checks thw equality within a tolerance:
True

Code Editor

import numpy as np arr1 = np.array([10,20,30,40,50,-40,-10, 200]) arr2 = np.array([10,20,30,40,50,-40,-10, 200.002]) print("Initial Arrays") print(arr1) print(arr2) print("Checks the equality") print(np.equal(arr1, arr2)) print("Checks thw equality within a tolerance:") print(np.allclose(arr1, arr2))