【问题标题】:Difference between r2_score and score() in linear regression线性回归中 r2_score 和 score() 的区别
【发布时间】:2020-11-14 21:54:20
【问题描述】:

我发现 LinearRegression 中 score() 的结果与 r2_score() 不同。我希望他们返回相同的结果。代码如下:

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))

【问题讨论】:

  • 你把r2_score的输入搞混了。检查文档,哪些位置参数应该是真实值,哪些应该是预测值。
  • 我的错误。太感谢了。我对它有了更好的理解。

标签: python machine-learning scikit-learn linear-regression


【解决方案1】:

在使用 r2_score 时,你做到了:

r2_score(lr.predict(X_train_tf), y_train)

根据documentation,第一个参数应该是真值,即应该是:

r2_score(y_train, lr.predict(X_train_tf))

这将得到与 LinearRegression() 中的 score 方法相似的结果

同样的问题here

【讨论】:

    【解决方案2】:

    score() :- 它只是比较实际值和预测值之间的误差/残差。

    r2_score() :- 它是指定整个数据集的残差量的值。

    r2 分数更稳健,并且经常使用准确度矩阵。

    计算如下

    (r2_score = 1 - (RSS / TSS))

    其中(RSS = Sqaure 的剩余总和 & TSS = sqaure 的总和)。在使用 OLS 方法执行回归时,您还应该考虑 adjustedR2

    的值

    【讨论】:

    • 非常感谢。真的很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2022-08-04
    • 2015-02-14
    • 2012-08-22
    • 2023-03-29
    • 1970-01-01
    • 2022-01-01
    • 2015-03-21
    • 2018-01-13
    相关资源
    最近更新 更多