NumPy | allclose method
Start your free 7-days trial now!
Numpy's allclose(~)
performs an element-wise comparison given two arrays, and returns True if all of the differences between each pair 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)
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.allclose([2,3], [5,3])
False
Here, the first element-wise comparison 2 != 5
, so the method returns False
.
Specifying an absolute tolerance parameter
np.allclose([6,4], [8,3], atol=2)
True
Here, absolute(6,8) <= 2
and absolute(4,3) <= 2
.
Specifying a relative tolerance parameter
np.allclose([6,3], [4,6], rtol=0.5)
True
Here, absolute(6,4) <= 4*0.5
and absolute(3,6) <= 6*0.5
.
Comparing NaNs
np.allclose(np.NaN, np.NaN)
False
np.allclose(np.NaN, np.NaN, equal_nan=True)
True