欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python scikit-learn (metrics): difference between r2_score and explained_variance_score?

程序员文章站 2022-07-24 12:09:23
I noticed that that 'r2_score' and 'explained_variance_score' are both build-in sklearn.metrics methods for regression problems. I was always under th ......

i noticed that that 'r2_score' and 'explained_variance_score' are both build-in sklearn.metrics methods for regression problems.

i was always under the impression that r2_score is the percent variance explained by the model. how is it different from 'explained_variance_score'?

when would you choose one over the other?

thanks!

 

ok, look at this example:

in [123]:
#data
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
print metrics.explained_variance_score(y_true, y_pred)
print metrics.r2_score(y_true, y_pred)
0.957173447537
0.948608137045
in [124]:
#what explained_variance_score really is
1-np.cov(np.array(y_true)-np.array(y_pred))/np.cov(y_true)
out[124]:
0.95717344753747324
in [125]:
#what r^2 really is
1-((np.array(y_true)-np.array(y_pred))**2).sum()/(4*np.array(y_true).std()**2)
out[125]:
0.94860813704496794
in [126]:
#notice that the mean residue is not 0
(np.array(y_true)-np.array(y_pred)).mean()
out[126]:
-0.25
in [127]:
#if the predicted values are different, such that the mean residue is 0:
y_pred=[2.5, 0.0, 2, 7]
(np.array(y_true)-np.array(y_pred)).mean()
out[127]:
0.0
in [128]:
#they become the same stuff
print metrics.explained_variance_score(y_true, y_pred)
print metrics.r2_score(y_true, y_pred)
0.982869379015
0.982869379015

so, when the mean residue is 0, they are the same. which one to choose dependents on your needs, that is, is the mean residue suppose to be 0?

 

most of the answers i found (including here) emphasize on the difference between r2 and explained variance score, that is: the mean residue (i.e. the mean of error).

however, there is an important question left behind, that is: why on earth i need to consider the mean of error?


refresher:

r2: is the coefficient of determination which measures the amount of variation explained by the (least-squares) linear regression.

you can look at it from a different angle for the purpose of evaluating the predicted values of y like this:

varianceactual_y × r2actual_y = variancepredicted_y

so intuitively, the more r2 is closer to 1, the more actual_y and predicted_y will have samevariance (i.e. same spread)


as previously mentioned, the main difference is the mean of error; and if we look at the formulas, we find that's true:

r2 = 1 - [(sum of squared residuals / n) / variancey_actual]

explained variance score = 1 - [variance(ypredicted - yactual) / variancey_actual]

in which:

variance(ypredicted - yactual) = (sum of squared residuals - mean error) / n 

so, obviously the only difference is that we are subtracting the mean error from the first formula! ... but why?


when we compare the r2 score with the explained variance score, we are basically checking the mean error; so if r2 = explained variance score, that means: the mean error = zero!

the mean error reflects the tendency of our estimator, that is: the biased v.s unbiased estimation.


in summary:

if you want to have unbiased estimator so our model is not underestimating or overestimating, you may consider taking mean of error into account.