python - numpy 测试断言数组不等于

标签 python numpy python-unittest

我们有numpy.testing.assert_array_equal断言两个数组相等。

但是执行 numpy.testing.assert_array_not_equal 的最佳方法是什么,即确保两个数组不相等?

最佳答案

如果您想专门使用 NumPy 测试,则可以将 numpy.testing.assert_array_equalnumpy.testing.assert_raises 一起使用为相反的结果。例如:

assert_raises(AssertionError, assert_array_equal, array_1, array_2)

还有 numpy.testing.utils.assert_array_compare(它被 numpy.testing.assert_array_equal 使用),但我没有在任何地方看到它的文档,所以谨慎使用。这将检查每个元素是否不同,所以我想这不是您的用例:

import operator

assert_array_compare(operator.__ne__, array_1, array_2)

关于python - numpy 测试断言数组不等于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38506044/

相关文章:

python - 如何将 numpy 数组存储在 Pandas 数据框的列中?

python - 测试设置的继承

python - 如何扩展/自定义unittest的shortDescription()?

python - 为什么涉及 list.index() 调用的 lambda 如此慢?

python - Django - 禁止直接分配到多对多集的前侧

python - 如何获得睡衣或 gwt 中菜单项之间的边距?

python - 减少 python 中的循环

python - TypeError : strptime() argument 1 must be str, 不是 datetime.date Python

python - 对数组的所有元素进行积分

python - 如何在使用 pytest 和命令行选项时跳过 unittest 案例中的设置和拆卸?