简体   繁体   English

线性回归中 r2_score 和 score() 的区别

[英]Difference between r2_score and score() in linear regression

I found the results of score() in LinearRegression is different from r2_score().我发现 LinearRegression 中 score() 的结果与 r2_score() 不同。 I expected them to return the same results.The codes are as below:我希望他们返回相同的结果。代码如下:

r2_train = np.empty(shape=[10, 0])
r2_train_n = np.empty(shape=[10, 0])

for set_degree in range (0,10):
    pf = PolynomialFeatures(degree= set_degree)
    X_train_tf = pf.fit_transform(X_train.reshape(11,1))
    X_test_tf = pf.transform(X_test.reshape(4,1))
    lr = LinearRegression().fit(X_train_tf, y_train)

    r2_train = np.append(r2_train, r2_score(lr.predict(X_train_tf), y_train))
    r2_train_n = np.append(r2_train_n, lr.score(X_train_tf, y_train))

score() :- It is just comparing the error/residual in between the actual values and the predicted values. score() :- 它只是比较实际值和预测值之间的误差/残差。

r2_score() :- it is the value which specifies the amount of the residual across the whole dataset. r2_score() :- 它是指定整个数据集的残差量的值。

The r2 score is more robust and quite often used accuracy matrix. r2 分数更稳健,并且经常使用准确度矩阵。

It is calculated as它计算为

(r2_score = 1 - (RSS / TSS)) (r2_score = 1 - (RSS / TSS))

Where(RSS = Residual Sum of Sqaure & TSS = Total Sum of sqaure).其中(RSS = Sqaure 的剩余总和 & TSS = sqaure 的总和)。 While performing the regression by using the OLS method you should also consider the value of adjustedR2在使用 OLS 方法执行回归时,您还应该考虑adjustedR2的值

In using r2_score, you made it:在使用 r2_score 时,您做到了:

r2_score(lr.predict(X_train_tf), y_train)

According to the documentation , the first argument should be the true values, ie it should be:根据文档,第一个参数应该是真实值,即它应该是:

r2_score(y_train, lr.predict(X_train_tf))

This will give similar result with the score method in LinearRegression()这将给出与 LinearRegression() 中的 score 方法相似的结果

Same question here同样的问题在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM