Quantitative Analysis · Data Science · Machine Learning

Steps to reduce overfitting a model using RFC

Overfitting is a common issue in machine learning where a model becomes excessively tailored to the training data, losing its ability to generalize well to unseen data. In the context of finance and using machine learning models like the Random Forest Classifier (RFC), several factors contribute to the susceptibility of overfitting:

  • Limited data: Financial data can be limited, especially when dealing with specific market conditions or rare events. Insufficient data points can make it challenging to capture the true underlying patterns, leading to overfitting.
  • Noise and outliers: Financial datasets often contain noise, outliers, or unpredictable events that can mislead the model. Overfitting can occur when the model excessively adjusts to these anomalies, resulting in poor performance on new data.
  • High dimensionality: Financial datasets typically involve numerous variables, such as stock prices, economic indicators, or sentiment analysis. When the number of features is high relative to the number of data points, the risk of overfitting increases as the model may find spurious relationships between variables.
  • Parameter tuning: Machine learning models have various hyperparameters that need to be tuned for optimal performance. If the hyperparameters are not carefully chosen, the model may end up overfitting the training data, failing to generalize well to new data.
  • Data snooping bias: In finance, extensive experimentation and testing of models can lead to data snooping bias. When multiple models and variations are evaluated on historical data, the chosen model may appear to perform well by chance, but it may fail to perform as expected on unseen data.

To mitigate overfitting in machine learning models like RFC in finance, several techniques can be employed such as:

  • Cross-validation: Using techniques like k-fold cross-validation helps in assessing model performance on multiple subsets of data, reducing the risk of overfitting.
  • Regularization: Applying regularization techniques like L1 or L2 regularization can help prevent overfitting by adding penalty terms to the model’s objective function, discouraging overly complex solutions.
  • Feature selection: Careful feature selection or dimensionality reduction techniques, such as principal component analysis (PCA), can help eliminate irrelevant or redundant variables, reducing the risk of overfitting.
  • Early stopping: Monitoring the model’s performance on a validation set during training and stopping the training process when the performance starts to deteriorate can prevent overfitting.
  • Ensemble methods: Using ensemble methods like bagging or boosting can improve the generalization capability of the model by combining predictions from multiple models, reducing the risk of overfitting.

Overall, being aware of the challenges and employing appropriate techniques can help mitigate the problem of overfitting in machine learning models like RFC when applied in the field of finance. To prevent overfitting in a Scikit-learn random forest classifier, you can adjust several parameters that control the complexity and generalization of the model. Below are some key parameters you can tune to reduce overfitting:

  • n_estimators: This parameter determines the number of trees in the random forest. Increasing the number of trees generally helps to reduce overfitting. However, there is a trade-off in terms of computational cost. (* See note below)
  • max_depth: Limiting the maximum depth of each tree can prevent them from becoming too complex and overfitting the data. You can set an appropriate maximum depth based on the size and complexity of your dataset.
  • min_samples_split: This parameter sets the minimum number of samples required to split an internal node. Increasing this value can help prevent the tree from splitting too early and capturing noise in the data.
  • min_samples_leaf: This parameter sets the minimum number of samples required to be at a leaf node. Increasing this value helps to control the depth of the tree and prevents the model from creating leaves with very few samples, which can lead to overfitting.
  • max_features: This parameter controls the number of features to consider when looking for the best split at each node. Reducing the number of features can make the model less prone to overfitting by reducing the complexity of individual trees.
  • bootstrap: By default, random forests use bootstrapping (sampling with replacement) to create subsets of the data for each tree. You can experiment with setting this parameter to False to train each tree on the entire dataset, which can help reduce overfitting.
  • random_state: This parameter allows you to set a seed value for the random number generator. Setting a specific random_state ensures reproducibility and allows you to compare different model configurations more accurately.

It’s important to note that the optimal parameter values for your specific problem may vary. To find the best combination of parameters, you can use techniques such as cross-validation or grid search, which help evaluate the model’s performance on different parameter settings. The default parameter settings shipped with Sklearns’ Random forest classifier provide a balanced starting point for the random forest classifier, however you can further fine-tune these values based on your specific dataset and problem.

If your model exhibit overfit behavior, it might be a good idea to drastically reduce model complexity and then gradually increase complexity to find a good balance in fitting your model data.

Steps to decrease overfitting and encourage underfitting in a (RFC) random forest classifier:

  1. Increase n_estimators: Increase the number of trees in the forest. More trees reduce the complexity and potential overfitting. For example, you can try setting n_estimators to a higher value like 50 or 100. (* See note below)
  2. Decrease max_depth: Limit the maximum depth of each tree. By reducing the depth, you restrict the model’s capacity to learn complex relationships. Setting max_depth to a small value like 3 or 5 can promote underfitting.
  3. Increase min_samples_split: Raise the minimum number of samples required to split an internal node. This makes it harder for the model to create more complex decision boundaries and can help prevent overfitting. For instance, you can try setting min_samples_split to a larger value like 10 or 20.
  4. Increase min_samples_leaf: Raise the minimum number of samples required to be at a leaf node. This constraint prevents the model from creating leaves with very few samples, reducing the chances of overfitting. You can try setting min_samples_leaf to a higher value like 5 or 10.
  5. Decrease max_features: Limit the number of features to consider when searching for the best split at each node. By reducing the number of features, you reduce the model’s capacity to overfit. You can try setting max_features to a smaller value like ‘sqrt’ (square root of the total number of features) or ‘log2’ (log base 2 of the total number of features).
  6. Set bootstrap to False: Instead of using bootstrapping, you can train each tree on the entire dataset. This reduces randomness and may decrease overfitting. Set bootstrap=False to disable bootstrapping.

from sklearn.ensemble import RandomForestClassifier
# Create the classifier object
rf_classifier = RandomForestClassifier(
n_estimators=100, # Increase this value if computational resources permit
max_depth=None, # or a specific value based on the complexity of your dataset
max_features='auto', # or a specific value, e.g., 'sqrt' or an integer
random_state=42 # Set a specific seed value for reproducibility

* Special Note: n_estimators

Reducing the number of trees in a Random Forest Classifier (RFC) can have an impact on both overfitting and underfitting. Let’s explore how each scenario may be affected:


Random Forests are known for their ability to handle overfitting due to their ensemble nature. They aggregate predictions from multiple decision trees, which reduces the likelihood of overfitting on the training data. Each tree in the forest contributes to the final prediction, and by averaging or voting across the trees, the ensemble model can generalize better to unseen data.

When you reduce the number of trees in the forest, you decrease the complexity of the model. With fewer trees, the model becomes less capable of capturing intricate relationships and patterns in the data. Consequently, the risk of overfitting may increase, especially if the original forest had a large number of trees that contributed to the model’s ability to generalize well. Therefore, reducing the number of trees in the forest might make the model more prone to overfitting, leading to decreased performance on unseen data.


Underfitting occurs when a model fails to capture the underlying patterns and relationships in the data, resulting in poor performance on both training and test sets. In the context of a Random Forest Classifier, reducing the number of trees can potentially increase the underfitting risk.
Random Forests achieve robustness by combining the predictions of multiple diverse trees. By having a larger number of trees, the model can capture a wider range of patterns and increase its overall flexibility. With fewer trees, the model’s capacity to capture complex relationships decreases, potentially leading to an underfitting scenario. The model may struggle to fit the training data adequately and might not generalize well to new, unseen samples.

It’s important to strike a balance when choosing the number of trees in a Random Forest Classifier. Ideally, you want to have enough trees to capture the underlying patterns in the data without overfitting. The optimal number of trees depends on the dataset and can be determined through techniques such as cross-validation or using performance metrics like out-of-bag error estimates.


Leave a reply