"less"中的无效值在数组中比较np.nan时 [英] invalid value in "less" when comparing np.nan in an array
问题描述
观察
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 > 0
,nan < 0
和nan < nan
,nan == 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屋!