Numpy Array Equal Method : How to check if numpy arrays are equal

Numpy Array Equal Method
Numpy Array Equal Method

Sometimes we have two or more NumPy arrays and want to validate the equality between them. To do so there is a method in NumPy and that is numpy.array_equal(). In this entire tutorial, I will show you the implementation of this method to check NumPy array is equal or not. All things will be done step by step.

Steps to Check Numpy Array Equal or Not

Step 1: Import the library

Here in this entire post, I am using only NumPy module. So let’s import it.

import numpy as np

Step 2: Create a Sample Numpy array

I want to check whether a NumPy array is equal or not. To do so you have let’s create three NumPy arrays and then implement the method. Two arrays will be equal and one is different.

Array 1

array_1 = np.array([1,2,3,4,5,6])

Output

array([1, 2, 3, 4, 5, 6])

Array 2

array_2 = np.array([1,2,3,4,5,6])

Output

array([1, 2, 3, 4, 5, 6])

Array 3

array_3 = np.array([1,2,3])

Output

array([1, 2, 3])

Step 3: Check the Numpy Array is Equal or Not

After the above two steps let’s check the equality of the NumPy array. I will show you all the methods to do so. Let me see it.

Method 1: Check equality of Numpy array elementwise

You can check whether a single element in the array is equal or not using the logical == operator. For example, If I check array_1 and array_2 then all the output will be True, and if array_1 and array_3 then you will get a single output False if it doesn’t match.

Execute the below lines of code.

array_1 == array_2

Output

Check equality of the array using logical == operator
Check equality of the array using logical == operator
array_1 == array_3

Output

False

Method 2: Check equality of Numpy array using numpy.array_equal function

The other method to check Numpy Array is Equal or not is using the numpy.array() method. Here you have to just pass the two arrays as an argument to get the output.

Comparison of Array 1 and Array 2

Let’s compare array_1d and array_2d and see the output.

np.array_equal(array_1,array_2)

Output

Check equality of the array using Numpy Array Equal Method
Check equality of the array using Numpy Array Equal Method

Comparison of Array 1 and Array 3

In the same way, If I will compare array_1 and array_3 then you will get the output as False.

np.array_equal(array_1,array_3)

Output

Check inequality of the array using Numpy Array Equal Method
Check inequality of the array using Numpy Array Equal Method

Conclusion

Numpy array_equal() is a mostly used function to validate any duplicacy of the array. There are two methods I have compiled for you. If you will ask me which method to use then I will suggest choosing the second method.

Hope you have liked this article. If you have any query on it then you can contact us for more information.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner