当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Numpy np.assert_array_almost_equal()用法及代码示例


借助np.assert_array_almost_equal()方法,如果两个数组对象不等于期望的精度值,我们可以通过使用来获得断言错误np.assert_array_almost_equal()方法。

用法: np.assert_array_almost_equal(actual, desired, decimal)
返回: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])



相关用法


注:本文由纯净天空筛选整理自Jitender_1998大神的英文原创作品 Python | Numpy np.assert_array_almost_equal() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。