python: getting around division by zero

前端 未结 7 2401
忘了有多久
忘了有多久 2020-12-09 03:38

I have a big data set of floating point numbers. I iterate through them and evaluate np.log(x) for each of them. I get

RuntimeWarning: divide b         


        
相关标签:
7条回答
  • 2020-12-09 04:21

    you could do:

    def safe_ln(x):
        #returns: ln(x) but replaces -inf with 0
        try:
            l = np.log(x)
        except RunTimeWarning:
            l = 0
        return l
    
    0 讨论(0)
提交回复
热议问题