Python numpy中“在less_equal中遇到无效值”的原因可能是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34955158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What might be the cause of 'invalid value encountered in less_equal' in numpy
提问by Alex Gao
I experienced a RuntimeWarning
我遇到了 RuntimeWarning
RuntimeWarning: invalid value encountered in less_equal
Generated by this line of code of mine:
由我的这行代码生成:
center_dists[j] <= center_dists[i]
Both center_dists[j]and center_dists[i]are numpy arrays
两个center_dists[j]和center_dists[i]是numpy的阵列
What might be the cause of this warning ?
出现此警告的原因可能是什么?
采纳答案by Divakar
That's most likely happening because of a np.nansomewhere in the inputs involved. An example of it is shown below -
这很可能是由于所np.nan涉及的输入中的某个地方而发生的。它的一个例子如下所示 -
In [1]: A = np.array([4, 2, 1])
In [2]: B = np.array([2, 2, np.nan])
In [3]: A<=B
RuntimeWarning: invalid value encountered in less_equal
Out[3]: array([False, True, False], dtype=bool)
For all those comparisons involving np.nan, it would output False. Let's confirm it for a broadcastedcomparison. Here's a sample -
对于所有涉及的比较np.nan,它会输出False. 让我们确认一下进行broadcasted比较。这是一个示例 -
In [1]: A = np.array([4, 2, 1])
In [2]: B = np.array([2, 2, np.nan])
In [3]: A[:,None] <= B
RuntimeWarning: invalid value encountered in less_equal
Out[3]:
array([[False, False, False],
[ True, True, False],
[ True, True, False]], dtype=bool)
Please notice the third column in the output which corresponds to the comparison involving third element np.nanin Band that results in all Falsevalues.
请注意输出中的第三列,它对应于涉及第三个元素的比较np.nan,B并导致所有False值。
回答by Ulrich Stern
As a follow-up to Divakar's answer and his comment on how to suppress the RuntimeWarning, a safer way is suppressing them only locallyusing with np.errstate()(docs): it is good to generally be alerted when comparisons to np.nanyield False, and ignore the warning only when this is really what is intended. Here for the OP's example:
作为 Divakar 的回答和他对如何抑制 的评论的后续行动RuntimeWarning,一种更安全的方法是仅在本地使用with np.errstate()(文档)抑制它们:在与np.nanyield进行比较时通常会收到False警报,并且仅在出现这种情况时才忽略警告真的是什么意思。这是 OP 的示例:
with np.errstate(invalid='ignore'):
center_dists[j] <= center_dists[i]
Upon exiting the withblock, error handling is reset to what it was before.
退出with块后,错误处理将重置为之前的状态。
Instead of invalid value encountered, one can also ignore all errors by passing all='ignore'. Interestingly, this is missing from the kwargsin the docs for np.errstate(), but not in the ones for np.seterr(). (Seems like a small bug in the np.errstate()docs.)
取而代之的是invalid value encountered,您还可以通过传递忽略所有错误all='ignore'。有趣的是,这kwargs在 的文档中np.errstate()没有,但在 的文档中没有np.seterr()。(似乎是np.errstate()文档中的一个小错误。)
回答by Revanth M
This happens due to Nanvalues in dataframe, which is completely fine with DF.
这是由于Nan数据帧中的值造成的,这对 DF 来说完全没问题。
In Pycharm, This worked like a charm for me:
在 Pycharm 中,这对我来说就像一个魅力:
import warnings
warnings.simplefilter(action = "ignore", category = RuntimeWarning)
回答by Emir Atakan Y?lmaz
Numpy dtypes are so strict. So it doesnt produce an array like np.array([False, True, np.nan]), it returns array([ 0., 1., nan])which a floatarray.
Numpy dtypes 非常严格。所以它不会产生一个像 的数组np.array([False, True, np.nan]),它返回 array([ 0., 1., nan])一个float数组。
If you try to change a bool array like:
如果您尝试更改 bool 数组,例如:
x= np.array([False, False, False])
x[0] = 5
will retrun array([ True, False, False])... wow
会重新运行array([ True, False, False])......哇
But I think 5>np.nancannot be False, it should be nan, Falsewould mean that a data comparison has been made and it returned the result like 3>5, which I think it's a disaster. Numpy produces data that we actually don't have. If it could have returned nanthen we could handle it with ease.
但我认为 5>np.nan不可能False,应该是nan,False这意味着进行了数据比较并返回了类似的结果3>5,我认为这是一场灾难。Numpy 产生我们实际上没有的数据。如果它可以返回,nan那么我们可以轻松处理它。
So I tried to modify the behavior with a function.
所以我试图用一个函数来修改行为。
def ngrater(x, y):
with np.errstate(invalid='ignore'):
c=x>y
c=c.astype(np.object)
c[np.isnan(x)] = np.nan
c[np.isnan(y)] = np.nan
return c
a = np.array([np.nan,1,2,3,4,5, np.nan, np.nan, np.nan]) #9 elements
b = np.array([0,1,-2,-3,-4,-5, -5, -5, -5]) #9 elements
ngrater(a,b)
returns:
array([nan, False, True, True, True, True, nan, nan, nan], dtype=object)
返回:
array([nan, False, True, True, True, True, nan, nan, nan], dtype=object)
But I think whole memory structure is changed in that way. Instead of getting a memory-block with uniform unites, it will produce a block of pointers, where the real data is somewhere else. So function may perform slower and probably that's why Numpy doesn't do that. We need a superBooldtype which will contain also np.nan, or we just have to use float arrays +1:True, -1:False, nan:nan
但我认为整个内存结构都以这种方式改变了。它将生成一个指针块,而不是获得一个具有统一单元的内存块,而真正的数据在其他地方。所以函数可能执行得更慢,这可能就是 Numpy 不这样做的原因。我们需要一个superBooldtype 也将包含np.nan,或者我们只需要使用浮点数组+1:True, -1:False, nan:nan
回答by Yuri Feldman
Adding to the above answers another way to suppress this warning is to use numpy.lessexplicitly, supplying the whereand outparameters:
添加到上述答案的另一种抑制此警告的方法是使用numpy.less显式,提供where和out参数:
np.less([1, 2], [2, np.nan])
outputs: array([ True, False])causing the runtime warning,
输出:array([ True, False])导致运行时警告,
np.less([1, 2], [2, np.nan], where=np.isnan([2, np.nan])==False)
does not calculate result for the 2nd array element according to the docsleaving the value undefined (I got True output for both elements), while
不根据文档计算第二个数组元素的结果,留下未定义的值(我对两个元素都得到了 True 输出),而
np.less([1, 2], [2, np.nan], where=np.isnan([2, np.nan])==False, out=np.full((1, 2), False)
writes the result into an array pre-initilized to False (and so always gives False in the 2nd element).
将结果写入预先初始化为 False 的数组中(因此总是在第二个元素中给出 False)。

