首页 > 解决方案 > RandomForest 高 OOB 分数与低 KFold 验证分数

问题描述

我一直在用 Titanic 数据集训练随机森林模型。许多文章指出我们不需要对 RF 分类器进行交叉验证,而很少有人说您可以使用交叉验证。我两种都试过了,但我不知道如何计算分数,我怀疑如果在没有交叉验证的情况下使用我的模型会过度拟合。

该模型的 oob 得分为 96.85,平均交叉验证得分为 83.27 [如果我设置 score='f1',此模型给出 74.01]

这是我的代码,

from sklearn.ensemble import RandomForestClassifier

clf = RandomForestClassifier(n_estimators=10, random_state=44, oob_score=True)

clf.fit(titanic[features], titanic['Survived'])

clf.score(titanic[features], titanic['Survived'])

score : 0.9685746352413019

predictors = features
clf = RandomForestClassifier(random_state=10, n_estimators=10)
clf.fit(titanic[features],titanic["Survived"])

kf = KFold(n_splits=10)

scores = cross_val_score(clf, titanic[predictors], titanic["Survived"], cv=kf)

print(scores.mean())
score : 83.27

有人可以解释一下这个分数吗?

谢谢!

标签: pythonmachine-learningrandom-forestensemble-learning

解决方案


clf.score不返回 OOB 分数,而是返回训练数据上的分数。

通过该clf.oob_score_方法访问 OOB 分数。


推荐阅读