How to Compare two Numpy Arrays?


Numpy is a widely used library in Python programming that offers efficient and useful ways for array manipulation. During the development process, it is common to encounter situations where it is necessary to compare two Numpy arrays. This can be for verifying the presence of identical elements or to detect the differences between them.

In this article, we will look into various methods for comparing two Numpy arrays, including fundamental comparison operators such as == as well as more advanced functions such as numpy.array_equal() and numpy.setdiff1d(). Our aim is to provide a comprehensive understanding of how to compare Numpy arrays in Python and to guide you in selecting the appropriate method for your specific comparison needs.

Method: 1

Comparing two arrays using the == operator is one of the easiest methods available. This operator checks the elements of the input arrays at the same position and returns a boolean array. In this array, each element represents whether the elements in the input arrays at the corresponding position are equal or not.

Here is an example of the same:

Example

import numpy as np

a = np.array([1, 2, 3])
b = np.array([1, 2, 4])

print(a == b)

Output

[ True  True False]

In this case, the output tells us that the first two elements of the arrays are equal, while the third element is not.

This method is useful when comparing two arrays with the same shape, but it does not work if the arrays have different shapes.

Example

Here is an example:

import numpy as np

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

print(a == b)

Output

False

In this case, we get a ValueError because the two arrays have different shapes, and cannot be shown together.

Method: 2

Another useful function for comparing arrays is numpy.array_equal(). This function returns a boolean output indicating whether the two input arrays have the same shape and elements.

Example

Here is an example:

import numpy as np

a = np.array([1, 2, 3])
b = np.array([1, 2, 3])
c = np.array([1, 2])

print(np.array_equal(a, b))

print(np.array_equal(a, c))

Output

True
False

In this case, the first call to numpy.array_equal() returns True, as the two arrays have the same shape and elements. The second call returns False, as the arrays have different shapes.

While numpy.array_equal() is a useful function for comparing two Numpy arrays for exact equality, it may not be the best approach when dealing with arrays that have small differences. In such cases, numpy.allclose() or other methods may be more appropriate.

Example

Here is an example explaining the same:

import numpy as np

a = np.array([1.0, 2.0, 3.0])
b = np.array([1.0000001, 2.0000001, 3.0000001])

print(np.array_equal(a, b))

Output

False

In this particular case, the two arrays are almost identical, yet numpy.array_equal() yields a False result since the values are not precisely equal.

Method: 3

If you want to check the elements that are present in one array but not in the other, the numpy.setdiff1d() function is used. By comparing the two arrays, this function returns the elements that are present in one array but not in the other.

Example

Here is an example:

import numpy as np

a = np.array([1, 2, 3, 4, 5])
b = np.array([3, 4, 5, 6, 7])

print(np.setdiff1d(a, b))

print(np.setdiff1d(b, a))

Output

[1 2]
[6 7]

Method: 4

If we want to compare two arrays that may have small differences, we can use the numpy.allclose() function. It returns a boolean value indicating whether all elements of two arrays are equal within a certain tolerance.

Example

Here is an example:

import numpy as np

a = np.array([1.0, 2.0, 3.0])
b = np.array([1.0000001, 2.0000001, 3.0000001])

print(np.allclose(a, b))

Output

True

In this case, numpy.allclose() returns True, indicating that the arrays are the same.

Method: 5

If you want to find the elements that two Numpy arrays have in common, you can use the numpy.intersect1d() function, which will return only the elements that are present in both arrays.

Example

Here is an example:

import numpy as np

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

print(np.intersect1d(a, b))

Output

[3 4]

In this case, numpy.intersect1d() returns the elements that are in both a and b.

Method: 6

Another approach to comparing two Numpy arrays is by sorting them and comparing them element−wise using the numpy.array_equiv() function. This function checks if the two arrays have the same shape and if their corresponding elements are equal after sorting.

Example

Here is an example:

import numpy as np

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

print(np.array_equiv(np.sort(a), np.sort(b)))

Output

True

In this case, numpy.array_equiv() returns True, indicating that the sorted arrays are equal.

Conclusion

To sum up, comparing two Numpy arrays is a critical operation in many scientific and data analysis applications. By leveraging the different methods discussed in this article, you can compare arrays for equality, similarity, and differences in a variety of scenarios. From the straightforward == operator to the more advanced numpy.array_equiv() function, each method has its own strengths and weaknesses that make it suitable for specific use cases. Whether you are working with small or large datasets, Numpy provides a powerful set of tools to help you manipulate and analyze your data efficiently. By incorporating these techniques into your Python code, you can streamline your workflow and make more informed decisions based on your data.

Updated on: 20-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements