我正在做一个简单的线性模型.我有
fire = load_data()
regr = linear_model.LinearRegression()
scores = cross_validation.cross_val_score(regr, fire.data, fire.target, cv=10, scoring='r2')
print scores
Run Code Online (Sandbox Code Playgroud)
产量
[ 0.00000000e+00 0.00000000e+00 -8.27299054e+02 -5.80431382e+00
-1.04444147e-01 -1.19367785e+00 -1.24843536e+00 -3.39950443e-01
1.95018287e-02 -9.73940970e-02]
Run Code Online (Sandbox Code Playgroud)
这怎么可能?当我对内置的糖尿病数据做同样的事情时,它的效果非常好,但对于我的数据,它会返回这些看似荒谬的结果.我做错了什么吗?
为什么r2_scorescikit-learn中的函数与维基百科中描述的确定系数公式之间存在显着差异?哪个是正确的?
我正在使用Python 3.5来预测线性和二次模型,而我正在尝试的适合度的衡量标准之一是.但是,在测试时,维基百科中提供的r2_score指标scikit-learn与计算之间存在显着差异.
我在这里提供我的代码作为参考,它计算上面链接的维基百科页面中的示例.
from sklearn.metrics import r2_score import numpy y = [1, 2, 3, 4, 5] f = [1.9, 3.7, 5.8, 8.0, 9.6] # Convert to numpy array and ensure double precision to avoid single precision errors observed = numpy.array(y, dtype=numpy.float64) predicted = numpy.array(f, dtype=numpy.float64) scipy_value = r2_score(observed, predicted) >>> scipy_value:
很明显,scipy计算值是-3.8699999999999992维基百科中的参考值0.998.
谢谢!
更新:这与关于如何在scikit-learn中计算R ^ 2的问题不同,我正在努力理解并澄清两种结果之间的差异.这个问题表明scikit中使用的公式与维基百科的公式相同,不应该导致不同的值.
更新#2: …