"less"中的无效值在数组中比较np.nan时 [英] invalid value in "less" when comparing np.nan in an array

查看:79
本文介绍了"less"中的无效值在数组中比较np.nan时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

观察

np.nan < 0

False


np.array([np.nan]) < 0

array([False], dtype=bool)


但是


However

np.array([-1, np.nan]) < 0

//anaconda/envs/3.5/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: invalid value encountered in less
  if \__name\__ == '\__main\__':

array([ True, False], dtype=bool)


该错误是什么意思?


What does that error mean?

推荐答案

这仅是警告,并非例外. Numpy会尝试提供帮助,并报告您数组中有一个nan.忽略它是安全的.如果警告本身困扰您(或您的用户),则可以取消警告:

This is only a warning, not an exception. Numpy tries to be helpful and reports that you have a nan in your array. It's safe to ignore it. If the warning itself bothers you (or your users), you can suppress it:

with np.errstate(invalid='ignore'):
    np.less([np.nan, 0], 1)

但是,实际上不建议这样做,因为如果需要在冗长的计算中查找nan的来源,它可以掩盖不太明显的问题并使调试变得更加困难.

However, this is really not recommended since it can mask less obvious issues and make debugging harder if you need to find where a nan comes from in a lengthy calculation.

请注意nan的语义:nan > 0nan < 0nan < nannan == nan都是False.

Note the semantics of a nan: nan > 0, nan < 0 and nan < nan, nan == nan are all False.

还请注意,在numpy中(不同于熊猫),nan的确表示无效而不是不可用.

Also note that in numpy (unlike e.g. pandas) nan really means invalid not not available.

这篇关于"less"中的无效值在数组中比较np.nan时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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