numpy中“在less_equal中遇到无效值"的原因可能是什么 [英] What might be the cause of 'invalid value encountered in less_equal' in numpy

查看:71
本文介绍了numpy中“在less_equal中遇到无效值"的原因可能是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 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屋!

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