Previous topic

numpy.logical_xor

Next topic

numpy.isclose

numpy.allclose

numpy.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)[source]

如果两个数组在元素级别在容差内相等,则返回True。

The tolerance values are positive, typically very small numbers. 将相对差(rtol * abs(b))和绝对差atol相加在一起来比较ab之间的绝对差。

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.

Parameters:

a, b : array_like

进行比较的输入数组。

rtol : float

The relative tolerance parameter (see Notes).

atol : float

绝对容差参数(见注释)。

equal_nan : bool

是否比较NaN的相等性。If True, NaN’s in a will be considered equal to NaN’s in b in the output array.

New in version 1.10.0.

Returns:

allclose : bool

Returns True if the two arrays are equal within the given tolerance; False otherwise.

另见

isclose, all, any

注释

如果下面的方程在元素级别为True,那么allclose返回True。

absolute(a - b) <= (atol + rtol * absolute(b))

上述方程在ab中不对称,因此在极少的情况下,allclose(a, b)可能不同于allclose(b, a)

Examples

>>> np.allclose([1e10,1e-7], [1.00001e10,1e-8])
False
>>> np.allclose([1e10,1e-8], [1.00001e10,1e-9])
True
>>> np.allclose([1e10,1e-8], [1.0001e10,1e-9])
False
>>> np.allclose([1.0, np.nan], [1.0, np.nan])
False
>>> np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)
True