sklearn.metrics.r2_score

r2_score函数是计算 R^{2} (决定系数coefficient of determination)的。

决定系数,有的翻译为判定系数,也称为拟合优度。是相关系数的平方。表示可根据自变量的变异来解释因变量的变异部分。如某学生在某智力量表上所得的 IQ 分与其学业成绩的相关系数 r=0.66,则决定系数 R^{2} =0.4356,即该生学业成绩约有 44%可由该智力量表所测的智力部分来说明或决定。

r2_score函数提供了一种衡量模型预测未来样本的可能性的方法。

如果 \hat{y^{i}} 表示第i个样本的预测值,y^{i} 是对应的真实值,R^{^{2}} 被定义为:

sklearn.metrics.r2_score_第1张图片

 

函数:

sklearn.metrics.r2_score(y_true, y_pred, sample_weight=None, multioutput=’uniform_average’)

 最好的可能得分是1.0,它可以是负数(因为模型可以是任意差的)。一个常数模型总是预测y的期望值,无视输入特征,会得到一个R^{^{2}} 得分为0.0分。

Parameters:

y_true : array-like of shape = (n_samples) or (n_samples, n_outputs)

真实标签值。

y_pred : array-like of shape = (n_samples) or (n_samples, n_outputs)

预测值。

sample_weight : array-like of shape = (n_samples), optional

样本权重。

multioutput : string in [‘raw_values’, ‘uniform_average’, ‘variance_weighted’] or None or array-like of shape (n_outputs)

Defines aggregating of multiple output scores. Array-like value defines weights used to average scores. 默认是 “uniform_average”.

 

‘raw_values’ : Returns a full set of scores in case of multioutput input.

 

‘uniform_average’ : Scores of all outputs are averaged with uniform weight.

 

‘variance_weighted’ : Scores of all outputs are averaged, weighted by the variances of each individual output.

 

Changed in version 0.19: Default value of multioutput is ‘uniform_average’.

Returns:

z : float or ndarray of floats

The R^{2} score or ndarray of scores if ‘multioutput’ is ‘raw_values’.

示例:

>>> from sklearn.metrics import r2_score
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> r2_score(y_true, y_pred)  
0.948...
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
>>> y_pred = [[0, 2], [-1, 2], [8, -5]]
>>> r2_score(y_true, y_pred,
...          multioutput='variance_weighted') 
0.938...
>>> y_true = [1, 2, 3]
>>> y_pred = [1, 2, 3]
>>> r2_score(y_true, y_pred)
1.0
>>> y_true = [1, 2, 3]
>>> y_pred = [2, 2, 2]
>>> r2_score(y_true, y_pred)
0.0
>>> y_true = [1, 2, 3]
>>> y_pred = [3, 2, 1]
>>> r2_score(y_true, y_pred)
-3.0

 

参考:https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html

你可能感兴趣的:(sklearn)