Python - 随机森林 - 迭代添加树

Alc*_*one 9 python random-forest scikit-learn

我在Python上做一些机器学习任务.我需要构建RandomForest,然后构建一个图表,显示训练和测试样本的质量如何取决于随机森林中的树木数量.是否有必要每次使用一定数量的树木建立一个新的随机森林?或者我可以以某种方式迭代地添加树(如果可能的话,你能给出代码的例子如何做到这一点)?

ldi*_*rer 15

您可以使用该warm start参数RandomForestClassifier来做到这一点.

这是一个可以适应您特定需求的示例:

errors = []
growing_rf = RandomForestClassifier(n_estimators=10, n_jobs=-1,  
                                    warm_start=True, random_state=1514)
for i in range(40):
    growing_rf.fit(X_train, y_train)
    growing_rf.n_estimators += 10
    errors.append(log_loss(y_valid, growing_rf.predict_proba(X_valid)))

_ = plt.plot(errors, '-r')
Run Code Online (Sandbox Code Playgroud)

这是我得到的:

我得到的学习曲线