1
$\begingroup$

I'm new to ML and I'm trying to create a linear regression model. My data consist of 100 samples with 4 features each. This is my humble code

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=80)


reg = LinearRegression()
reg.fit(X_train, y_train)
score_test = reg.score(X_test, y_test)
score_train = reg.score(X_train, y_train)
print("Train Score is :", score_train)
print("Test Score is :", score_test)

The problem is that the score in Test set (0.97) is way bigger than the score in Train set (0.71). How we can explain this?

Thank you in advance :)

$\endgroup$
0
$\begingroup$

Have you perhaps misinterpreted what "score" is meant to be doing? From the sci kit learn website:

.score( ) Returns the coefficient of determination R^2 of the prediction

It is not looking at the accuracy measure of your regression, but the R^2 value of your regression fit of your linear regression on your training data set. It doesn't make sense to use it again on the testing data (that's not how R^2 is supposed to work).

$\endgroup$
  • $\begingroup$ But shouldn't the R^2 score in the train set be bigger than R^2 score in the test set?? $\endgroup$ – Don Aug 28 at 18:35
  • $\begingroup$ Please refer to this anawer. I think it may help in your understanding stats.stackexchange.com/a/320519/117574 $\endgroup$ – pche8701 Aug 28 at 18:50
  • $\begingroup$ Also imagine if your test set were two data points, and these two points completely lie on your model regression line by chance. It would have an R^2 value of 1. $\endgroup$ – pche8701 Aug 28 at 18:52
  • $\begingroup$ Also if you are happy with my answer please dont forget to mark it as accepted by using the tick :) $\endgroup$ – pche8701 Aug 28 at 18:53

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.