numpy-tutorial
numpy-tutorial
numpy中比较两个数字的断言函数
assert_allclose比较数组相等
与assert_array_equal不同的是,该函数有atol(绝对容差限)、rtol参数(相对容差限)。比如对于数组a,b,则将测试是否满足
∣ a − b ∣ ≤ ( a t o l + r t o l ∗ ∣ b ∣ ) |a-b| \leq (atol+rtol*|b|) ∣a−b∣≤(atol+rtol∗∣b∣)
a = np.array([0,0.123456789])
b = np.array([0,0.123456780])
print(np.testing.assert_allclose(a,b,rtol=1e-7,atol=0))
===================================================================
assert_almost_equal断言精度近似相等
#指定精度为小数点后七位
a = 0.123456789
b = 0.123456780
print(np.testing.assert_almost_equal(a,b,decimal=8))
#None 表示没有异常
#指定精度为小数点后九位
# print(np.testing.assert_almost_equal(a,b,decimal=9))
===================================================================
assert_approx_equal断言有效位近似相等
#指定有效位为8
a = 0.123456789
b = 0.123456788
print(np.testing.assert_approx_equal(a,b,significant=8))
#指定有效位为9
# print(np.testing.assert_approx_equal(a,b,significant=9))
===================================================================
assert_array_almost_equal数组近似比较
assert_array_almost_equal数组会首先比较维度,然后再比较数值。
# 精度为8
a = np.array([0,0.123456789])
b = np.array([0,0.123456780])
print(np.testing.assert_array_almost_equal(a,b,decimal=8))
# 精度为9
# print(np.testing.assert_array_almost_equal(a,b,decimal=9))
===================================================================
assert_array_equal比较数组相等
严格比较数组的维度与元素值
a = np.array([0,0.123456789])
b = np.array([0,0.123456789])
print(np.testing.assert_array_equal(a,b))
https://github.com/eric496/numpy-tutorial
https://github.com/numpy/numpy.org/blob/master/content/en/learn.md