# Why do you set 1 as intercept in linear regression model in python?

I'm learning linear regression in Udacity as a beginner. I know statsmodels.regression.linear_model.OLS() needs an intercept but why do you set 1 as intercept?

Even though we set that value, the fit result shows different values as the intercept.

Then what does setting 1 mean? Also do we usually use 1 for this setting?

I don't know the python function/method you are referring to. But you may be confusing that the 1 you add is to your variables/feature such that it is multiplied by the intercept parameter in your parameter vector. In other words, 1 is rather added to your features and is NOT the value of your intercept.

• Thank you for your answer and you're right about I'm confused. Then any number is ok instead of 1? Mar 8 '19 at 0:28
• Technically you could use any number, but it is convention to use 1. It makes things nicer.
– Tom
Mar 8 '19 at 10:21
• I see...! My mind became clear! Thank you for your help. Mar 8 '19 at 14:51
• You can't use zero for an intercept...
– Sycorax
Sep 1 '20 at 5:33
• @Yuki.U If you use a number different from 1, interpretation becomes much harder. Intercept often is the "baseline". So if you use 1, the beta0 indicates the baseline level. If you would use, e.g. 0.5, then beta0 would be twice the baseline. So you would need to calculate what the actual baseline is, if you are interested in that value. For just predictions or significance testing, it doesn't matter at all. Sep 1 '20 at 8:25

I do not know Python, but as you can readily illustrate in R, setting the value of the intercept to 1 is really just a convention (a useful one, though, of course, allowing us to interpret the intercept as the expected effect when $$x=0$$).

n <- 10
y <- rnorm(n)             # some random data
x <- rnorm(n)
intercept <- rep(1,n)     # a "hand-made" intercept

lm(y~x)                   # the default in R which includes an intercept
lm(y~intercept+x-1)       # removing the default intercept with -1 and re-adding it manually as another regressor
lm(y~I(2*intercept)+x-1)  # removing the default intercept with -1 and re-adding 2 as a constant term


Output:

> lm(y~x)

Call:
lm(formula = y ~ x)

Coefficients:
(Intercept)            x
-0.07813      0.55086

> lm(y~intercept+x-1)

Call:
lm(formula = y ~ intercept + x - 1)

Coefficients:
intercept          x
-0.07813    0.55086

> lm(y~I(2*intercept)+x-1)

Call:
lm(formula = y ~ I(2 * intercept) + x - 1)

Coefficients:
I(2 * intercept)                 x
-0.03907           0.55086


As you can see, the first two regressions are exactly the same (as fully expected), and the third has the same coefficient on x, and exactly half the coefficient on the constant term, to account for the effect that we have multiplied that by two.