借助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 decimalsMismatch:100%
Max absolute difference:2.99
Max relative difference:0.92793743
x:array([0.232, 1.262])
y:array([3.222, 1.222])
相關用法
- Python Numpy np.fft()用法及代碼示例
- Python numpy.ma.ids()用法及代碼示例
- Python Numpy np.hermeroots()用法及代碼示例
- Python Numpy np.lagvander()用法及代碼示例
- Python Numpy np.lagadd()用法及代碼示例
- Python Numpy np.lagdiv()用法及代碼示例
- Python Numpy np.lagsub()用法及代碼示例
- Python Numpy np.lagmul()用法及代碼示例
- Python Numpy np.hermegrid2d()用法及代碼示例
- Python Numpy np.lagone()用法及代碼示例
- Python Numpy np.lagzero()用法及代碼示例
- Python Numpy np.lagcompanion()用法及代碼示例
- Python Numpy np.lag2poly()用法及代碼示例
- Python Numpy np.hermegrid3d()用法及代碼示例
- Python Numpy np.hermefromroots()用法及代碼示例
注:本文由純淨天空篩選整理自Jitender_1998大神的英文原創作品 Python | Numpy np.assert_array_almost_equal() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。