比较NumPy数组,使NaNs比较相等 [英] Comparing NumPy arrays so that NaNs compare equal

查看:253
本文介绍了比较NumPy数组,使NaNs比较相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个惯用的方法来比较两个NumPy数组,它们会将NaN彼此相等(但不等于NaN以外的任何其他)。

Is there an idiomatic way to compare two NumPy arrays that would treat NaNs as being equal to each other (but not equal to anything other than a NaN).

例如,我想要以下两个数组进行比较:

For example, I want the following two arrays to compare equal:

np.array([1.0, np.NAN, 2.0])
np.array([1.0, np.NAN, 2.0])

和以下两个数组来比较不等号:

and the following two arrays to compare unequal:

np.array([1.0, np.NAN, 2.0])
np.array([1.0, 0.0, 2.0])

以下操作可以实现:

np.all((a == b) | (np.isnan(a) & np.isnan(b)))

但它很笨重,并创建所有这些中间数组。

but it's clunky and creates all those intermediate arrays.

有一种方法更容易眼睛,使用内存?

Is there a way that's easier on the eye and makes better use of memory?

PS如果它有帮助,数组已知具有相同的形状和dtype。

P.S. If it helps, the arrays are known to have the same shape and dtype.

推荐答案

如果你真的关心内存使用有非常大的数组),那么你应该使用numexpr和下面的表达式将为你工作:

If you really care about memory use (e.g. have very large arrays), then you should use numexpr and the following expression will work for you:

np.all(numexpr.evaluate('(a==b)|((a!=a)&(b!=b))'))

我测试了它在长度为3e8的非常大的数组上,代码在我的机器上具有相同的性能

I've tested it on very big arrays with length of 3e8, and the code has the same performance on my machine as

np.all(a==b)

的内存

这篇关于比较NumPy数组,使NaNs比较相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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