简体   繁体   English

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

[英]Python - Random Forest - Iteratively adding trees

I am doing some machine learning task on Python. 我正在Python上执行一些机器学习任务。 I need to build RandomForest and then build a graph that will show how the quality of the training and test samples depends on the number of trees in the Random Forest. 我需要构建RandomForest,然后构建一个图形,该图形将显示训练和测试样本的质量如何取决于随机森林中树木的数量。 Is it necessary to build a new Random Forest each time with a certain number of trees? 每次有一定数量的树木是否有必要建造一个新的随机森林? Or I can somehow iteratively add trees (if it possible, can you give the example of code how to do that)? 或者,我可以以某种方式迭代添加树(如果可能的话,可以举代码示例的方式)吗?

You can use the warm start parameter of the RandomForestClassifier to do just that. 您可以使用RandomForestClassifierwarm start参数来执行此操作。

Here's an example you can adapt to your specific needs: 这是您可以适应特定需求的示例:

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')

Here's what I got: 这是我得到的:

我得到的学习曲线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM