Python - Random Forest - Iteratively adding trees

I am performing a machine learning task in Python. I need to create a RandomForest, and then build a graph that shows how the quality of the training and test samples depends on the number of trees in Random Forest. Do I need to create a new random forest every time with a certain number of trees? Or can I somehow add the trees iteratively (if possible, can you give a code example on how to do this)?

+4
source share
1 answer

You can use the warm start parameter RandomForestClassifier to do just that.

Here is an example that 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 is what I got:

Learning curve i got

+8
source

All Articles