Best way to assert for numpy.array equality?

后端 未结 6 1136
长情又很酷
长情又很酷 2020-12-13 16:37

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)

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 17:21

    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.

提交回复
热议问题