Python| Numpy np.assert_array_almost_equal() 方法
在np.assert_array_almost_equal()
方法的帮助下,如果两个数组对象不等于所需的精度值,我们可以使用np.assert_array_almost_equal()
() 方法得到断言错误。
Syntax : np.assert_array_almost_equal(actual, desired, decimal)
Return : Return the assertion error if two objects are not equal.
示例 #1:
在这个例子中,我们可以看到通过使用np.assert_array_almost_equal()
方法,如果两个数组对象不相等,我们可以通过使用该方法得到断言错误。
# import numpy and assert_array_almost_equal
import numpy as np
import numpy.testing as npt
# using np.assert_array_almost_equal() method
gfg = npt.assert_array_almost_equal([0.2222, 1.2222], [0.2222, 1.2222], decimal = 3)
print(gfg)
输出 :
None
示例 #2:
# import numpy and assert_array_almost_equal
import numpy as np
import numpy.testing as npt
# using np.assert_array_almost_equal() method
gfg = npt.assert_array_almost_equal([0.2322, 1.2622], [3.2222, 1.2222], decimal = 3)
print(gfg)
输出 :
AssertionError:
Arrays are not almost equal to 3 decimals
Mismatch: 100%
Max absolute difference: 2.99
Max relative difference: 0.92793743
x: array([0.232, 1.262])
y: array([3.222, 1.222])