regr.score 和 r2_score 给出不同的值

regr.score and r2_score give different values

我正在使用 sklearn.linear_model.LogisticRegression 并使用它计算 R^2 值如下

regr.score(xtest, ytest)

我的分数是 0.65

现在,为了进行比较,我使用了 sklearn.metrics.r2_score¶ 提供的指标,我计算的分数如下

r2_score(ytest,regr.predict(xtest))

我的分数是 -0.54

根据文档 regr.score returns "R^2 of self.predict(X) wrt. y." 这就是我使用度量计算 R^2 所做的,但我不明白为什么这些值如此不同?

谁能帮我解释一下?

更新: 按照建议我在 r2_score 中切换了变量 ytest,regr.predict(xtest) 但在逻辑回归中我仍然得到不同的值。所以我更新了问题。

你得到不同值的原因是因为score function in LogisticRegression class by default calculates the accuracy score. The accuracy score is simply the number of correct predictions divided by the total number of predictions. On the other hand an R2 score is entirely different and you can read more about its mathematics here

希望对您有所帮助!