python: getting around division by zero

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

    You can do this.

    def safe_ln(x):
       try:
          l = np.log(x)
       except ZeroDivisionError:
          l = 0
       return l
    

提交回复
热议问题