3

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
  • Is this scikit-learn? Commented Nov 18, 2020 at 18:07
  • @ahiijny Yes, from sklearn.linear_model import LinearRegression Commented Nov 19, 2020 at 13:08

2 Answers 2

5

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 
Sign up to request clarification or add additional context in comments.

Comments

-3

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

6 Comments

This is not true. The intercept is the value at 0. No intercept just means that the line must go through (0, 0).
If the line goes through (0,0), the intercept would be 0, which is different from having no intercept.
?? A linear model is wx + b. No intercept means we just have wx. This is the same as b = 0.
This is a meaningless technicality. It is obvious that the question is asking about forcing the intercept to be 0.
In colloquial speech, people tend to use "0" and "none"/"nothing" interchangeably. It's perhaps slightly sloppy terminology, but I think this is what the OP meant. Perhaps another way to look at it is: A linear model with 0-intercept would have no intercept term in the linear model equation. On an unrelated note, I'm reminded of a joke: "Did you hear about the mathematician who's afraid of negative numbers? They'll stop at nothing to avoid them!" :-)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.