Home > Blockchain >  what is the difference between score() of KNeighborsRegressor and r2_score of sklearn.metrics
what is the difference between score() of KNeighborsRegressor and r2_score of sklearn.metrics

Time:08-05

print("Test set R2 score: {:.2f}".format(reg.score(X_test, y_test)))

from sklearn.metrics import r2_score
print(r2_score(X_test, y_test))

enter image description here

The two methods show different r2 score values.

CodePudding user response:

You probably meant r2_score(y_test, reg.predict(X_test)).

sklearn.metrics.r2_score knows nothing of your model, so passing X to it is meaningless.

CodePudding user response:

As we know in machine learning different model have different metrics. In our case it is regression model so basically for example, a least-squares regression classifier would have a score method that returns something like the sum of squared errors. The score always return the mean accuracy of the given test data and labels So this is all about score() method in knn-regressor and other regression problem as well.

The r2_score is a accuracy metrics which is calculated by the specific formula i.e R2_Score=1-(RSS/TSS) where RSS--> residual sum of square and TSS--> Total sum of squares. It lies between in the range of 0 to 1 while 0 is worst and 1 is best. Rss can be calculated by squaring actual-predicted value while tss value can be calculated by squaring actual value-mean of the sample value and if u calculate these values you will get r2_score for your regression model. So basically this is the main difference for score() and r2_score()

  • Related