﻿ NumPy: Create an element-wise comparison (equal, equal within a tolerance) of two given arrays - w3resource

# NumPy: Create an element-wise comparison (equal, equal within a tolerance) of two given arrays

## NumPy: Basic Exercise-11 with Solution

Write a NumPy program to create an element-wise comparison (equal, equal within a tolerance) of two given arrays.

Sample Solution :

Python Code :

``````import numpy as np
x = np.array([72, 79, 85, 90, 150, -135, 120, -10, 60, 100])
y = np.array([72, 79, 85, 90, 150, -135, 120, -10, 60, 100.000001])
print("Original numbers:")
print(x)
print(y)
print("Comparison - equal:")
print(np.equal(x, y))
print("Comparison - equal within a tolerance:")
print(np.allclose(x, y))
``````

Sample Output:

```Original numbers:
[  72   79   85   90  150 -135  120  -10   60  100]
[  72.         79.         85.         90.        150.       -135.
120.        -10.         60.        100.000001]
Comparison - equal:
[ True  True  True  True  True  True  True  True  True False]
Comparison - equal within a tolerance:
True
```

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

﻿

Inviting useful, relevant, well-written and unique guest posts