## RuntimeWarning: divide by zero encountered in log

runtimewarning: divide by zero encountered in divide

runtimewarning: invalid value encountered in log

runtimewarning: divide by zero encountered in true_divide

runtimewarning: divide by zero encountered in double_scalars

runtimewarning: invalid value encountered in multiply

runtimewarning divide by zero encountered in log1p result getattr ufunc method inputs kwargs

python ignore divide by zero

I am using numpy.log10 to calculate the log of an array of probability values. There are some zeros in the array, and I am trying to get around it using

result = numpy.where(prob > 0.0000000001, numpy.log10(prob), -10)

However, `RuntimeWarning: divide by zero encountered in log10`

still appeared and I am sure it is this line caused the warning.

Although my problem is solved, I am confused why this warning appeared again and again?

`numpy.log10(prob)`

calculates the base 10 logarithm for all elements of `prob`

, even the ones that aren't selected by the `where`

. If you want, you can fill the zeros of `prob`

with `10**-10`

or some dummy value before taking the logarithm to get rid of the problem. (Make sure you don't compute `prob > 0.0000000001`

with dummy values, though.)

**"Divide by zero encountered in log" when not dividing by zero ,** transpose()) function is spitting larger values(above 40 or so), resulting in the output of sigmoid( ) to be 1 . And then you're basically taking np. log of 1-1 that is 0 . And as DevShark has mentioned above, it causes the RuntimeWarning: Divide by zero The warning originates from np.log(yp) when yp==0 and in np.log(1 - yp) when yp==1. One option is to filter out these values, and not to pass them into np.log . The other option is to add a small constant to prevent the value from being exactly 0 (as suggested in one of the comments above)

I solved this by finding the lowest non-zero number in the array and replacing all zeroes by a number lower than the lowest :p

Resulting in a code that would look like:

def replaceZeroes(data): min_nonzero = np.min(data[np.nonzero(data)]) data[data == 0] = min_nonzero return data ... prob = replaceZeroes(prob) result = numpy.where(prob > 0.0000000001, numpy.log10(prob), -10)

Note that all numbers get a tiny fraction added to them.

**`RuntimeWarning: divide by zero encountered in log` in ,** `RuntimeWarning: divide by zero encountered in log` in `lstm_text_generation.py` #3496. Closed. hdmetor opened this issue on Aug 16, 2016 · 12 comments. RuntimeWarning: divide by zero encountered in double_scalars. and in another line two errors: RuntimeWarning: divide by zero encountered in double_scalars. RuntimeWarning: invalid value encountered in arcsin. I found it might be problem with float values, but im not sure about it and im typing it here for looking yours help. :) Here is the code:

You can turn it off with seterr

numpy.seterr(divide = 'ignore')

and back on with

numpy.seterr(divide = 'warn')

**Division by zero warning with numpy.log · Issue #92 · hgrecco/pint ,** UnitRegistry() >>> x = 1 * ureg.meter >>> y = 2 * ureg.meter >>> np.log(x / y) __main__:1: RuntimeWarning: divide by zero encountered in log I have ran every function individually and I know the problem is in the function patternRecognition(). I keep getting RuntimeWarning: divide by zero encountered in double_scalars I don't know where this zero is coming from. I know the code is still kind of crappy but I have yet to go over and clean it up. def percentChange(startPoint

This solution worked for me, use `numpy.sterr`

to turn `warnings`

off followed by `where`

numpy.seterr(divide = 'ignore') df_train['feature_log'] = np.where(df_train['feature']>0, np.log(df_train['feature']), 0)

**Naive Bayes: Divide by Zero error,** Although I haven't verified it, a first glance at the features and training set you used shows an obvious problem. You have just two data samples with the exact Division by zero warning with numpy.log · Issue #92 · hgrecco/pint · GitHub. I'm running the latest version from the master branch, and I'm seeing this weird behavior: >>> import pint >>> import numpy as np >>> ureg = pint.UnitRegistry() >>> x = 1 * ureg.meter >>> y = 2 * ureg.meter >>> np.log(x / y) __main__:1:

Just use the `where`

argument in `np.log10`

import numpy as np np.random.seed(0) prob = np.random.randint(5, size=4) /4 print(prob) result = np.where(prob > 0.0000000001, prob, -10) # print(result) np.log10(result, out=result, where=result > 0) print(result)

**Output**

[1. 0. 0.75 0.75] [ 0. -10. -0.12493874 -0.12493874]

**RuntimeWarning: divide by zero encountered in log,** First piece of code will run error: RuntimeWarning: divide by zero encountered in log xxxxxxxx. Cause of the problem is that too big a number, overflow both with float32 and float64 inputs, np.log(nan) returns nan; both with float32 and float64 inputs, np.log(0) returns -inf, but emits a RuntimeWarning: divide by zero encountered in log; Both seem reasonable behaviors to me. Can we close this issue? 👍

**RuntimeWarning: divide by zero encountered in log,** RuntimeWarning: divide by zero encountered in log. Is there a way to get a traceback instead of just printing the line that triggered the I think your code is trying to "divide by zero" or "divide by NaN". If you are aware of that and don't want it to bother you, then you can try: import numpy as np np.seterr(divide='ignore', invalid='ignore')

**“Divide by zero encountered in log” when not dividing by zero,** That's the warning you get when you try to evaluate log with 0: >>> import numpy as np >>> np.log(0) __main__:1: RuntimeWarning: divide by RuntimeWarning: divide by zero encountered in double_scalars Which basically means, you are trying to divide something by 0. speed_dt = distance_dt/(data.loc[i,'delta_time']/3600) This statement might be causing this issue. Try and convert 0 into a anything more or less than 0 and then run again.

**PowerTransformer 'divide by zero encountered in log' + proposed fix,** /Users/bcbrock/github/scikit-learn/sklearn/preprocessing/data.py:2930: RuntimeWarning: divide by zero encountered in log loglike = -n_samples / 2 * np.log( RuntimeWarning: divide by zero encountered in log. Is there a way to get a traceback instead of just printing the line that triggered the

##### Comments

`numpy.log10(prob)`

is being evaluated before the`where`

is being evaluated.- Note that you can use
`numpy.seterr`

eventually in combinations with`catch_warnings`

to change the behaviour of numpy's division by zero. See this related question. - Slightly better: use a context manager:
`with numpy.errstate(divide='ignore'):`