11

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

David
  • 4,634
  • 7
  • 35
  • 42

1 Answers1

15

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.

Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132