What might be the cause of 'invalid value encountered in less_equal' in numpy

前端 未结 5 1145
陌清茗
陌清茗 2020-12-09 07:27

I experienced a RuntimeWarning

 RuntimeWarning: invalid value encountered in less_equal

Generated by this line of code of mine:

         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 08:05

    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)
    

    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)
    

    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.

提交回复
热议问题