Kodeclik Logo

Our Programs

Learn More

Schedule

Kodeclik Blog

How to check if two numpy arrays are equal

When working with Numpy arrays, we will sometimes feel the need to compare them and check if they are equal, i.e., if they are equal when comparisons are done element-wise. The numpy.equal() function takes two numpy arrays as input and does precisely this - after doing an element-wise comparison returning an output boolean array of results.

Python numpy.equal()

Example of numpy.equal() in action

Here is a simple program to explore this:

import numpy as np

array1 = np.array([1,2])
array2 = np.array([1,3])
print(np.equal(array1,array2))

In this program, we create two arrays with elements [1,2] and [1,3] respectively and then use numpy.equal() on these arrays. The output is:

[ True False]

revealing that the (respective) first elements are equal but the second elements are not.

Using numpy.equal() with arrays of different sizes

If we try this:

import numpy as np

array1 = np.array([1,2,3])
array2 = np.array([1,3])
print(np.equal(array1,array2))

we will get this error:

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    print(np.equal(array1,array2))
ValueError: operands could not be broadcast together with shapes (3,) (2,)

meaning that the two arrays are not broadcastable to a common shape (which becomes the shape of the output).

Using numpy.equal() with two dimensional arrays

Here we will use numpy.arange() to first generate values in a certain interval and then numpy.reshape() to organize them into a two dimensional array:

import numpy as np

array1 = np.arange(8).reshape(4,2)
array2 = np.arange(1,9).reshape(4,2)
print(array1)
print(array2)
print(np.equal(array1,array2))

The first array will have values from 0 to 8 and the second will have values from 1 to 9. As a result even though they are of the same size (4 rows and 2 columns), they will not have element-wise the same values, so np.equal() will return all False’s. The output is thus:

[[0 1]
 [2 3]
 [4 5]
 [6 7]]
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
[[False False]
 [False False]
 [False False]
 [False False]]

Using numpy.equal() with 2D arrays having the same values but different sizes

In this example, we will create two arrays with the same values but reshape them into different sizes. As a result numpy.equall() will fail.

import numpy as np

array1 = np.arange(8).reshape(4,2)
array2 = np.arange(8).reshape(2,4)
print(array1)
print(array2)
print(np.equal(array1,array2))

The output is:

[[0 1]
 [2 3]
 [4 5]
 [6 7]]
[[0 1 2 3]
 [4 5 6 7]]
Traceback (most recent call last):
  File "main.py", line 7, in <module>
    print(np.equal(array1,array2))
ValueError: operands could not be broadcast together with shapes (4,2) (2,4)

Shorthand for numpy.equal is ==

We can use the == operator to do the same operations we have been doing thus far.

Thus, the following program:

import numpy as np

array1 = np.arange(8).reshape(4,2)
array2 = np.arange(8).reshape(4,2)
print(array1 == array2)

returns:

[[ True  True]
 [ True  True]
 [ True  True]
 [ True  True]]

To summarize, checking for equality between two numpy arrays is really easy, either with numpy.equal() or the == operator. The array sizes have to match up exactly, in order to allow an element-wise comparison. An aggregate True or False is not returned; instead an element-wise True or False is returned (which we can then aggregate if needed).

For more understanding of numpy arrays, checkout our blogpost on creating numpy arrays from lists.

For more Python content, checkout the math.ceil() and math.floor() functions! Also
learn about the math domain error in Python and how to fix it!

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.