Element wise comparison of two NumPy arrays in Python
Here in this tutorial, we will see how to element-wise compare two NumPy arrays in Python language. We will see all three fundamental comparisons that are equal to(==
), greater than(>
), and smaller than(<
) between each element of two one-dimensional NumPy arrays. Then we will obtain the output in the boolean form to see whether the comparison between each element is True or False.
NumPy – Numeric Python, the core library for scientific computing in Python. Fast mathematical and numerical functions and powerful multi-dimensional arrays.
Equal Elements:
In this example, we will check whether the elements of the two NumPy arrays are equal or not.
# import the numpy library import numpy as np # create two one-dimensional numpy arrays array1=np.array([1,2,3,4,5,6,7]) array2=np.array([1,2,4,3,5,6,4]) # compare whether the elements are equal or not print(array1==array2)
Output:
[ True True False False True True False]
True- The elements are equal
False- The elements are not equal
Greater Elements:
In this example, we will check whether the elements of array1 are greater than the elements of array2 or not.
# import the numpy library import numpy as np # create two one-dimensional numpy arrays array1=np.array([1,2,3,4,5,6,7]) array2=np.array([1,2,4,3,5,6,4]) # compare whether the elements of array1 are greater than array2 print(array1>array2)
Output:
[False False False True False False True]
True- The elements of array1 are greater than the elements of array2.
False- The elements of array1 are not greater than the elements of array2.
Smaller Elements:
In this example, we will check whether the elements of array1 are smaller than the elements of array2 or not.
# import the numpy library import numpy as np # create two one-dimensional numpy arrays array1=np.array([1,2,3,4,5,6,7]) array2=np.array([1,2,4,3,5,6,4]) # compare whether the elements of array1 are smaller than array2 print(array1<array2)
Output:
[False False True False False False False]
True- The elements of array1 are smaller than the elements of array2.
False- The elements of array1 are smaller than the elements of array2.
Leave a Reply