I am using Python Linear Regression to predict the weekly orders for a food deliver company. But some of my orders are coming out as negative. Is there any way to restrict the predicted values to be greater than 0,i.e positive?
Use a generalized linear model which allows you to account for this. I do not recommend discretizing your continuous variable, you will lose a ton of information doing this and the size of the bins are arbitrary.
A standard linear model can be rephrased as a generalized linear model under a normal distribution and identity link function. Simply switch the link function to log, and derive your predictions = exp(XB), where XB is the linear predictor. Alternatively, change the distribution to a distribution with support on [0, infinity] like the gamma or inverse gaussian and use a log link. If you have count data (1 order, 2 orders, 3 orders, ...) rather than a fully continuous variable (550.567 dollars of orders, 300.234 dollars of orders, ...) then use a generalized linear model with a Poisson/log link instead.
As another alternative, log your predictor and assume Y = ln(target) follows a normal distribution. Then exp(Y) follows a lognormal distribution. That is, log your target variable, predict the logged target variable (using a standard linear model, normal/identity link usually) and then use exponentiation to get back your predictions. Note, however, that this might introduce some bias when you convert back to the standard unit. You can correct this by calculating the sample variance of your logged target variable in your training set and then letting your final prediction = exp(XB + variance^2/2). This comes from the expected value of a lognormal distribution.
You could categorize your target values. For example,
- 1-3 orders
- 4-5 orders
- 6-7 orders ...
Then you could execute a regression with categorical variables.
Categorical variables (also known as factoror qualitative variables) have a limited number of different values, called levels.
Regression analysis requires numerical variables, so the categorical variables are recoded into a set of separate binary variables. This recoding is called “dummy coding” and can be done automatically in Python.