Time for action – asserting arrays almost equal
Let's form arrays with the values from the previous Time for action tutorial by adding a 0
to each array:
Calling 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:
Decimal 8 None
Calling the function with higher precision:
print "Decimal 9", np.testing.assert_array_almost_equal([0, 0.123456789], [0, 0.123456780], decimal=9)
An exception is thrown:
Decimal 9 Traceback (most recent call last): … assert_array_compare raiseAssertionError(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 array with different shapes
Use the NumPy array_almost_equal
function to compare two arrays with different shapes.