Best way to assert for numpy.array equality?

后端 未结 6 1143
长情又很酷
长情又很酷 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:23

    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.

提交回复
热议问题