The Numpy library in Python comes with a number of useful built-in functions to work with arrays. In this tutorial, we will look at how to check if two Numpy arrays are equal or not with the help of some examples.
How to check for equality of two Numpy arrays?
We say two Numpy arrays are equal if the corresponding elements in both arrays are equal.
You can use the Numpy built-in array_equal()
function to check whether two arrays are equal or not. The following is the syntax –
import numpy as np # compare numpy arrays a1 and a2 for equality np.array_equal(a1, a2)
It returns True
if both arrays are equal and False
if the arrays are not equal.
There are other methods are well that you can use to check for equality of two Numpy arrays (see the examples below).
Examples
Let’s now look at examples of some of the methods that we can use to check the equality of two Numpy arrays.
First, let’s create some Numpy arrays that we will be using throughout this tutorial.
import numpy as np # create numpy arrays a = np.array([1, 2, 3]) b = np.array([1, 2, 3]) c = np.array([1, 2, 4]) # display the arrays print(f"a = {a}") print(f"b = {b}") print(f"c = {c}")
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
a = [1 2 3] b = [1 2 3] c = [1 2 4]
Here, we used the np.array()
function to create three Numpy arrays – a
, b
, and c
. You can see that arrays a
and b
are equal.
Example 1 – Using Numpy array_equal()
function
To compare two arrays for equality using the Numpy array_equal()
function, pass both the arrays as arguments to the function.
Let’s check if the arrays a
and b
are equal or not.
# check if arrays a and b are equal print(np.array_equal(a, b))
Output:
True
We get True
as the output which indicates that both arrays are equal.
Let’s check if arrays a
and c
are equal.
# check if arrays a and c are equal print(np.array_equal(a, c))
Output:
False
We get False
as the output since the arrays a
and c
are not equal.
For more on the Numpy array_equal()
function, refer to its documentation.
Example 2 – Using the ==
operator and the all()
function
You can also use a combination of the ==
operator and the Numpy all()
to check if the two arrays are equal or not.
First, let’s see what we get if we only use the equality operator, ==
to compare two Numpy arrays.
# compare arrays a and b with == print(a==b)
Output:
[ True True True]
We get a boolean array with each value representing whether the corresponding elements at that index are equal or not. Now, if all the values in this boolean array are True
we can say that both arrays are equal.
You can use the Numpy array all()
function to check whether all the values in a given array are True
or not. Let’s apply()
the all()
function on the result of the ==
operator.
# check if arrays a and b are equal print((a==b).all())
Output:
True
We get True
as the output. This means that the Numpy arrays a
and b
are equal.
Let’s now use this method to check whether arrays a
and c
are equal or not.
# check if arrays a and c are equal print((a==c).all())
Output:
False
We get False
as the output indicating the arrays a
and c
are not equal.
Note that using this method to compare two arrays of different lengths for equality will result in an error.
# create numpy arrays of different lengths a = np.array([1, 2, 3]) b = np.array([1, 2]) # check if arrays a and b are equal print((a==b).all())
Output:
/var/folders/s7/bqr5_87n7vs5c76jfk340cqw0000gn/T/ipykernel_82233/3831394073.py:5: DeprecationWarning: elementwise comparison failed; this will raise an error in the future. print((a==b).all()) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [38], in <module> 3 b = np.array([1, 2]) 4 # check if arrays a and b are equal ----> 5 print((a==b).all()) AttributeError: 'bool' object has no attribute 'all'
Here, we’re comparing two arrays of different lengths using the ==
operator which gives us a warning that “elementwise comparison failed” and results in False
. Then we’re applying the Numpy all()
function (which only works on Numpy arrays) to this boolean value which results in the above error.
Summary – Compare two Numpy arrays for equality
In this tutorial, we looked at how to check if two Numpy arrays are equal or not. The following are the key takeaways from this tutorial.
- Two Numpy arrays are considered equal if their corresponding elements are equal.
- Use the Numpy
array_equal()
function to check if two Numpy arrays are equal or not. - Alternatively, you can also use a combination of the
==
operator and theall()
function to compare two arrays from equality. Note that this method gives an error when comparing arrays of different lengths.
You might also be interested in –
- Different ways to Create NumPy Arrays
- Numpy – Remove Duplicates From Array
- Numpy – Print Array With Commas
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.