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

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

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

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.