# limit regression prediction to positive (Sklearn)

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?

• 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. – Michael R. Chernick Jan 20 '17 at 4:20
• 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}$. – Matthew Gunn Jan 20 '17 at 4:21
• 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. – Nick Cox Jan 20 '17 at 13:49