python: getting around division by zero

前端 未结 7 2422
忘了有多久
忘了有多久 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
    

提交回复
热议问题