Python sci-kit learn(metrics):r2_score和explain_variance_score之间的区别?

时间:2014-06-24 04:10:55

标签: python scikit-learn

我注意到那个' r2_score'和' explain_variance_score'是回归问题的内置sklearn.metrics方法。

我总是认为r2_score是模型解释的百分比差异。它与" explain_variance_score'?

的区别如何?

你什么时候选择一个?

谢谢!

2 个答案:

答案 0 :(得分:9)

好的,看看这个例子:

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

因此,当平均残基为0时,它们是相同的。哪一个选择依赖于您的需求,即平均残留假设为0?

答案 1 :(得分:5)

我找到的大多数答案(包括此处)都强调R2Explained Variance Score之间的区别,即:平均残差(即误差均值) 。

但是,还有一个重要的问题,那就是:到底为什么我需要考虑“误差的均值”?


刷新:

R 2 :是测定系数,用于测量(最小二乘)线性回归解释的变化量。

您可以从另一个角度查看它,以评估y预测值,如下所示:

差异 actual_y × R 2 actual_y = 差异< / strong> predicted_y

从直觉上讲,R 2 越接近1,越多的Actual_y和Forecast_Y将具有相同的方差(即相同的价差< / em>)


如前所述,主要区别在于均值;如果我们看一下公式,就会发现这是真的:

R2 = 1 - [(Sum of Squared Residuals / n) / Variancey_actual]

Explained Variance Score = 1 - [Variance(Ypredicted - Yactual) / Variancey_actual]

其中:

Variance(Ypredicted - Yactual) = (Sum of Squared Residuals - Mean Error) / n 

因此,显然唯一的区别是我们要从第一个公式中减去均值误差! ... 但是为什么?


当我们将 R 2 得分解释方差得分进行比较时,我们基本上是在检查均值误差;因此,如果R 2 =解释方差得分,则意味着:平均错误=

平均误差反映了我们估计量的趋势,即:有偏诉无偏估计


摘要:

如果您希望使用无偏估计量,以使我们的模型不会低估或高估,则可以考虑考虑误差平均值