Comparing NumPy arrays so that NaNs compare equal

后端 未结 4 2162
南旧
南旧 2020-12-15 18:16

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 e

4条回答
  •  被撕碎了的回忆
    2020-12-15 18:45

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

    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)
    

    and uses the same amount of memory

提交回复
热议问题