4

I found the results of score() in LinearRegression is different from r2_score(). I expected them to return the same results.The codes are as below:

r2_train = np.empty(shape=[10, 0])
r2_train_n = np.empty(shape=[10, 0])

for set_degree in range (0,10):
    pf = PolynomialFeatures(degree= set_degree)
    X_train_tf = pf.fit_transform(X_train.reshape(11,1))
    X_test_tf = pf.transform(X_test.reshape(4,1))
    lr = LinearRegression().fit(X_train_tf, y_train)

    r2_train = np.append(r2_train, r2_score(lr.predict(X_train_tf), y_train))
    r2_train_n = np.append(r2_train_n, lr.score(X_train_tf, y_train))
desertnaut
  • 57,590
  • 26
  • 140
  • 166
johnwow
  • 477
  • 1
  • 6
  • 12
  • 1
    You've mixed up the inputs to `r2_score`. Check the documentation for which positional argument should be the true values and which should be the predictions. – Nick Becker Jul 25 '20 at 02:54
  • My mistake. Thank you so much. I got a better understanding of it. – johnwow Jul 25 '20 at 03:39

2 Answers2

4

score() :- It is just comparing the error/residual in between the actual values and the predicted values.

r2_score() :- it is the value which specifies the amount of the residual across the whole dataset.

The r2 score is more robust and quite often used accuracy matrix.

It is calculated as

(r2_score = 1 - (RSS / TSS))

Where(RSS = Residual Sum of Sqaure & TSS = Total Sum of sqaure). While performing the regression by using the OLS method you should also consider the value of adjustedR2

Sudarshan
  • 702
  • 6
  • 24
2

In using r2_score, you made it:

r2_score(lr.predict(X_train_tf), y_train)

According to the documentation, the first argument should be the true values, i.e. it should be:

r2_score(y_train, lr.predict(X_train_tf))

This will give similar result with the score method in LinearRegression()

Same question here

Ayenew Yihune
  • 1,059
  • 13
  • 19