Python 断言 numpy.array 相等的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3302949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:21:26  来源:igfitidea点击:

Best way to assert for numpy.array equality?

pythonunit-testingnumpy

提问by maker

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.assertEqualfails), what is the best way to assert for equality?

我想为我的应用程序进行一些单元测试,我需要比较两个数组。由于array.__eq__返回一个新数组(因此TestCase.assertEqual失败),断言相等的最佳方法是什么?

Currently I'm using

目前我正在使用

self.assertTrue((arr1 == arr2).all())

but I don't really like it

但我真的不喜欢它

回答by SiggyF

I think (arr1 == arr2).all()looks pretty nice. But you could use:

我觉得(arr1 == arr2).all()挺好看的 但你可以使用:

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.

注意 scipy.array 实际上是一个引用 numpy.array。这样可以更轻松地找到文档。

回答by Josef

check out the assert functions in numpy.testing, e.g.

检查断言函数numpy.testing,例如

assert_array_equal

assert_array_equal

for floating point arrays equality test might fail and assert_almost_equalis more reliable.

对于浮点数组相等性测试可能会失败并且assert_almost_equal更可靠。

update

更新

A few versions ago numpy obtained assert_allclosewhich 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.

几个版本之前获得的 numpyassert_allclose现在是我最喜欢的,因为它允许我们指定绝对和相对误差,并且不需要小数四舍五入作为接近标准。

回答by asimoneau

I find that using self.assertEqual(arr1.tolist(), arr2.tolist())is the easiest way of comparing arrays with unittest.

我发现 using self.assertEqual(arr1.tolist(), arr2.tolist())是将数组与 unittest 进行比较的最简单方法。

I agree it's not the prettiest solution and it's probably not the fastest but it's probably more uniform with the rest of your test cases, you get all the unittest error description and it's really simple to implement.

我同意这不是最漂亮的解决方案,它可能不是最快的,但它可能与您的其余测试用例更加统一,您将获得所有单元测试错误描述,并且实现起来非常简单。

回答by HagaiH

Since Python 3.2 you can use assertSequenceEqual(array1.tolist(), array2.tolist()).

从 Python 3.2 开始,您可以使用assertSequenceEqual(array1.tolist(), array2.tolist()).

This has the added value of showing you the exact items in which the arrays differ.

这具有向您显示数组不同的确切项目的附加价值。

回答by Edo user1419293

In my tests I use this:

在我的测试中,我使用这个:

try:
    numpy.testing.assert_array_equal(arr1, arr2)
    res = True
except AssertionError as err:
    res = False
    print (err)
self.assertTrue(res)

回答by schiebermc

np.linalg.norm(arr1 - arr2) < 1e-6

np.linalg.norm(arr1 - arr2) < 1e-6