numpy.allclose() in Python
numpy.allclose() function is used to find if two arrays are element-wise equal within a tolerance. The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(arr2)) and the absolute difference atol are added together to compare against the absolute difference between arr1 and arr2. If either array contains one or more NaNs, False is returned. Infs are treated as equal if they are in the same place and of the same sign in both arrays.
If the following equation is element-wise True, then allclose returns True.
absolute(arr1 - arr2) <= (atol + rtol * absolute(arr2))
As, The above equation is not symmetric in arr1 and arr2, So, allclose(arr1, arr2) might be different from allclose(arr2, arr1) in some rare cases.
Syntax : numpy.allclose(arr1, arr2, rtol, atol, equal_nan=False)
Parameters :
arr1 : [array_like] Input 1st array.
arr2 : [array_like] Input 2nd array.
rtol : [float] The relative tolerance parameter.
atol : [float] The absolute tolerance parameter.
equal_nan : [bool] Whether to compare NaN’s as equal. If True, NaN’s in arr1 will be considered equal to NaN’s in arr2 in the output array.Return : [ bool] Returns True if the two arrays are equal within the given tolerance, otherwise it returns False.
Code #1 :
# Python program explaining # allclose() function import numpy as geek # input arrays in_arr1 = geek.array([5e5, 1e-7, 4.000004e6]) print ("1st Input array : ", in_arr1) in_arr2 = geek.array([5.00001e5, 1e-7, 4e6]) print ("2nd Input array : ", in_arr2) # setting the absolute and relative tolerance rtol = 1e-05atol = 1e-08 res = geek.allclose(in_arr1, in_arr2, rtol, atol) print ("Are the two arrays are equal within the tolerance: \t", res) |
1st Input array : [ 5.00000000e+05 1.00000000e-07 4.00000400e+06] 2nd Input array : [ 5.00001000e+05 1.00000000e-07 4.00000000e+06] Are the two arrays are equal within the tolerance: True
Code #2 :
# Python program explaining # allclose() function import numpy as geek # input arrays in_arr1 = geek.array([5e5, 1e-7, 4.000004e6]) print ("1st Input array : ", in_arr1) in_arr2 = geek.array([5.00001e5, 1e-7, 4e6]) print ("2nd Input array : ", in_arr2) # setting the absolute and relative tolerance rtol = 1e-06atol = 1e-09 res = geek.allclose(in_arr1, in_arr2, rtol, atol) print ("Are the two arrays are equal within the tolerance: \t", res) |
1st Input array : [5000000.0, 1e-07, 40000004.0] 2nd Input array : [5000001.0, 1e-07, 40000000.0] Are the two arrays are equal within the tolerance: True
Code #3 :
# Python program explaining # allclose() function import numpy as geek # input arrays in_arr1 = geek.array([5e5, 1e-7, geek.nan]) print ("1st Input array : ", in_arr1) in_arr2 = geek.array([5e5, 1e-7, geek.nan]) print ("2nd Input array : ", in_arr2) # setting the absolute and relative tolerance rtol = 1e-06atol = 1e-09 res = geek.allclose(in_arr1, in_arr2, rtol, atol) print ("Are the two arrays are equal within the tolerance: \t", res) |
1st Input array : [500000.0, 1e-07, nan] 2nd Input array : [500000.0, 1e-07, nan] Are the two arrays are equal within the tolerance: False
Code #4 :
# Python program explaining # allclose() function import numpy as geek # input arrays in_arr1 = geek.array([5e5, 1e-7, geek.nan]) print ("1st Input array : ", in_arr1) in_arr2 = geek.array([5e5, 1e-7, geek.nan]) print ("2nd Input array : ", in_arr2) # setting the absolute and relative tolerance rtol = 1e-06atol = 1e-09 res = geek.allclose(in_arr1, in_arr2, rtol, atol, equal_nan = True) print ("Are the two arrays are equal within the tolerance: \t", res) |
1st Input array : [500000.0, 1e-07, nan] 2nd Input array : [500000.0, 1e-07, nan] Are the two arrays are equal within the tolerance: True
Recommended Posts:
- Important differences between Python 2.x and Python 3.x with examples
- Python | Convert list to Python array
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Index of Non-Zero elements in Python list
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- abs() in Python
- Python Set | pop()
- set add() in python
- Any & All in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



