NumPy: Test whether two arrays are element-wise equal within a tolerance
NumPy: Basic Exercise-9 with Solution
Write a NumPy program to test whether two arrays are element-wise equal within a tolerance.
Note: The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.
This problem involves using NumPy to compare two arrays for element-wise equality within a specified tolerance. The task is to write a program that checks whether corresponding elements in two given NumPy arrays are equal, considering a small tolerance to account for potential floating-point precision errors. This is particularly useful in numerical computations where exact equality is rare due to rounding errors.
Sample Solution :
Python Code :
# Importing the NumPy library with an alias 'np'
import numpy as np
# Printing the description of the operation being performed
print("Test if two arrays are element-wise equal within a tolerance:")
# Checking if the two arrays are element-wise equal within a tolerance and printing the result
print(np.allclose([1e10,1e-7], [1.00001e10,1e-8]))
print(np.allclose([1e10,1e-8], [1.00001e10,1e-9]))
print(np.allclose([1e10,1e-8], [1.0001e10,1e-9]))
# Checking if the arrays with NaN (Not a Number) values are equal while treating NaN as equal
print(np.allclose([1.0, np.nan], [1.0, np.nan]))
# Checking if the arrays with NaN (Not a Number) values are equal considering NaN values as equal
print(np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True))
Output:
Test if two arrays are element-wise equal within a tolerance: False True False False True
Explanation:
In the above exercise -
print(np.allclose([1e10,1e-7], [1.00001e10,1e-8])): Here the np.allclose() function tests the two arrays [1e10, 1e-7] and [1.00001e10, 1e-8] are element-wise equal within the default tolerance (relative tolerance rtol=1e-9, absolute tolerance atol=1e-12). In this case, the two arrays are not considered equal within the given tolerance, so the function returns False. print(np.allclose([1e10,1e-8], [1.00001e10,1e-9])): Heres the np.allclose() function tests if the two arrays [1e10, 1e-8] and [1.00001e10, 1e-9] are element-wise equal within the default tolerance. In this case, the two arrays are considered equal within the given tolerance.
print(np.allclose([1e10,1e-8], [1.0001e10,1e-9])): Here np.allclose() function tests if the two arrays [1e10, 1e-8] and [1.0001e10, 1e-9] are element-wise equal within the default tolerance. In this case, the two arrays are not considered equal within the given tolerance, so the function returns False.
print(np.allclose([1.0, np.nan], [1.0, np.nan])): Here np.allclose() function tests if the two arrays [1.0, np.nan] and [1.0, np.nan] are element-wise equal within the default tolerance. By default, the function treats NaN values as unequal, so it returns False.
print(np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)): Here the np.allclose() function tests if the two arrays [1.0, np.nan] and [1.0, np.nan] are element-wise equal within the default tolerance. However, this time the equal_nan=True parameter is passed, which tells the function to treat NaN values as equal. In this case, the two arrays are considered equal within the given tolerance, so the function returns True.
Python-Numpy Code Editor:
Previous: Ttest element-wise for complex number, real number of a given array. Also test if a given number is a scalar type or not.
Next: Create an element-wise comparison (greater, greater_equal, less and less_equal) of two given arrays.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/numpy/basic/numpy-basic-exercise-9.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics