numpy.allclose() in Python
Last Updated :
28 Dec, 2018
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 :
Python3
# 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-05
atol = 1e-08
res = geek.allclose(in_arr1, in_arr2, rtol, atol)
print ("Are the two arrays are equal within the tolerance: \t", res)
Output:
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 :
Python3
# 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-06
atol = 1e-09
res = geek.allclose(in_arr1, in_arr2, rtol, atol)
print ("Are the two arrays are equal within the tolerance: \t", res)
Output:
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 :
Python3
# 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-06
atol = 1e-09
res = geek.allclose(in_arr1, in_arr2, rtol, atol)
print ("Are the two arrays are equal within the tolerance: \t", res)
Output:
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 :
Python3
# 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-06
atol = 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)
Output:
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
Similar Reads
numpy.all() in Python The numpy.all() function tests whether all array elements along the mentioned axis evaluate to True. Syntax: numpy.all(array, axis = None, out = None, keepdims = class numpy._globals._NoValue at 0x40ba726c) Parameters :Â array :[array_like]Input array or object whose elements, we need to test. axis
3 min read
Python | numpy.assert_allclose() method With the help of numpy.assert_allclose() method, we can get the assertion errors when two array objects are not equal upto the mark by using numpy.assert_allclose(). Syntax : numpy.assert_allclose(actual_array, desired_array) Return : Return the Assertion error if two array objects are not equal. Ex
1 min read
numpy.real_if_close() function - Python In this numpy.real_if_close()function, if complex input returns a real array then complex parts are close to zero. Syntax : numpy.real_if_close(arr, tol = 100) Parameters : arr : [array_like] Input array. tol : [float] âClose to zeroâ is defined as tol. Tolerance in machine epsilons for the complex
1 min read
Matplotlib.pyplot.close() in Python Matplotlib close() function in pyplot module of the matplotlib library is used to close a figure window. This function is designed to close a figure window or a set of figure windows. When called without any arguments, it closes the currently active figure. Alternatively, we can pass a figure number
3 min read
__closure__ magic function in Python Almost everything in Python is an object, similarly function is an object too and all the function objects have a __closure__ attribute. __closure__ is a dunder/magic function i.e. methods having two underscores as prefix and suffix in the method name A closure is a function object that remembers va
1 min read
close() method in PyQt5 In this article, we will see how to use close() method which belongs to the QWidget class, this method is used to close the window in PyQt5 application. In other words by close() method the window get closed without manually closing it. Syntax : self.close() Argument : It takes no argument. Code : P
1 min read
Python Closures In Python, a closure is a powerful concept that allows a function to remember and access variables from its lexical scope, even when the function is executed outside that scope. Closures are closely related to nested functions and are commonly used in functional programming, event handling and callb
3 min read
Wand path_close() function in Python path_close() is another function included in wand for paths. The main aim of this function is to join the last destination point to the first point in the path. It simply adds a path element to the current path which closes the current subpath by drawing a straight line from the current point to the
1 min read
Python | os.close() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.close() method in Python is used to close the given file descriptor, so that i
2 min read
Context Manager in Python In any programming language, the usage of resources like file operations or database connections is very common. But these resources are limited in supply. Therefore, the main problem lies in making sure to release these resources after usage. If they are not released then it will lead to resource l
3 min read