python - 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
有人可以解释一下这个分数吗?
谢谢!
解决方案
clf.score
不返回 OOB 分数,而是返回训练数据上的分数。
通过该clf.oob_score_
方法访问 OOB 分数。
推荐阅读
- java - 如何修复 java 中的“无法将套接字转换为 TLS”错误
- c# - 解析 AppContainer 中的 viewModelType 时出现 Autofac.Core.DependencyResolutionException
- ios - 有没有办法让自定义约束在界面生成器中工作?
- javascript - reactjs中设置状态时如何使用三元运算
- javascript - 点击按钮扫描到elementbyid?
- vue.js - 使用目标 wc 构建选项时,vue-cli 不会将代码转换为 es5 或 commonjs
- javascript - 引导卡不会始终弯曲到全宽
- c# - 如何在 Angular 2 和 .net 核心中制作进度条
- angular - Angular 8:对象不支持属性或方法“包含”
- bash - 在服务器之间复制文件