为什么断言np.nan == np.nan会导致错误? [英] Why does assert np.nan == np.nan cause an error?

查看:300
本文介绍了为什么断言np.nan == np.nan会导致错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果

assert 1 == 1

很好,那为什么呢?

assert np.nan == np.nan

引起断言错误吗?

更令人困惑的是,这没关系:

What's even more confusing, this is OK:

assert np.nan != np.nan

测试nan的最佳方法是什么?

What's the best way to test for nan?

推荐答案

NaN具有与自身不相等的属性,您应该使用

NaN has the property that it doesn't equal itself, you should use np.isnan to test NaN values, here np.isnan(np.nan) will yield True:

In[5]:
np.nan == np.nan

Out[5]: False

In[6]:
np.nan != np.nan

Out[6]: True

In[7]:
np.isnan(np.nan)

Out[7]: True

这篇关于为什么断言np.nan == np.nan会导致错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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