0
$\begingroup$

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?

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Thanks for the solution. $\endgroup$ – Athul Vasan Jul 16 '19 at 15:40
0
$\begingroup$

You could categorize your target values. For example,

  1. 1-3 orders
  2. 4-5 orders
  3. 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.

$\endgroup$
2
  • $\begingroup$ Thanks for the solution $\endgroup$ – Athul Vasan Jul 16 '19 at 15:41
  • $\begingroup$ Appreciate the words, but usually people give an up vote for a good solution? $\endgroup$ – grldsndrs Jul 16 '19 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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