“Runtimewarning: divide by zero encountered in the log” is a kind of warning that occurs when you try to take the logarithm of a number that has been divided by zero. Go through this blog to learn more about this warning and ways through which you can prevent this in Python.
About the warning
We know that division by zero is not defined. So, when you first divide by 0 and take the log of that quantity, you’ll get this warning.
Methods of resolving Runtimewarning: Divide by zero encountered in the log
You can choose any of the methods mentioned below to get rid of this warning.
Using an if-else block
Here, you can first specify whether zero exists in the denominator. With the help of if and else statements, you can explicitly check whether a division will give a defined value or not. In this example, you’ll get 0.5 as the output.
import logging
def divide(a, b):
if b == 0:
logging.warning("Cannot divide by zero")
return None
return a / b
result = divide(1, 2)
print(result)
Using numpy.seterr()
This is used for handling errors. You may ignore, warn, raise, call a function, print, or log any type of error. Now, the given error is a division error. So, in this example, first the division error is ignored, and after that, it is
import numpy as np
arr = np.array([4, 2, 0, 5])
np.seterr(divide='ignore')
print(np.log10(arr))
np.seterr(divide='warn')
Using numpy.errstate()
It is similar to the previous function, but it handles threads well. This means that it works well as long as this context manager runs. Once the code completes its execution, the error handling is restored to the original settings. So, in this case, the division error will be ignored.
import numpy as np
arr = np.array([1,2,3,4,0,8])
with np.errstate(divide='ignore'):
print(np.log10(arr))
Using where()
The where the argument will only work for the values that satisfy the given condition. If you take a variable that accepts values greater than zero and then calculate the log, you will not get this warning.
import numpy as np
arr = np.array([9, 0, 81])
result = np.log10(arr, where=arr > 0)
print(result)
Use epsilon instead of zero
You can replace your zeroes with a small value like epsilon. This lets you prevent the division by zero error. Similar to the given code, you can add the epsilon value to your array elements.
import numpy as np
EPSILON = 1e-10
def log10_with_epsilon(arr):
result = np.log10(arr + EPSILON)
return result
arr = np.array([4, 12, 0, 16, 160, 320])
result = log10_with_epsilon(arr)
print(result)
Work with NumPy’s log1p() function
np.log1p() function works with finding the value of the log for numbers closer to zero. Thus, you’ll not get the zero division error at all. The code given below explains how one can use the log1p() function of numpy.
import numpy as np
def log1p_with_zero_check(arr):
result = np.log1p(arr)
return result
arr = np.array([4, 12, 0, 16, 160, 320])
result = log1p_with_zero_check(arr)
print(result)
Check the filtering condition for an array
You can filter the array, too, to avoid zero or negative values. If the value of x is less than 0, it takes the y value.
Consider the given example. Assume arr to be a sample array on which you need to perform the log division operation. In order to avoid the runtimewarning, divide by zero encountered in log error. You can take the divisor as 1e-8. It is an extremely small constant value. Now, c implies a boolean array. It is formed when you compare the arr element value with 1e-8. If c is True ( since it is a boolean array), it adds that element to xv. Otherwise, it will add the corresponding element to yv. This yv is always zero as it is an array of zeroes only.
result = [xv if c and xv > 0 else yv for c, xv, yv in zip(condition, x, y)]
#or
[xv if c else yv
for c, xv, yv in zip(arr>1e-8, np.log(arr), 0)]
statsmodels Runtimewarning: Divide by zero encountered in the log
In statsmodels, this error surfaces when you fit data with either zero or negative values on a regression model. It’s better to convert zero values to a small positive value. For example, it can be 1e-10.
Other tips while working with division
- NumPy’s masked arrays can prove to be useful if you want to mask certain values like zero. This way, you may remove zero before performing log or division operations.
- NumPy’s floating-point numbers have a certain precision value. You should always check this limit.
- Some numpy functions like np.divide() will raise an error if zero is present in the array of numbers. So, you should avoid it at all costs. The np.divide() function is an example of NumPy’s universal functions (ufuncs).
- Try incorporating if statements to check if a zero exists.
FAQs
You should create a mask array that doesn’t have any zero values and then perform division.
Conclusion
This article elaborates on the “runtimewarning: divide by zero encountered in log” warning in Python. It covers ways through which you can prevent the occurrence of this warning. In addition to this, this article covers certain tips through which you can avoid division by zero errors.