w3resource
NumPy Exercises

NumPy: Create an element-wise comparison (greater, greater_equal, less and less_equal) of two given arrays

NumPy: Basic Exercise-10 with Solution

Write a NumPy program to create an element-wise comparison (greater, greater_equal, less and less_equal) of two given arrays.

Sample Solution :

Python Code :

import numpy as np
x = np.array([3, 5])
y = np.array([2, 5])
print("Original numbers:")
print(x)
print(y)
print("Comparison - greater")
print(np.greater(x, y))
print("Comparison - greater_equal")
print(np.greater_equal(x, y))
print("Comparison - less")
print(np.less(x, y))
print("Comparison - less_equal")
print(np.less_equal(x, y))

Sample Output:

Original numbers:
[3 5]
[2 5]
Comparison - greater
[ True False]
Comparison - greater_equal
[ True  True]
Comparison - less
[False False]
Comparison - less_equal
[False  True]                         

Python Code Editor:


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

Previous: Write a Bumpy program to test if two arrays are element-wise equal within a tolerance.
Next: Write a Bumpy program to create an element-wise comparison (equal, equal within a tolerance) of two given arrays.

What is the difficulty level of this exercise?