Skip to content

Instantly share code, notes, and snippets.

@alexgorban
Last active December 15, 2017 23:35
Show Gist options
  • Save alexgorban/f0b3f09db4e0a9509d712218f0d5ef12 to your computer and use it in GitHub Desktop.
Save alexgorban/f0b3f09db4e0a9509d712218f0d5ef12 to your computer and use it in GitHub Desktop.
Debugging broken tests can be very tricky
Can you spot a problem in the code?
np.testing.assert_almost_equal(A, B, decimal=7)
Error log:
-------------------------------------------------------------------------------
AssertionError:
Arrays are not almost equal to 7 decimals
(mismatch 18.75%)
x: array([[ -3.7830681e-01, 9.2375589e-01, -5.9657533e-02, 0.0000000e+00],
[ -4.5748650e-01, -2.4260328e-01, -8.5548218e-01, 0.0000000e+00],
[ -8.0472981e-01, -2.9634221e-01, 5.1438432e-01, 0.0000000e+00],
[ -2.1066381e+05, 3.0747290e+05, -6.3619737e+06, 1.0000000e+00]])
y: array([[ -3.7830681e-01, 9.2375589e-01, -5.9657533e-02, 0.0000000e+00],
[ -4.5748650e-01, -2.4260328e-01, -8.5548218e-01, 0.0000000e+00],
[ -8.0472981e-01, -2.9634221e-01, 5.1438432e-01, 0.0000000e+00],
[ -2.1066381e+05, 3.0747290e+05, -6.3619737e+06, 1.0000000e+00]])
-------------------------------------------------------------------------------
What about now?
np.set_printoptions(suppress=True)
np.testing.assert_almost_equal(A, B, decimal=7)
Error log:
-------------------------------------------------------------------------------
AssertionError:
Arrays are not almost equal to 7 decimals
(mismatch 18.75%)
x: array([[ -0.3783068, 0.9237559, -0.0596575, 0. ],
[ -0.4574865, -0.2426033, -0.8554822, 0. ],
[ -0.8047298, -0.2963422, 0.5143843, 0. ],
[ -210663.8053348, 307472.8990574, -6361973.6501733, 1. ]])
y: array([[ -0.3783068, 0.9237559, -0.0596575, 0. ],
[ -0.4574865, -0.2426033, -0.8554822, 0. ],
[ -0.8047298, -0.2963422, 0.5143843, 0. ],
[ -210663.805 , 307472.899 , -6361973.65 , 1. ]])
Can you spot a problem in the code?
np.testing.assert_almost_equal(A, B, decimal=7)
Error log:
-------------------------------------------------------------------------------
AssertionError:
Arrays are not almost equal to 7 decimals
(mismatch 18.75%)
x: array([[ -3.7830681e-01, 9.2375589e-01, -5.9657533e-02, 0.0000000e+00],
[ -4.5748650e-01, -2.4260328e-01, -8.5548218e-01, 0.0000000e+00],
[ -8.0472981e-01, -2.9634221e-01, 5.1438432e-01, 0.0000000e+00],
[ -2.1066381e+05, 3.0747290e+05, -6.3619737e+06, 1.0000000e+00]])
y: array([[ -3.7830681e-01, 9.2375589e-01, -5.9657533e-02, 0.0000000e+00],
[ -4.5748650e-01, -2.4260328e-01, -8.5548218e-01, 0.0000000e+00],
[ -8.0472981e-01, -2.9634221e-01, 5.1438432e-01, 0.0000000e+00],
[ -2.1066381e+05, 3.0747290e+05, -6.3619737e+06, 1.0000000e+00]])
--------------------------------------------------------------------------------
The answer https://goo.gl/z6dY5H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment