【问题标题】:Difference between model score() vs r2_score模型 score() 与 r2_score 之间的差异
【发布时间】:2018-01-13 18:08:54
【问题描述】:

我正在训练一个 LinearRegression() 分类器并试图衡量它的预测准确度

from sklearn.metrics import r2_score
from sklearn.linear_model import LinearRegression
regr_rf = LinearRegression()

regr_rf.fit(df[features],df['label'])
y_rf = regr_rf.predict(df[features])
score = regr_rf.score(df[features],df['label'])
print score
score2 = r2_score(y_rf,df['label'])
print score2

score 和 score2 都显示出非常不同的值。我虽然假设模型的分数函数与明确计算的 r2_score 相同

【问题讨论】:

    标签: scikit-learn sklearn-pandas


    【解决方案1】:

    您对 r2_score 的使用是错误的。第一个参数应该是真实值,而不是预测值。

    根据the documentation

    r2_score(y_true, y_pred, ...)
    

    因此,将代码中的这一行 score2 = r2_score(y_rf,df['label']) 更改为:

    score2 = r2_score(df['label'], y_rf)
    

    然后比较结果。

    【讨论】:

    • 感谢 Vivek,这确实是问题所在。
    猜你喜欢
    • 2020-06-01
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多