How to check if NumPy Array equal or not? By using Python NumPy np.array_equal() function or == (equal operator) you 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 Python 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.
# Below are the quick examples
# Create NumPy array
arr = np.array([2,4,5,7,9])
arr1 = np.array([2,4,5,7,9])
# Example 1: Using array_equal()
np.array_equal(arr, arr1)
# Example 2: Using == operator and all() method
print((arr == arr1).all())
# Example 3: use numpy.allcloses() function to numpy array equal
arr2 = np.allclose(arr, arr1)
# Example 4: Use numpy.array_equiv() function
np.array_equiv(arr, arr1)
2. Using == Operator and all() Method
2.2 Check Array Equal Element Wise using == Operator
Like any other programming language, you can use the equal comparison operator (==) to check if two NumPy arrays are equal or not. This operator checks element by element and returns the array with the result for each element. The following example compares arr and arr1 objects element-wise and returns the array with boolean values either True for matching and False for not matching.
import numpy as np
arr = np.array([2,4,5,7,9])
arr1 = np.array([2,4,6,7,10])
# Using == operator and all() method
print(arr == arr1)
# Output
#[ True True False True False]
2.3 Check Two Arrays are Exactly Equal
If you wanted a single value whether all elements in an array are matched or not use all()
function on the result of the == equal condition.
arr = np.array([2,4,5,7,9])
arr1 = np.array([2,4,6,7,10])
# Using == operator and all() method
print((arr == arr1).all())
# Output
# False
Now let’s use the same array and check the result. The below example returns True as all elements of both arrays are exactly the same.
arr = np.array([2,4,5,7,9])
arr1 = np.array([2,4,5,7,9])
# Using == operator and all() method
print((arr == arr1).all())
# Output
# True
3. Using array_equal()
You can also use numpy.array_equal()
function to check if two elements are exactly the same or not. This function takes the arrays you wanted to compare and equal_nan
param whether to consider nan values or not in the comparison.
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)
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.2 numpy.array_equal() Example
In the below example we are comparing two arrays that contain same values hence, it returns True.
arr = np.array([2,4,5,7,9])
arr1 = np.array([2,4,5,7,9])
# Using == operator and all() method
print(np.array_equal(arr,arr1))
# Output
# True
4. Use numpy.allcloses() Function to NumPy Array Equal
We 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. In the below code, we used the np.allclose() function to check if arr is equal to arr1.
# Use numpy.allcloses() function to numpy array equal
arr2 = np.allclose(arr, arr1)
print(arr2)
# Output
# True
5. Use numpy.array_equiv() Function
You can also use numpy.array_equiv()
function to check whether two arrays are equal or not in Python. This function returns True if both arrays have the same shape and all the elements are equal and return False otherwise.
import numpy as np
arr = np.array([2,4,5,7,9])
arr1 = np.array([2,3,5,8,9])
# Use numpy.array_equiv() function
arr2 = np.array_equiv(arr, arr1)
print(arr2)
# Output
# False
Conclusion
In this article, I have explained different ways how 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!!
Related Articles
- NumPy where() Function With Examples
- How To Use NumPy dot() Function in Python
- Numpy loadtxt() Explained with Examples
- How to use Python NumPy arange() Function
- How to Calculate minimum() of Array in NumPy?
- Python NumPy Split Array – Using split() Function
- How to Calculate Maximum of Array in NumPy
- How to Use NumPy Sum() in Python
- Python NumPy zeros() Function