numpy中``less_equal中遇到无效值''的原因可能是什么 [英] What might be the cause of 'invalid value encountered in less_equal' in numpy
问题描述
我遇到了RuntimeWarning
I experienced a RuntimeWarning
RuntimeWarning: invalid value encountered in less_equal
由以下这行代码生成:
center_dists[j] <= center_dists[i]
center_dists[j]
和center_dists[i]
都是numpy数组
Both center_dists[j]
and center_dists[i]
are numpy arrays
此警告可能是什么原因?
What might be the cause of this warning ?
推荐答案
这很可能是由于所涉及的输入中的某个地方存在np.nan
而导致的.下面是一个示例-
That's most likely happening because of a np.nan
somewhere in the inputs involved. An example of it is shown below -
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)
对于所有涉及np.nan
的比较,它将输出False
.让我们为 broadcasted
比较进行确认.这是一个示例-
For all those comparisons involving np.nan
, it would output False
. Let's confirm it for a broadcasted
comparison. Here's a sample -
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)
请注意输出中的第三列,该列与B
中涉及第三元素np.nan
的比较相对应,并且得出所有False
值.
Please notice the third column in the output which corresponds to the comparison involving third element np.nan
in B
and that results in all False
values.
这篇关于numpy中``less_equal中遇到无效值''的原因可能是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!