What is the best way to implement a unittest that compares two numpy float arrays.
I've tried unittest.assertEqual() but didn't work for float arrays because float are never 100% equal. I can't use assertAlmostEqual because it tests the round(floats) equality ...
does anyone emplemented something like this
self.assertFloatArrayEqual(array1, array2, msg = "array are not equal")
thanks
Method 1: We generally use the == operator to compare two NumPy arrays to generate a new array object. Call ndarray. all() with the new array object as ndarray to return True if the two NumPy arrays are equivalent.
pad the smaller array to be as long as the longer array, then substract one from the other and look with np. where((Arr1-Arr2)==0).
Compare Two Arrays in Python Using the numpy. array_equiv() Method. The numpy. array_equiv(a1, a2) method takes array a1 and a2 as input and returns True if both arrays' shape and elements are the same; otherwise, returns False .
If you are using numpy anyway, why not use the numpy testing functions?
numpy.testing.assert_array_almost_equal
and
numpy.testing.assert_array_almost_equal_nulp
These also handles NaN's fine, check shape, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With