RuntimeWarning: divide by zero encountered in log

RuntimeWarning: divide by zero encountered in log. How to overcome this error. My data is imbalanced
Giving error while performing logistic regression…

You’ll have to balance your dataset first.
Or you can try adding a small value epsilon ~ 10^-5 inside the log to refrain it from setting to zero.
You can write log(k) as log(k+epsilon).
Here, epsilon=0.000001
Let me know if that works.

1 Like

I removed few features and that helped me… Anyways thanks for this i will try this too can u try to tell abt this little more… When i was doing sm.logit(Y_train) it was giving

You should be encountering this error in the cross entropy/log loss, the idea is to just add a small value to zero, so that divide by zero can be taken care of.