How to compare two arrays in Numpy?

Following is a material on how to compare two arrays using Numpy Python library.
Numpy compare two arrays array_equal

Comparing two arrays

Let’s start from the easiest example.

The most convenient way to compare two Numpy arrays is to use Numpy array_equal method which simply takes two arrays as parameters.

import numpy as np

my_array = np.array([1, 2, 3, 4])
my_array_to_compare = np.array([1, 2, 3, 4])

print(f'Are they the same? {np.array_equal(my_array, my_array_to_compare)}')

my_array = np.array([1, 2, 3, 4])
my_array_to_compare = np.array([1, 2, 3, 4, 5])

print(f'Are they the same? {np.array_equal(my_array, my_array_to_compare)}')

Numpy compare two arrays array_equal

Comparing two arrays using array_equiv method

Alternatively you can also use another Numpy method which is array_equiv.

my_array = np.array([1, 2, 3, 4])
my_array_to_compare = np.array([1, 2, 3, 4])

print(f'Are they the same? {np.array_equiv(my_array, my_array_to_compare)}')

my_array = np.array([1, 2, 3, 4])
my_array_to_compare = np.array([1, 2, 3, 4, 5])

print(f'Are they the same? {np.array_equiv(my_array, my_array_to_compare)}')

Numpy compare two arrays array_equal

This how to compare the arrays when you would like to know if they are equal.

See also  How to perform Chi-Squared test using SciPy?

How to test if two arrays are equal in Numpy?

In another type of use cases you may like to know only when arrays are not equal.

import numpy as np

my_array = np.array([1, 2, 3, 4])
my_array_to_compare = np.array([1, 2, 3, 4])

np.testing.assert_array_equal(my_array, my_array_to_compare)

When arrays are the same this will report nothing.

import numpy as np

my_array = np.array([1, 2, 3, 4])
my_array_to_compare = np.array([1, 2, 3, 4, 5])

np.testing.assert_array_equal(my_array, my_array_to_compare)

The arrays are different so this will report an error.

See also  How to convert Numpy array to Python list?

Numpy arrays compare error

Of course you can handle this expection to get more meaningful info.

Similarly to assert_array_equal you can use assert_allclose Numpy method.

import numpy as np

my_array = np.array([1, 2, 3, 4])
my_array_to_compare = np.array([1, 2, 3, 4])

np.testing.assert_allclose(my_array, my_array_to_compare)

my_array = np.array([1, 2, 3, 4])
my_array_to_compare = np.array([1, 2, 3, 4, 5])

np.testing.assert_allclose(my_array, my_array_to_compare)

And this is the output you may need to expect from assert_allclose.

See also  How to use interpolate in Numpy

Numpy arrays compare mismatch error

How to confirm that two arrays are equal in Numpy

You can all confirm that two arrays are the same using all or any methonds.

import numpy as np

my_array = np.array([1, 2, 3, 4])
my_array_to_compare = np.array([1, 2, 3, 4])

if_the_same = (my_array == my_array_to_compare).any()

print(f'Are they the same? {if_the_same}')

my_array = np.array([1, 2, 3, 4])
my_array_to_compare = np.array([1, 2, 3, 4])

if_the_same = (my_array == my_array_to_compare).all()

print(f'Are they the same? {if_the_same}')

Numpy confirm arrays are the same

As you can see you need to define an additional variable and call all or any methods to get a result.