numpy.testing.assert_array_almost_equal

assert_array_almost_equal(x,y,decimal=6,err_msg=' ',verbose=True)

如果x,y不满足一定精度的误差的话,会报错,decimal默认为6。

精度的设置是:abs(desired-actual) < 1.5 * 10**(-decimal),而且是逐个元素的比较。

如下:

import numpy as np
from numpy.testing import assert_array_almost_equalif __name__ == '__main__':a = np.arange(6).reshape(2,3)print(a,a.sum(0))b = a - 1e-5print(b)assert_array_almost_equal(a,b,decimal=6)

输出:

[[0 1 2][3 4 5]] [3 5 7]
[[-1.00000e-05  9.99990e-01  1.99999e+00][ 2.99999e+00  3.99999e+00  4.99999e+00]]
AssertionError: 
Arrays are not almost equal to 6 decimalsMismatched elements: 6 / 6 (100%)
Max absolute difference: 1.e-05
Max relative difference: 1.x: array([[0, 1, 2],[3, 4, 5]])y: array([[-1.00000e-05,  9.99990e-01,  1.99999e+00],[ 2.99999e+00,  3.99999e+00,  4.99999e+00]])

参考:numpy.testing.assert_array_almost_equal — NumPy v1.22 Manualicon-default.png?t=M3C8https://numpy.org/doc/stable/reference/generated/numpy.testing.assert_array_almost_equal.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部