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

前端 未结 5 1138
陌清茗
陌清茗 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:01

    Adding to the above answers another way to suppress this warning is to use numpy.less explicitly, supplying the where and out parameters:

    np.less([1, 2], [2, np.nan])  
    

    outputs: array([ True, False]) causing the runtime warning,

    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 docs leaving the value undefined (I got True output for both elements), while

    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).

提交回复
热议问题