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

前端 未结 5 1147
陌清茗
陌清茗 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条回答
  •  旧时难觅i
    2020-12-09 08:01

    As a follow-up to Divakar's answer and his comment on how to suppress the RuntimeWarning, a safer way is suppressing them only locally using with np.errstate() (docs): it is good to generally be alerted when comparisons to np.nan yield False, and ignore the warning only when this is really what is intended. Here for the OP's example:

    with np.errstate(invalid='ignore'):
      center_dists[j] <= center_dists[i]
    

    Upon exiting the with block, error handling is reset to what it was before.

    Instead of invalid value encountered, one can also ignore all errors by passing all='ignore'. Interestingly, this is missing from the kwargs in the docs for np.errstate(), but not in the ones for np.seterr(). (Seems like a small bug in the np.errstate() docs.)

提交回复
热议问题