我正在训练 Linear Regression() 分类器并尝试衡量其预测准确性
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 相同
您对 r2_score 的使用是错误的。第一个参数应该是真实值,而不是预测值。
根据文档:
r2_score(y_true, y_pred, ...)
所以改变这一行score2 = r2_score(y_rf,df['label'])
在您的代码中:
score2 = r2_score(df['label'], y_rf)
然后比较结果。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)