In this article, we will learn to compare two NumPy Arrays element-wise using Python.
Table Of Contents
There are the multiple ways to compare two NumPy Arrays element-wise. Let’s discuss them one by one.
Compare two NumPy Arrays using == operator
When two numpy arrays are compared using == operator, it will return a boolean array. If any value in the boolean array is true, then the corresponding elements in the both the arrays are equal, otherwise not equal.
Approach:
- Import NumPy library.
- Create two numpy arrays of equal length.
- apply the
==
operator on both the arrays, i.e,arr1 ==arr2
. It will return a bool array. - Call the all() function on bool array. If it returns True, it means both arrays are equal, otherwise not.
Source Code
import numpy as np # creating two numpy arrays a = np.array([1, 2, 8, 7, 5]) b = np.array([1, 2, 3, 2, 5]) # comparing the arrays using == operator arr = a==b print(arr) if arr.all(): print('Both arrays are equal') else: print('Both Arrays are not equal')
Output:
Frequently Asked:
[ True True False False True] Both Arrays are not equal
The comparision can aslo be done with greater than (>) and less than (<) operators. The boolean array elements will contain true if the applied operator is true else false.
Code using > and < operator
import numpy as np # creating two numpy arrays a = np.array([1, 2, 8, 7, 5]) b = np.array([1, 2, 8, 7, 5]) # comparing the arrays using > operator print("comparing the arrays using > operator ", a > b) # comparing the arrays using < operator print("comparing the arrays using < operator ", a < b) if ( (~(a < b)).all() and (~(a > b)).all() ): print('Both arrays are equal') else: print('Both Arrays are not equal')
Output:
comparing the arrays using > operator [False False False False False] comparing the arrays using < operator [False False False False False] Both arrays are equal
Compare two NumPy Arrays using for loop and zip()
The zip() method takes multiple iterables as arguments and yeilds n-length tuple. Where n is the number of iterables passed to it. Now using for loop and zip() we will iterate over both the arrays and compare them element-wise.
Approach:
- Import NumPy library.
- Create two numpy arrays of equal length.
- Iterate over array and compare elements
- Print the boolean array.
Source Code
import numpy as np # Creating two numpy arrays a = np.array([1, 2, 8, 7, 5]) b = np.array([1, 2, 3, 4, 5]) # Comparing the arrays using == comparision = [] for i,j in zip(a,b): if i==j: comparision.append(True) else: comparision.append(False) print(comparision) if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal')
Output:
[True, True, False, False, True] Both Arrays are not equal
The comparision can aslo be done with greater than (>) and less than (<) operators. By replacing ==
with >
or <
operator.
Compare two NumPy Arrays using for loop
Iterate over the array and compare each element using ==, > or <
operators. For accesing the elements of both the arrays use indexing.
Approach:
- Import NumPy library.
- Create two numpy arrays of equal length.
- Iterate over array using for loop and compare elements
- print the boolean array.
Source Code
import numpy as np # creating two numpy arrays a = np.array([1, 2, 8, 7, 5]) b = np.array([1, 2, 3, 4, 5]) # comparing the arrays using == comparision = [] for i in range(np.size(a)): if a[i]==b[i]: comparision.append(True) else: comparision.append(False) print(" comparision using ==", comparision) if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal') # comparing the arrays using > comparision = [] for i in range(np.size(a)): if a[i] > b[i]: comparision.append(True) else: comparision.append(False) print(" comparision using >", comparision) if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal') # comparing the arrays using < comparision = [] for i in range(np.size(a)): if a[i] < b[i]: comparision.append(True) else: comparision.append(False) print(" comparision using <", comparision) if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal')
Output:
comparision using == [True, True, False, False, True] Both Arrays are not equal comparision using > [False, False, True, True, False] Both Arrays are not equal comparision using < [False, False, False, False, False] Both Arrays are not equal
Compare two NumPy Arrays using List Comprehension
Using list comprehension, iterate over the array and compare each element using ==, > or <
operator.
Approach:
- Import NumPy library.
- Create two numpy arrays of equal length.
- Use list comprehension to compare the elements.
- Print the boolean array.
Source Code
import numpy as np # creating two numpy arrays a = np.array([1, 2, 8, 7, 5]) b = np.array([1, 2, 3, 4, 5]) # comparing the arrays using == comparision = [i==j for i,j in zip(a,b)] if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal') # comparing the arrays using > comparision = [i > j for i,j in zip(a,b)] if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal') # comparing the arrays using < comparision = [i < j for i,j in zip(a,b)] if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal')
Output:
Both Arrays are not equal Both Arrays are not equal Both Arrays are not equal
Compare two NumPy Arrays using while loop
Iterate over the array using while loop and compare each element using ==, > or <
operator. For accesing the elements of both the arrays use indexing.
Approach:
- Import NumPy library.
- Create two numpy arrays of equal length.
- Iterate over array using while loop and compare elements.
- print the boolean array.
Source Code
import numpy as np # creating two numpy arrays a = np.array([1, 2, 8, 7, 5]) b = np.array([1, 2, 3, 4, 5]) # comparing the arrays using == comparision = [] i = 0 while(i < np.size(a)): if a[i]==b[i]: comparision.append(True) else: comparision.append(False) i+=1 print(" comparision using ==", comparision) # comparing the arrays using > comparision = [] i = 0 while(i < np.size(a)): if a[i]==b[i]: comparision.append(True) else: comparision.append(False) i+=1 print(" comparision using >", comparision) # comparing the arrays using < comparision = [] i = 0 while(i < np.size(a)): if a[i]==b[i]: comparision.append(True) else: comparision.append(False) i+=1 print(" comparision using <", comparision)
Output
comparision using == [True, True, False, False, True] comparision using > [True, True, False, False, True] comparision using < [True, True, False, False, True]
Summary
Great! you made it, We have discussed All possible methods to compare two NumPy Arrays element-wise using Python. Happy learning.