Compare two NumPy Arrays element-wise in Python

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:

Advertisements
  1. Import NumPy library.
  2. Create two numpy arrays of equal length.
  3. apply the == operator on both the arrays, i.e, arr1 ==arr2. It will return a bool array.
  4. 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:

[ 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:

  1. Import NumPy library.
  2. Create two numpy arrays of equal length.
  3. Iterate over array and compare elements
  4. 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:

  1. Import NumPy library.
  2. Create two numpy arrays of equal length.
  3. Iterate over array using for loop and compare elements
  4. 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:

  1. Import NumPy library.
  2. Create two numpy arrays of equal length.
  3. Use list comprehension to compare the elements.
  4. 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:

  1. Import NumPy library.
  2. Create two numpy arrays of equal length.
  3. Iterate over array using while loop and compare elements.
  4. 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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top