Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between model score() vs r2_score

I am training a LinearRegression() classifier and trying to gauge its prediction accruacy

from sklearn.metrics import r2_score
from sklearn.linear_model import LinearRegression
regr_rf = LinearRegression()

regr_rf.fit(df[features],df['label'])
y_rf = regr_rf.predict(df[features])
score = regr_rf.score(df[features],df['label'])
print score
score2 = r2_score(y_rf,df['label'])
print score2

both score and score2 are showing very different value. I though the score function of the model is suppose to be the same as r2_score calculated explicitly

like image 206
David Avatar asked Mar 30 '26 00:03

David


1 Answers

Your usage of r2_score is wrong. First argument should be true values, not the predicted values.

According to the documentation:

r2_score(y_true, y_pred, ...)

So change this line score2 = r2_score(y_rf,df['label']) in your code to:

score2 = r2_score(df['label'], y_rf)

And then compare the results.

like image 107
Vivek Kumar Avatar answered Apr 02 '26 11:04

Vivek Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!