【发布时间】:2016-03-04 05:49:03
【问题描述】:
我一直在使用随机森林进行分类任务。我读过一些参考资料,提到如果更多的树更好,我们也可以使用OOB error rate 在将树木添加到森林时获得分类错误的运行无偏估计。
但是,通过使用OOB error rate,我仍然无法确定随机森林中的最佳树木数量,因为我们应该设置将被评估的最小和最大树木数量的范围。因为,如果可以在设定的范围之外找到最佳数量的树,这是可能的。在这里,我需要您的高级建议,如何让 OOB 错误率准确返回随机森林中的最佳树数。以下是使用OOB错误率的代码,具有特定范围的最小和最大树数(10到100):
import matplotlib.pyplot as plt
from collections import OrderedDict
from sklearn.ensemble import RandomForestClassifier
ensemble_clfs = [ ("RandomForestClassifier, max_features=None",RandomForestClassifier(warm_start=True, max_features=None, oob_score=True,))]
error_rate = OrderedDict((label, []) for label, _ in ensemble_clfs)
min_estimators = 10
max_estimators = 100
for label, clf in ensemble_clfs:
for i in range(min_estimators, max_estimators + 1):
clf.set_params(n_estimators=i)
clf.fit(X, Y)
oob_error = 1 - clf.oob_score_
error_rate[label].append((i, oob_error))
for label, clf_err in error_rate.items():
xs, ys = zip(*clf_err)
plt.plot(xs, ys, label=label)
plt.xlim(min_estimators, max_estimators)
plt.xlabel("n_estimators")
plt.ylabel("OOB error rate")
plt.legend(loc="upper right")
plt.show()
结果:
【问题讨论】:
标签: python scikit-learn classification