python: getting around division by zero python: getting around division by zero python python

python: getting around division by zero


You are using a np function, so I can safely guess that you are working on a numpy array?Then the most efficient way to do this is to use the where function instead of a for loop

myarray= np.random.randint(10,size=10)result = np.where(myarray>0, np.log(myarray), 0)

otherwise you can simply use the log function and then patch the hole:

myarray= np.random.randint(10,size=10)result = np.log(myarray)result[result==-np.inf]=0

The np.log function return correctly -inf when used on a value of 0, so are you sure that you want to return a 0? if somewhere you have to revert to the original value, you are going to experience some problem, changing zeros into ones...


Since the log for x=0 is minus infinite, I'd simply check if the input value is zero and return whatever you want there:

def safe_ln(x):    if x <= 0:        return 0    return math.log(x)

EDIT: small edit: you should check for all values smaller than or equal to 0.

EDIT 2: np.log is of course a function to calculate on a numpy array, for single values you should use math.log. This is how the above function looks with numpy:

def safe_ln(x, minval=0.0000000001):    return np.log(x.clip(min=minval))


You can do this.

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