NumPy | isclose method
Start your free 7-days trial now!
Numpy's isclose(~) performs an element-wise comparison given two arrays, and for each comparison, returns True if the difference between the two values falls within the specified tolerance.
Parameters
1. x1 | array-like
The first input array.
2. x2 | array-like
The second input array.
3. rtol | float | optional
The relative tolerance parameter. By default, rtol=0.
4. atol | float | optional
The absolute tolerance parameter. By default, atol is set to a small number (~1e-8), and is thus not suitable when comparing numbers that are far smaller than 1.
5. equal_nan | boolean | optional
If True, then element-wise comparisons that involve two NaNs will evaluate to True. By default, equal_nan=False.
Here, the element-wise comparison evaluates to True if:
absolute(a - b) <= (atol + rtol * absolute(b))
Return value
A single boolean that indicates whether or not two arrays are "close" enough.
Examples
Basic usage
np.isclose([1,2], [3,2])
array([False, True])
Here, the first element-wise comparison 2 != 5, so the method returns False.
Specifying an absolute tolerance parameter
np.isclose([6,4], [8,3], atol=2)
array([ True, True])
Here, absolute(6,8) <= 2 and absolute(4,3) <= 2.
Specifying a relative tolerance parameter
np.isclose([6,3], [4,6], rtol=0.5)
array([ True, True])
Here, absolute(6,4) <= 4*0.5 and absolute(3,6) <= 6*0.5.
Comparing NaNs
np.isclose(np.NaN, np.NaN)
False
np.isclose(np.NaN, np.NaN, equal_nan=True)
True