Is there a way to force python to form a linear regression with intercept equal to 0?

Viewed 280

I have a dataset including two columns age ans flexibility variables. The following plot displays the corrolation between the age of people and their body flexibility based on my dataset:

enter image description here

I am trying to make a cubic model where flexibility depends on the age cubed. So I have done:

from sklearn.linear_model import LinearRegression

df["Age_cubed"] = df["Age"].pow(3)
X = df[["Age_cubed"]]
Y = df["Flexibility"]

model = linear_model.LinearRegression()
model.fit(X, Y)
r_sq = model.score(X, Y)

model.coef_  # 10.02
model.score(X, Y) # 0.93

Now the corresponding plot is: enter image description here

This model has an interocept of 0.034:

print(model.intercept_) # 0.034

Is there a way to force python to form the above linear regression model with intercept equal to 0?

2 Answers

Yes, there is a way if you set the value of fit_intercept to False:

model = linear_model.LinearRegression(fit_intercept=False)

Then when you print the intercept:

print(model.intercept_)

The output is:

0.0

Unless the line is vertical, there must be an intercept.

Related