numpy.testing.assert_array_almost_equal#

testing.assert_array_almost_equal(actual, desired, decimal=6, err_msg='', verbose=True)[源代码]#

如果两个对象在期望的精度内不相等,则引发 AssertionError.

备注

建议使用 assert_allcloseassert_array_almost_equal_nulpassert_array_max_ulp 之一来代替此函数,以进行更一致的浮点数比较.

该测试验证了相同的形状,并确保 actualdesired 的元素满足:

abs(desired-actual) < 1.5 * 10**(-decimal)

这是一个比最初文档中描述的更宽松的测试,但与实际实现直到舍入差异之前所做的相符.如果形状不匹配或值冲突,则会引发异常.与numpy中的标准用法相反,NaN 像数字一样进行比较,如果两个对象在相同位置都有 NaN,则不会引发断言.

参数:
actualarray_like

要检查的实际对象.

desiredarray_like

期望的对象.

decimalint, 可选

期望的精度,默认是 6.

err_msgstr, 可选

在失败情况下要打印的错误消息.

verbosebool, 可选

如果为真,冲突的值将被附加到错误消息中.

引发:
AssertionError

如果实际值和期望值在指定精度下不相等.

参见

assert_allclose

比较两个类似数组的对象,以达到期望的相对和/或绝对精度.

assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal

示例

第一个断言没有引发异常

>>> np.testing.assert_array_almost_equal([1.0,2.333,np.nan],
...                                      [1.0,2.333,np.nan])
>>> np.testing.assert_array_almost_equal([1.0,2.33333,np.nan],
...                                      [1.0,2.33339,np.nan], decimal=5)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not almost equal to 5 decimals

Mismatched elements: 1 / 3 (33.3%)
Max absolute difference among violations: 6.e-05
Max relative difference among violations: 2.57136612e-05
 ACTUAL: array([1.     , 2.33333,     nan])
 DESIRED: array([1.     , 2.33339,     nan])
>>> np.testing.assert_array_almost_equal([1.0,2.33333,np.nan],
...                                      [1.0,2.33333, 5], decimal=5)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not almost equal to 5 decimals

nan location mismatch:
 ACTUAL: array([1.     , 2.33333,     nan])
 DESIRED: array([1.     , 2.33333, 5.     ])