Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing numpy float arrays in unit tests [duplicate]

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

like image 270
Cobry Avatar asked Feb 17 '13 11:02

Cobry


People also ask

How do you check if two NumPy arrays are the same?

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.

How do I compare two arrays of different sizes Python?

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).

How do you compare two arrays the same in Python?

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 .


1 Answers

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.

like image 196
seberg Avatar answered Sep 21 '22 05:09

seberg