Time for action – asserting arrays almost equal
Let's form arrays with the values from the previous Time for action section by adding a 0
to each array:
- Call the function with lower precision:
print("Decimal 8", np.testing.assert_array_almost_equal([0, 0.123456789], [0, 0.123456780], decimal=8))
The result is as follows:
Decimal 8 None
- Call the function with higher precision:
print("Decimal 9", np.testing.assert_array_almost_equal([0, 0.123456789], [0, 0.123456780], decimal=9))
The test raises an
AssertionError
:Decimal 9 Traceback (most recent call last): … assert_array_compare raise AssertionError(msg) AssertionError: Arrays are not almost equal (mismatch 50.0%) x: array([ 0. , 0.12345679]) y: array([ 0. , 0.12345678])
What just happened?
We compared two arrays with the NumPy array_almost_equal()
function.
Have a go hero – comparing arrays with different shapes
Use the NumPy array_almost_equal()
function to compare two arrays...