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?
Get NumPy : Beginner's Guide - Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.