NumPy Logic functions: isclose() function
numpy.isclose() function
The isclose() function is used to returns a boolean array where two arrays are element-wise equal within a tolerance.
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.
Syntax:
numpy.isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)
Version: 1.15.0
Parameter:
Name | Description | DataTypes | Required / Optional |
---|---|---|---|
a, b | Input arrays to compare. array_like |
Required | |
rtol | The relative tolerance parameter. | float | Required |
atol | The absolute tolerance parameter. | float | Required |
equal_nan | Whether to compare NaN’s as equal. If True, NaN’s in a will be considered equal to NaN’s in b in the output array. |
bool | Required |
Returns:
y : array_like - Returns a boolean array of where a and b are equal within the given tolerance.
If both a and b are scalars, returns a single boolean value.
Notes:
For finite values, isclose uses the following equation to test whether two floating point values are equivalent.
absolute(a - b) <= (atol + rtol * absolute(b))
Unlike the built-in math.isclose, the above equation is not symmetric in a and b – it assumes b is the reference
value – so that isclose(a, b) might be different from isclose(b, a). Furthermore, the default value of atol is not zero,
and is used to determine what small values should be considered close to zero. The default value is appropriate for
expected values of order unity: if the expected values are significantly smaller than one, it can result in false positives.
atol should be carefully selected for the use case at hand. A zero value for atol will result in False if either a or b is zero.
NumPy.isclose() method Example-1:
>>> import numpy as np
>>> np.isclose([1e10,1e-6], [1.00001e10,1e-7])
Output:
array([True, False])
NumPy.isclose() method Example-2:
>>> import numpy as np
>>> np.isclose([1e10,1e-9], [1.00001e10,1e-8])
Output:
array([True, True])
NumPy.isclose() method Example-3:
>>> import numpy as np
>>> np.isclose([1e10,1e-8], [1.0001e10,1e-9])
Output:
array([False, True])
NumPy.isclose() method Example-4:
>>> import numpy as np
>>> np.isclose([1.0, np.nan], [1.0, np.nan])
Output:
array([True, False])
NumPy.isclose() method Example-5:
>>> import numpy as np
>>> np.isclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)
Output:
array([True, True])
NumPy.isclose() method Example-6:
>>> import numpy as np
>>> np.isclose([1e-8, 1e-7], [0.0, 0.0])
Output:
array([ True, False])
NumPy.isclose() method Example-7:
>>> import numpy as np
>>> np.isclose([1e-100, 1e-7], [0.0, 0.0], atol=0.0)
Output:
array([False, False])
NumPy.isclose() method Example-8:
>>> import numpy as np
>>> np.isclose([1e-15, 1e-15], [1e-30, 0.0])
Output:
array([ True, True])
NumPy.isclose() method Example-9:
>>> import numpy as np
>>> np.isclose([1e-15, 1e-15], [1e-30, 0.999999e-15], atol=0.0)
Output:
array([False, True])
Python - NumPy Code Editor:
Previous: allclose() function
Next: array_equal() function
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/numpy/logic-functions/isclose.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics