I have a dependent continuous variable with range 0-100 representing restaurant health violations. Due to the nature of the variable, it does not make sense for a regression equation to predict a restaurant to score negative violations. I would like to limit the prediction interval for many different regression algorithms that I am running in scikit-learn (OLS, Lasso, Ridge, Random Forest).

Other responses to this problem (example) state that "If your DV is never negative then you can take the log. Then the predicted values on the raw score would never be negative."

I used numpy to take the log of my DV and my predictions are still returning negative (I don't know why they would be different). How can I address this issue, specifically with implementation in python?

  • $\begingroup$ In the example from your link Peter Flom in his answer provides a number of solutions including the log transformation. But I do not think he intended that recommendation for a DV that can take the value 0. The log of 0 is negative infinity. Bootstrap confidence intervals provide another approach or even a Bayesian approach with a prior that can not be zero. $\endgroup$ Commented Jan 20, 2017 at 4:20
  • 1
    $\begingroup$ Quick comment: (1) If the number of restaurant health violations can be zero, taking the log is not appropriate. (2) If you did take the log, the idea is $Y_i$ is your dependent variable and let $y_i = \log Y_i$ be the log of your dependent variable. Then you run linear regression on $y_i$. The predicted values are $\hat{y_i}$ and you exponentiate to get $\hat{Y}_i = e^{\hat{y}_i}$. $\endgroup$ Commented Jan 20, 2017 at 4:21
  • $\begingroup$ Predicted logarithms can be negative; that corresponds to predictions less than 1. For a bounded variable, I'd recommend logit, not log. This is an easy option in most statistical software but I have no idea about Python. $\endgroup$
    – Nick Cox
    Commented Jan 20, 2017 at 13:49

1 Answer 1


Honestly, I do not think taking a log will always be a good idea even it can give you positive responses because it will stress more on small violations than higher violations --- small violations will have relatively higher weights in the loss in log scale than in normal scales. If this is not what you want, you probably should not use it.

And I think a simple idea is to just use the normal model and training. When it gives negative responses, set them to 0. Tune the hyperparameter based on this. I think this can give you a reasonable model.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.