How the number of trees to be generated in Random Forest algorithm?

The default number of trees to be generated is 10. But I thought it should be a very large number and I put 500 trees. However it performed better when the number of trees are 10 than 500. My question is How to determine the number of trees to be generated in Random Forest algorithm?

1 Answer

The number of estimators in Random Forest is a hyper-parameter. If you are using SKLearn's Random Classifier you can use one of the following techniques to find a (near) optimal hyperparameter settings (Note:You can tweak other hyperparameters like min_leaf_size etc as well with this approach);

GridSearchCV You can specify a grid of all the hyperparameters and a scoring criteria. This function will then evaluate all combinations of these parameters for you and return the setting which performed best on the validation set.

RandomSearchCV You can specify a grid of all the hyperparameters and a scoring criteria. This function will then evaluate n randomly choosen combinations of these parameters for you and return the setting which performed best on the validation set.

Bayesian Optimization You can treat the hyperparameter settings and corresponding score as a black box function and use exploitation-exploration paradigm using bayesian optimization.