numpy中“less_equal”中遇到的“无效值”的原因可能是什么

时间:2022-12-10 13:13:53

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 ?

这个警告可能是什么原因?

1 个解决方案

#1


16  

That's most likely happening because of a np.nan somewhere 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 broadcasted comparison. Here's a sample -

对于涉及np.nan的所有那些比较,它将输出False。让我们确认它进行广播比较。这是一个样本 -

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.nan in B and that results in all False values.

请注意输出中的第三列,它对应于涉及B中第三个元素np.nan的比较,并导致所有False值。

#1


16  

That's most likely happening because of a np.nan somewhere 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 broadcasted comparison. Here's a sample -

对于涉及np.nan的所有那些比较,它将输出False。让我们确认它进行广播比较。这是一个样本 -

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.nan in B and that results in all False values.

请注意输出中的第三列,它对应于涉及B中第三个元素np.nan的比较,并导致所有False值。