Best way to assert for numpy.array equality?
I want to make some unit-tests for my app, and I need to compare two arrays. Since array.__eq__
returns a new array (so TestCase.assertEqual
fails), what is the best way to assert for equality?
Currently I'm using
self.assertTrue((arr1 == arr2).all())
but I don't really like it
check out the assert functions in numpy.testing
, e.g.
assert_array_equal
for floating point arrays equality test might fail and assert_almost_equal
is more reliable.
update
A few versions ago numpy obtained assert_allclose
which is now my favorite since it allows us to specify both absolute and relative error and doesn't require decimal rounding as the closeness criterion.
I think (arr1 == arr2).all()
looks pretty nice. But you could use:
numpy.allclose(arr1, arr2)
but it's not quite the same.
An alternative, almost the same as your example is:
numpy.alltrue(arr1 == arr2)
Note that scipy.array is actually a reference numpy.array. That makes it easier to find the documentation.