Random Forest Classifier: Deep in the Machine Learning Jungle

Table of Contents


Definition and Overview of Random Forest

Have you ever been lost in a forest? Trees everywhere, different paths, and you have to find your way out! Imagine if instead of one person trying to find a way, you had a group of friends each taking a different path and then coming together to decide the best way out. That’s pretty much how a Random Forest works in machine learning. It’s not a single algorithm, but a forest of many decision trees that come together to make a final decision. Quite the team effort, right?

Random Forest is a powerful and versatile machine-learning method capable of performing both regression and classification tasks. It also undertakes dimensional reduction methods, treats missing values, outlier values, and other essential steps of data exploration, and does a pretty good job.

When and why to use Random Forest

Just like how having more heads to think is better when solving a problem, in machine learning, having more trees or models can give you a better prediction. The main reason to use Random Forest is its simplicity and the fact that it can be used for both classification and regression tasks.

Let’s say you’re playing a game where you have to guess an object based on clues. Wouldn’t you have a better chance of guessing right if you had multiple friends giving their ideas instead of just one? The same logic applies to Random Forests. They give you a better prediction because they combine the output of multiple decision trees to give a final verdict.

Image Credit: DataDrive


Recap of Decision Trees

Remember our previous article on Decision Trees? Let’s take a quick trip down memory lane. A Decision Tree is like a game of “20 Questions.” It asks a series of questions, and each answer leads to a set of further questions until finally, it arrives at a prediction. The decisions (or questions) are made based on the features in the data. However, a Decision Tree has its limitations. It tends to overfit on a dataset, which basically means it’s too reliant on the training data and performs poorly on new, unseen data. And that’s where our savior, Random Forest, comes in!

Introduction to Bagging

Before we venture deep into the Random Forest, let’s talk about a key concept used in it: Bagging. Imagine you’re practicing archery. Instead of just one shot to hit the target, you get multiple shots. Some may hit, some may miss, but overall, you get a better chance of hitting the bullseye! That’s what bagging is in machine learning. It stands for Bootstrap Aggregation. It’s a technique used to reduce variance in a decision tree. Here, many subsets of the original dataset are created, a model is built for each, and finally, the model results are aggregated. The outcome? A better, more accurate model!

Explanation of variance and bias, and how Random Forest balances them

In archery, if most of your arrows are hitting the same spot but far from the bullseye, you have a high bias problem. If your arrows are all over the target, you have a high variance problem. Similarly, in machine learning, bias is when your model makes strong assumptions about the data and misses the target. Variance is when your model is overly sensitive and hits all over the target.

A Random Forest handles the balance between bias and variance very well. Each decision tree in the Random Forest has high variance but low bias. But because we’re averaging all these trees in the Random Forest, we are essentially averaging the variance as well — thus creating a low bias and low variance model.


Description of the tree-building process

Imagine you’re planning a picnic and you have a big group of friends to help you. You ask each friend for their opinion on where to go, what food to bring, what games to play, etc. Each friend gives you a plan (like a decision tree), based on their knowledge and taste. You then pick the most common suggestions from all plans, to make sure everyone will enjoy the picnic. That’s essentially what Random Forest does, it combines lots of decision trees to make a final decision. Here’s how it works: In Random Forest, we grow many trees (not just one like in Decision Trees). But, just like your friends don’t know everything, each tree in a Random Forest gets to see only a random subset of the data. And when it’s splitting a node, it’s allowed to choose from a random subset of the features. The tree-building continues until it reaches a stopping condition, such as no more features to split on, reaching the maximum depth, or each leaf node having a minimum number of samples. For an image, consider showing a few decision trees separately first, then combine them together in a forest. You could use a graphic of a literal forest to illustrate this concept, with each tree representing a decision tree, and the forest as a whole representing the Random Forest.

Explanation of how Random Forest makes predictions

Now that we have our forest, let’s see how it makes predictions. Going back to the picnic example, once you get all the suggestions from your friends, how do you make the final decision? You could take the most common suggestions, right? This is also known as “majority voting”. Random Forest does something similar.

When we want to predict a result, we let all the trees in the forest predict the result independently. Then we take the prediction that gets the most votes. For classification problems, this is called “majority voting”, and for regression problems, it’s usually the average of all the predictions.

Differences between Decision Trees and Random Forest

You might be thinking if Random Forest is just a bunch of decision trees, why not just use a single decision tree? It’s like asking one friend who you really trust for picnic ideas, instead of asking everyone. Well, the problem is that your friend, just like a decision tree, might have some biases or could be influenced by some random noise in the data. They might suggest a beach picnic because they love the beach, even though some of your friends can’t swim.

But when you ask many friends or build a forest of decision trees, the biases of each tree are likely to balance out, giving you a better overall plan. This is the main difference between Decision Trees and Random forests. Random Forest is more robust and less prone to overfitting than a single Decision Tree.

Working of Random Forest Model
Image Credit: Engineers Garage


Mathematical interpretation of randomness in tree building

Remember when we said that each tree only sees a random subset of the data and only a random subset of features? This is where the “Random” in Random Forest comes from. This randomness helps to make sure that our forest is diverse, meaning the trees are different from each other. If the trees were too similar, they would all make the same errors in prediction. But when they are different, they make different errors, and these errors cancel out, leading to better overall predictions.

For an image, consider showing two trees with different sets of data and features to split on. You can also show how errors from each tree cancel out when they are combined.

Implications of Randomness in Prediction

This randomness also has an impact on the predictions. Because each tree is trained on different data and features, they might make different predictions for the same input. This might seem like a bad thing, but it’s actually good! By having a variety of predictions, we ensure that our final prediction, which is the majority vote, is more likely to be accurate. It’s like getting advice from a diverse group of friends, which leads to a better decision. For an image, consider showing the same input data point being passed through multiple trees, each giving a different prediction, then show how these predictions are combined through voting to produce the final output.


  1. Random Forest: Picture a forest, a vast expanse of trees, each with different sizes, types, and strengths. Now, imagine these trees are not growing plants, but decision-making entities! That’s what a Random Forest in machine learning is – a collection of decision trees, each providing a different “opinion” on the data. And, just like in real life, this variety of opinions can help make better, more balanced decisions. When we put together the results from all these decision trees, we get a prediction that’s typically stronger and more reliable than the prediction from a single decision tree.
  2. Bagging: Think of a bag of jellybeans. Now imagine you randomly draw a handful of beans from the bag, then put them back in, and draw again. This method of drawing is called bagging, or bootstrap aggregating, in machine learning. It means we take random samples of our data, with replacement (which means the same data point can be picked more than once). Each decision tree in our Random Forest gets its own bag of data to make its decision, which is why each tree can have a different “opinion”!
  3. Decision Trees: A decision tree in machine learning is a lot like a game of “20 Questions.” It asks a series of yes/no questions about the data until it arrives at a prediction. For example, “Is the temperature above 75 degrees?” or “Did the team win more than 50 games?” Each question helps the decision tree get closer to the right answer. In a Random Forest, we have many decision trees, each asking their own set of questions to make a prediction.
  4. Variance and Bias: Variance refers to how much your model’s predictions could change if you used a different dataset. Bias, on the other hand, refers to how much the average prediction differs from the true value. Ideally, we want a model with low variance (it doesn’t change much with different data) and low bias (it’s close to the truth). Random Forest is good at balancing these two because it averages the predictions of many decision trees, each built with different data.
  5. Overfitting: Imagine you’re studying for a test, and you memorize every question and answer in the textbook. You might do well if the test questions are exactly the same, but you’ll struggle if they’re even slightly different. That’s what overfitting is in machine learning. A model that’s overfit has learned the training data too well and performs poorly on new, unseen data. By using many decision trees and averaging their results, Random Forest helps prevent overfitting.
  6. Feature Importance: In our “20 Questions” game, some questions help us get to the answer faster than others. Similarly, in machine learning, some features (or variables) are more important than others in making predictions. Random Forest can tell us how important each feature is, based on how much it improves the accuracy of our trees. It’s like knowing which questions to ask first in our game!


Defining a Practical Problem that can be Solved using Random Forest: Let’s say you’re the coach of a soccer team and you’re trying to predict which of your players will score the most goals in the next season. You have information like each player’s age, height, weight, position, number of years of experience, and goals scored in previous seasons. You can use a Random Forest to make this prediction!

Implementing Random Forest to Solve the Problem: To solve this problem, we’ll need to collect all the data we have on our players, then use a Random Forest to analyze it. The Random Forest will create a bunch of decision trees, each looking at the data in a slightly different way. Then, it will average the predictions of these trees to guess who will be the top scorer.

Discussing the Outcomes: After running our Random Forest, we get a list of players ranked by their predicted number of goals. We also get a measure of feature importance, which tells us which variables (like age, position, or experience) were most influential in making the prediction. This can help us understand what factors are most related to a player’s performance and inform our coaching strategy.

Other real-life examples of Random Forest use:

  • In medicine, Random Forest is used to predict diseases based on patients’ symptoms or genetics.
  • In finance, it can be used to predict whether a loan applicant is likely to default.
  • In e-commerce, Random Forest can help recommend products to customers based on their past purchases and browsing behavior.


To demonstrate the application of the Random Forest classifier, we’ll use the widely known and commonly used Iris flower dataset. This dataset is a multivariate data set introduced by the British statistician and biologist Ronald Fisher in his 1936 paper. It includes 150 samples from each of three species of Iris flowers (Iris setosa, Iris virginica, and Iris versicolor). Four features were measured from each sample: the lengths and the widths of the sepals and petals.

Why are we using the Iris dataset, you might ask? The beauty of this dataset lies in its simplicity and diversity. With only four features and three different classes, it’s relatively easy to understand. Yet, it offers enough complexity to demonstrate how Random Forest can handle multiclass classification problems.

Each class is evenly represented in the dataset, which allows our model to learn from a balanced example. Additionally, the dimensions of the Iris flowers vary enough that it can give us a good understanding of how Random Forest can handle different features and their interactions.

A sample image of the three types of Iris flowers can be found here.


Now, let’s dive into the fun part – the application of the Random Forest classifier to the Iris dataset! Follow along with these code steps:

# Step 1: Import required packages
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.datasets import load_iris

# Step 2: Load the Iris dataset
iris = load_iris()

# Step 3: Prepare the dataset
# The Iris dataset is already clean, so we just need to split it into training and test sets.
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)

# Step 4: Train the Random Forest model
# We're setting the number of trees in the forest (n_estimators) to 100
rf = RandomForestClassifier(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)

# Step 5: Make predictions with the model
y_pred = rf.predict(X_test)

# Step 6: Evaluate the predictions
# We'll use a confusion matrix and the classification report for this purpose.
print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred))

print("\nClassification Report:")
print(classification_report(y_test, y_pred))



Imagine you’re a chef, and you’ve just finished preparing a grand feast. The dishes look great and the aroma wafting off them is enticing. But, how do you know if they actually taste good? You’d have to taste them yourself, right? Or perhaps, ask others to taste them and give you feedback. The same goes for our Random Forest model. Once we’ve trained it, we need to evaluate its performance, or in other words, see how ‘tasty’ our model’s predictions are.

The Confusion Matrix and the Classification Report are like our taste-testers. They provide us with insights about how well our model is performing.

Understanding the Confusion Matrix

The Confusion Matrix is a table that shows us the number of correct and incorrect predictions made by our model. It’s called a ‘confusion’ matrix because it helps us understand where our model is getting ‘confused.’

Imagine that this confusion matrix is a scoreboard in a dart game. Each row represents the actual class, and each column represents the predicted class. We have three classes (0, 1, 2), so we have three rows and three columns.

The main diagonal (from the top left to bottom right) of the matrix contains the correct predictions. Our model predicted class 0 correctly 10 times, class 1 correctly 9 times, and class 2 correctly 11 times.

The off-diagonal elements are all zeros, which means our model didn’t make any wrong predictions. This is like hitting the bullseye with every dart throw, quite an achievement for our Random Forest model!

Random Forest Model Confusion Matrix

Deciphering the Classification Report

RF Model Classification Report

Next up, we have the Classification Report. This is like a detailed scorecard that gives us deeper insights into our model’s performance.

This report shows four key metrics for each class:

  • Precision: This is like accuracy in a shooting game, where it’s important not just to hit the target, but to hit the bullseye. High precision means that when our model predicts a class, it’s highly likely to be correct.
  • Recall: This is like catching fish in a pond. High recall means our model caught most of the fish (i.e., correctly identified most of the instances of a class).
  • F1-score: This is the harmonic mean of precision and recall, providing a balanced measure of the model’s performance.
  • Support: This shows how many instances of each class we had in our test data.

In our case, all these metrics are 1.00 for each class, which means our model is doing a stellar job in predicting the classes. This is like hitting the bullseye every single time, catching all the fish in the pond, and scoring top marks in every subject!

In conclusion, these results show that our Random Forest model has performed exceptionally well on our test data. It’s as if we’ve baked the perfect cake on our first try!

But wait, before we start celebrating, let’s be cautious. While a perfect score might seem like the ultimate goal, it could also suggest that our model might be overfitting the data, meaning it might not generalize well to new, unseen data. This is something we’ll have to keep an eye on when using the model in the real world.


Let’s now compare our champion Random Forest with its precursor, the Decision Tree. Think of this as a friendly match between a legendary chess grandmaster (the Decision Tree) and a team of proficient players (Random Forest) who, while they may not each be a grandmaster, can combine their skills to become even better!

Decision Trees are simple and easy to understand. They ask a series of Yes/No questions about the data (like “Is this person older than 20?”) until they arrive at a prediction. However, Decision Trees can sometimes get over-excited and create very complex structures that overfit the data, leading to less accurate predictions on new data.

In comparison, Random Forests, while a bit more complex, can achieve better performance. How do they do this? Well, instead of relying on one Decision Tree, a Random Forest gets input from multiple decision trees, each built on a different sample of data. It’s like the difference between relying on one very opinionated person versus getting a balanced viewpoint from a group.

This group decision-making process allows Random Forest to reduce the risk of overfitting, as the errors of individual trees tend to cancel out, leading to a more generalized and robust model. However, this process is a bit of a trade-off, as Random Forests can be more difficult to interpret than single Decision Trees.

To sum up, while Decision Trees are a powerful tool with easy-to-interpret results, Random Forests provide a more balanced and generalized model, which can lead to improved prediction performance, especially on larger, more complex datasets.


Discussing the pros and cons of using Random Forest

Random Forest, like every other machine learning algorithm, comes with its own set of advantages and disadvantages. Let’s start by diving into the lush greenery of its benefits!


  • Robust to Overfitting: Random Forest’s most prominent advantage is its robustness to overfitting. Imagine you’re a jungle explorer. You’ve been told that following one path (i.e., a single decision tree) could lead you to your goal, but it might also lead you into a dangerous trap! Now imagine you have the option to consult a council of experienced explorers (i.e., a forest of decision trees). They each have their unique path, but by considering their collective wisdom, you’re likely to avoid dangers and reach your goal safely. That’s exactly how Random Forest avoids overfitting; it aggregates the results of multiple trees, reducing the risk of going astray due to noisy data.
  • High Accuracy: Random Forest is known for its high accuracy and ability to work well with large datasets. It’s like having a team of expert jungle guides. Each guide (tree) might be accurate to a certain extent, but together, their collective decision is likely more accurate than any single one.
  • Handles Unbalanced Data: Random Forest performs well with unbalanced datasets, where the number of observations belonging to one class is significantly lower than those belonging to the other classes.
  • Feature Importance: Random Forest provides a good indicator of the importance it assigns to each feature when making predictions. Think of this as each jungle guide voting on which path features (like specific landmarks or markings) are most crucial in determining the route.

However, every silver lining has a cloud, and so does the Random Forest. Let’s discuss some of its limitations.


  • Computationally Intensive: Random Forests can be quite slow and are not ideal for real-time predictions due to their complexity. It’s like trying to get all the jungle guides to come to a consensus; it takes time!
  • Less Interpretability: A single decision tree is simple to understand because we can visualize its decisions. But with a whole forest of trees, this becomes much more difficult. It’s like trying to understand the thoughts of each guide in a large team – not a simple task!
  • Noisy Classification: While Random Forests are great for classification problems, they don’t perform as well with noisy classification/regression tasks.
  • Hyperparameters: Random Forest requires tuning a number of parameters and it’s not always clear what values will work best until you try them. It’s like trying to coordinate a team of guides; they each have their strengths, but getting them to work together optimally can be challenging!

Situations where Random Forest performs well and where it may not

Random Forest works well with large datasets where there may be many features of interest. It’s also great for cases where the relationship between these features and the response variable may be too complex for simpler models.

However, if your data is very noisy, or if interpretability is a primary concern, other models might be more suitable. It’s also not ideal for real-time predictions due to its computational demands.


Summarizing the key points of the article

In our journey through the Random Forest, we’ve explored many fascinating aspects of this powerful machine-learning algorithm. From the core concept of decision trees to the ensemble technique of bagging, we’ve seen how randomness plays a vital role in building this ‘forest’. We also learned how it makes predictions and handles overfitting, and we dove into some key concepts such as variance, bias, and feature importance. Through our real-world example, we’ve seen Random Forest in action, solving a practical problem with high accuracy. And finally, we’ve discussed its strengths and weaknesses, helping us understand when to use this algorithm.

Preempting the following topics in the series: ADA, SVM, Boosting algorithms, etc.

Our journey through the machine-learning jungle is far from over. Next, we’re going to learn about ADA, SVM, and boosting algorithms, among other things. These advanced methods will further expand our toolkit, allowing us to tackle even more complex prediction problems. So, get ready to dive deeper into the jungle!

Remember, as with exploring any new territory, practice is key in mastering these concepts. So keep practicing, keep exploring, and let’s meet in the next adventure!

QUIZ: Test Your Knowledge!

Share the Post:
Learn Data Science. Courses starting at $12.99.

Related Posts

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!