w3resource
NumPy Exercises

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.greater(x, y))
print("Comparison - equal within a tolerance:")
print(np.greater_equal(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:
[False False False False False False False False False False]
Comparison - equal within a tolerance:
[ True  True  True  True  True  True  True  True  True False]                         

Python Code Editor:


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

Previous: Write a NumPy program to create an element-wise comparison (greater, greater_equal, less and less_equal) of two given arrays.
Next: Write a NumPy program to create an array with the values 1, 7, 13, 105 and determine the size of the memory occupied by the array.

What is the difficulty level of this exercise?