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))
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With