比较单元测试中的numpy浮点数组 [英] Comparing numpy float arrays in unit tests

查看:62
本文介绍了比较单元测试中的numpy浮点数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现比较两个numpy浮点数组的单元测试的最佳方法是什么.

What is the best way to implement a unittest that compares two numpy float arrays.

我尝试了unittest.assertEqual(),但不适用于float数组,因为float永远不会100%相等.我不能使用assertAlmostEqual,因为它会测试round(floats)相等性...

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

谢谢

推荐答案

如果仍然使用numpy,为什么不使用numpy测试功能呢?

If you are using numpy anyway, why not use the numpy testing functions?

numpy.testing.assert_array_almost_equal

numpy.testing.assert_array_almost_equal_nulp

这些还处理NaN的细度,检查形状等.

These also handles NaN's fine, check shape, etc.

这篇关于比较单元测试中的numpy浮点数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆