Implementation: Effect of Number of Trees in Random Forests
The number of trees in a random forest signifies the total number of decision trees actually used in a random forest algorithm for prediction purposes. Let’s get into the coding part to compare how the increase in the value of number of trees affect the prediction ability of the random forest algorithm.
For showcasing this example we will be using the above dataset and code as it is. Let’s follow the steps given next in order to check how the number of trees affect the model.
Creating Models With Different Number of Trees
Here we will be creating two new models named ‘model_3’ and ‘model_4’ as ‘model_1’ and ‘model_2’ were created previously.
The third model contains 5 trees while the fourth model contains 500 trees in them. After the first step we will be fitting the training data to the third and the forth model.
# Creating two different models with different number of trees
model_3 = RandomForestClassifier(n_estimators = 5, random_state=42)
model_4 = RandomForestClassifier(n_estimators = 500, random_state=42)
# Training the models with the training data
model_3.fit(x_train, y_train)
model_4.fit(x_train, y_train)
Predicting Outcomes
# Predicting the test data with the help of trained models
model_3_pred = model_3.predict(x_test)
model_4_pred = model_4.predict(x_test)
# Measuring the accuracy score of the third and the fourth model
model_3_acc = accuracy_score(model_3_pred, y_test)
model_4_acc = accuracy_score(model_4_pred, y_test)
print(f'Accuracy of Third model: {model_3_acc}\nAccuracy of Fourth model: {model_4_acc}')
Output:
Accuracy of Third model: 0.6241299303944315
Accuracy of Fourth model: 0.7045630317092034
Here we can see that as we increase the number of trees from 50 to 500 in a random forest model the performance of the model is increased by 8 percent.
The Effects of the Depth and Number of Trees in a Random Forest
Random forests, powerful ensembles of decision trees, benefit from tuning key parameters like tree depth and number of trees for optimal prediction and data modeling.
In this article, we will be discussing the effects of the depth and the number of trees in a random forest model.
Table of Content
- Random Forest
- Understanding the Impact of Depth and Number of Trees in Random Forests
- Effect of depth in a Random Forest: Implementation
- Effect of Number of Trees in Random Forests : Implementation