How to Check NumPy Array Equal?

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:November 10, 2023
  • Reading time:18 mins read

How to check if NumPy Array equal or not? By using Python NumPy np.array_equal() function or == (equal operator) you can check if two arrays have the same shape and elements. These return True if it has the same shape and elements, False otherwise. There are also other ways to check if two NumPy arrays are equal or not.

In this article, I will explain how to check if two arrays are equal or not by using the following approaches. By using these we can carry out an element-wise equality comparison on NumPy arrays.

  • array_equal() Function
  • Equal Operator (==)
  • array_equiv() Function
  • allclose() Function

1. Quick Examples of Checking NumPy Array Equal

If you are in a hurry, below are some quick examples of how to check if two Python NumPy arrays are equal or not.


# Quick examples of checking numpy array equal

# Example 1: Check array equal element-wise 
arr = np.array([2,4,5,7,9])
arr1 = np.array([2,4,6,7,10])
# Using == operator
result = arr == arr1

# Example 2: Using == operator and all() method
arr = np.array([2,4,5,7,9])
arr1 = np.array([2,4,6,7,10])
result = (arr == arr1).all()

# Example 3: Using == operator and all() method
arr = np.array([2,4,5,7,9])
arr1 = np.array([2,4,5,7,9])
result = (arr == arr1).all()

# Example 4: Check if the arrays 
# Are equal element-wise
arr = np.array([2, 4, 5, 7, 9])
arr1 = np.array([2, 4, 5, 7, 9])
result = np.array_equal(arr, arr1)

# Example 5: Use numpy.allcloses() function 
# To numpy array equal
arr = np.array([2,3,5,8,9])
arr1 = np.array([2,3,5,8,9])
result = np.allclose(arr, arr1)

# Example 6: Use numpy.array_equiv() function
arr = np.array([2,3,5,8,9])
arr1 = np.array([2,3,5,8,9])
result = np.array_equiv(arr, arr1)

2. Using == Operator and all() Method

You can use the == operator and the all() method to check if all elements in two NumPy arrays are equal.

2.2 Check Array Equal Element Wise Using == Operator

You can check if two NumPy arrays are equal element-wise using the == operator. When you use the == operator on two NumPy arrays, it performs element-wise comparison and returns a boolean array of the same shape, where each element indicates whether the corresponding elements in the two arrays are equal or not.

In the below example, True indicates that the corresponding elements in arr and arr1 are equal, and False indicates that the elements are not equal. As you can see, the first, second, and fourth elements are equal, while the third and fifth elements are not equal between the two arrays.


# Imprt numpy
import numpy as np

arr = np.array([2,4,5,7,9])
print("First array:", arr)
arr1 = np.array([2,4,6,7,10])
print("Second array:", arr1)

# Check array equal element wise 
# Using == operator
result = arr == arr1
print("Check the element-wise equality:", result)

Yields below output.

numpy array equal

2.3 Check Two Arrays are Exactly Equal

You can use the == operator for element-wise comparison between arr and arr1, and then apply the .all() method to check if all elements in the resulting boolean array are True. If all elements are equal, it will print True; otherwise, it will print False.

This output indicates that the arrays are not equal element-wise because the elements at indices 2 (0-based indexing) are different in arr and arr1.


import numpy as np

arr = np.array([2,4,5,7,9])
print("First array:", arr)
arr1 = np.array([2,4,6,7,10])
print("Second array:", arr1)

# Using == operator and all() method
result = (arr == arr1).all()
print("Check exact equality:", result)

Yields below output.

numpy array equal

To check if the two NumPy arrays, arr, and arr1, are equal element-wise. You can use (arr == arr1) to perform an element-wise comparison between arr and arr1. This results in a boolean array where each element indicates whether the corresponding elements in the two arrays are equal (True) or not (False). Then use the all() method to check if all elements in the resulting boolean array are True. If all elements are True, it means that all elements in arr and arr1 are equal.


import numpy as np

arr = np.array([2,4,5,7,9])
arr1 = np.array([2,4,5,7,9])

# Using == operator and all() method
result = (arr == arr1).all()
print("Element-wise equality:", result)

# Output:
# Element-wise equality: True 

3. Using array_equal()

You can also use the numpy.array_equal() function to check if two arrays are exactly the same, and it also provides the equal_nan parameter, which allows you to specify whether to consider NaN values as equal or not.

3.1 Syntax of numpy.array_equal()

Following is the syntax of the numpy.array_equal() function.


# Syntax of Use numpy.array_equal()
numpy.array_equal(arr, arr1, equal_nan=False)

3.2 Parameters of the numpy.array_equal()

Following are the parameters of numpy.array_equal().

  • arr - The first input array.
  • arr1 - The second input array.
  • equal_nan - Whether to compare NaN’s as equal. If the dtype of arr and arr1 is complex, values will be considered equal if either the real or the imaginary component of a given value is nan.

3.3 numpy.array_equal() Example

To check if two NumPy arrays are equal, you can use the numpy.array_equal() function. This function compares the elements of two arrays and returns True if they are equal, and False otherwise.

In the below example, arr and arr1 are compared using np.array_equal(), and the result is stored in the result variable. If arr and arr1 have the same elements in the same order.


import numpy as np

arr = np.array([2, 4, 5, 7, 9])
arr1 = np.array([2, 4, 5, 7, 9])

# Check if the arrays are equal element-wise
result = np.array_equal(arr, arr1)
print("Element-wise equality:", result)

# Output:
# Element-wise equality: True

4. Use numpy.allcloses() Function to Check NumPy Array Equal

Alternatively, you can use numpy.allclose() function to check if two arrays are element-wise equal or not in Python. The numpy.allclose() function returns True if all the elements inside both arrays are equal within a specified tolerance.

This code np.allclose() is used to check if the elements of arr and arr1 are equal within a certain tolerance (which by default is very small). Since the arrays arr and arr1 have the same elements in the same order.


import numpy as np

arr = np.array([2,3,5,8,9])
arr1 = np.array([2,3,5,8,9])

# Use numpy.allcloses() function 
# To numpy array equal
result = np.allclose(arr, arr1)
print("Element-wise equality:", result)

# Output:
# Element-wise equality: True

5. Use numpy.array_equiv() Function

You can also use the numpy.array_equiv() function to check if the arrays arr and arr1 are equal element-wise. The numpy.array_equiv() function returns True if the arrays are broadcastable to a common shape and all their elements are equal, and False otherwise. In your case, since arr and arr1 have the same shape and all their elements are equal. This indicates that the arrays arr and arr1 are equal element-wise.


import numpy as np

arr = np.array([2,3,5,8,9])
arr1 = np.array([2,3,5,8,9])

# Use numpy.array_equiv() function
result = np.array_equiv(arr, arr1)
print("Element-wise equality:", result)

# Output:
# Element-wise equality: True

Frequently Asked Questions Check NumPy Array Equal

How do I check if two NumPy arrays are element-wise equal?

You can use the np.array_equal() function to check if two NumPy arrays are element-wise equal. For instance, np.array_equal(arr1, arr2) compares the elements of arr1 and arr2 element-wise. If all elements are equal, it returns True; otherwise, it returns False.

How do I check if two NumPy arrays are structurally equal (same shape and elements)?


To check if two NumPy arrays are structurally equal, meaning they have the same shape and elements at corresponding positions, you can use the np.array_equal() function. This function compares the shapes and elements of the arrays and returns True if they are structurally equal, and False otherwise.

Can I compare arrays element-wise and get a boolean array as the result?

You can compare NumPy arrays element-wise and get a boolean array as the result. NumPy supports element-wise operations, including element-wise comparison, which means comparing corresponding elements of two arrays.

What if I want to check if all elements in an array are equal to a specific value?

If you want to check if all elements in a NumPy array are equal to a specific value, you can use the np.all() function in combination with the equality operator (==).

Can np.array_equal() handle arrays with different shapes?

You can np.array_equal() compares arrays element-wise and requires the input arrays to have the same shape. If you want to compare arrays with different shapes, you might need to reshape or broadcast them to compatible shapes before using np.array_equal().

Conclusion

In this article, I have explained different ways to check if two NumPy arrays are equal by checking shape and values element-wise. Here, I used methods == operator, np.array_equal(), np.array_equiv(), and np.allclose() functions.

Happy Learning!!

References

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.

Leave a Reply