In our last lesson, we examined logistic regression, a powerful method for classification that defines a linear decision boundary. Today, we venture into a different family of models that operate on a completely different principle: decision trees and the powerful ensemble methods built upon them. This is a critical topic for any machine learning interview, as these models are not only widely used but also offer a unique balance of performance and interpretability.
This lesson will guide you through the mechanics of a single decision tree, exploring how it learns from data by making a series of splits. We'll then see how the weaknesses of a single tree are overcome by combining many trees into an "ensemble." We will cover two fundamental ensemble strategies: bagging, which leads to Random Forests, and boosting, which is the basis for Gradient Boosting.
Part 1: The Decision Tree
At its core, a decision tree is an intuitive model that mimics human decision-making. It's essentially a flowchart of questions asked about your data, where each answer leads you down a different path until you reach a conclusion or classification. The models we've seen so far, like linear and logistic regression, work by finding an optimal set of parameters for a single, complex equation. Decision trees, in contrast, work by recursively partitioning the feature space into smaller, more manageable regions.
For a fantastic and gentle introduction to the structure and terminology of decision trees, the StatQuest video below is an excellent starting point.
Decision and Classification Trees, Clearly Explained!!!
This video by Josh Starmer on the StatQuest channel clearly explains the basic structure of a decision tree (root, nodes, leaves) and distinguishes between classification and regression trees.
Please watch the first part of the video, from the beginning to the introduction of terminology. This will familiarize you with the core concepts and how to interpret a tree diagram.
How a Tree Learns: Recursive Splitting
A decision tree is built using a process called recursive binary splitting. It's a top-down, greedy approach.
- Top-down: It starts at the root node (with the entire dataset) and progressively splits the data into smaller subsets.
- Greedy: At each step, the algorithm chooses the best possible split at that moment, without looking ahead to see if a different split might lead to a better overall tree.
The "best" split is the one that results in the most homogeneous child nodes—that is, nodes that are as "pure" as possible in terms of their class composition. For instance, a perfectly pure node would contain data points from only one class.
To quantify this, we use an impurity measure. Two common measures are:
- Gini Impurity: Measures the probability of incorrectly classifying a randomly chosen element in the dataset if it were randomly labeled according to the class distribution in the subset. A Gini score of 0 represents perfect purity (all elements belong to one class).
- Entropy & Information Gain: Entropy is another measure of disorder or impurity, originating from information theory. A split is chosen based on which feature provides the highest Information Gain, which is the reduction in entropy achieved by the split.
While the math differs slightly, both methods achieve the same goal: finding the feature and the split point that best separate the classes. The following video provides a clear, step-by-step example of how Gini impurity is calculated to find the best split.
Decision and Classification Trees, Clearly Explained!!!
Continuing with the StatQuest video, this segment walks through the entire process of building a tree from a small dataset.
Watch the segments that explain how to build a tree, how to quantify impurity using Gini impurity, and how to recursively complete the tree. Pay close attention to how Gini impurity is calculated for both categorical and numerical features.
Overfitting and Pruning
A single decision tree, if left to grow indefinitely, is highly susceptible to overfitting. It can create a tree so deep and complex that it perfectly memorizes the training data, creating individual leaves for just a few or even single data points. Such a model will not generalize well to unseen data.
To combat this, we use pruning. Pruning is a technique to reduce the size and complexity of the tree. There are two main strategies:
- Pre-pruning (Early Stopping): We stop the tree from growing once it meets certain criteria. Common hyperparameters for this include
max_depth(the maximum depth of the tree) andmin_samples_leaf(the minimum number of samples a leaf node must have). - Post-pruning: We first grow the full tree and then remove branches that provide little predictive power. A popular method is Cost Complexity Pruning (CCP), which uses a parameter (
ccp_alphain scikit-learn) to control the trade-off between tree complexity and fit.
For a deeper dive into these concepts, especially the mathematical details of the splitting criteria and pruning, the following article is an excellent resource.
This article from Towards Data Science provides a comprehensive walkthrough of how a decision tree is built, including detailed calculations for both Information Gain and Gini Index, and an explanation of pruning.
Focus on the sections explaining the greedy approach and the two variable selection criteria. You can skim the detailed manual calculations but make sure you understand the logic. Then, read the sections on overfitting and pruning, and Cost Complexity Pruning. This is great material for answering interview questions about controlling model complexity.
Part 2: Ensemble Learning - Strength in Numbers
While pruning helps, a single decision tree is often considered a "weak learner." The solution is to use ensemble methods, which combine many weak learners to create a single, powerful model. This is the core idea behind Random Forest and Gradient Boosting.

The two main ensemble philosophies are Bagging and Boosting.

Part 3: Bagging and Random Forests
Bagging stands for Bootstrap Aggregating. It's a simple but powerful technique to reduce the variance of a high-variance model like a decision tree.
- Bootstrap: We create many new datasets by sampling with replacement from our original training data. Each of these "bootstrap samples" is the same size as the original dataset, but some data points will be repeated, and others will be left out.
- Aggregate: We train a separate decision tree on each bootstrap sample. To make a prediction for a new data point, we let all the trees "vote," and the majority class wins.
A Random Forest is a specific implementation of bagging that adds one more crucial twist to improve performance.
From Bagging to Random Forest
The problem with standard bagging is that if there is one very strong, dominant feature in the dataset, most of the trees will use that feature for their top split. This causes the trees to be highly correlated, and their predictions will be very similar. If the trees are similar, we lose the benefit of averaging, as they will tend to make the same mistakes.
Random Forest solves this by introducing feature randomness: at each split in a tree, the algorithm considers only a random subset of features from which to choose the best split.
This forces the trees to be different from one another, making them less correlated and improving the variance reduction from averaging. This decorrelation is the key to Random Forest's success.
The following StatQuest video explains this process beautifully.
StatQuest: Random Forests Part 1 - Building, Using and Evaluating
This video, also from the StatQuest channel, builds directly on the decision tree concepts to explain how a Random Forest works.
Watch the segments explaining how to create a bootstrap dataset, how to build a tree with a random subset of variables, how to use the forest for prediction, and the formal definition of bagging.
Out-of-Bag (OOB) Error
A clever feature of Random Forests is that they can be validated without needing a separate validation set. Because each tree is trained on a bootstrap sample, on average about one-third of the original data points are left out of that sample. These are the out-of-bag (OOB) samples.
We can use these OOB samples to get an unbiased estimate of the model's performance. For each data point, we make a prediction using only the trees that did not see that data point during training. The overall error rate on these OOB predictions is the OOB error, which is a reliable estimate of the test error.
Data Science Interview Guide – Part 5: Random Forests
This guide is specifically tailored for interview preparation and clearly articulates the core concepts behind Random Forests.
Read the sections on Ensemble Learning and Bagging, Random Forest, and Out-of-Bag Samples. Focus on being able to articulate why feature randomness is important and how OOB error works.
Part 4: Boosting and Gradient Boosting
Boosting is another ensemble technique, but it works sequentially. Instead of building independent trees, boosting builds a "chain" of trees, where each new tree is trained to correct the errors made by the previous ones.
The general idea is:
- Train a simple decision tree on the data.
- Identify the data points that the tree misclassified.
- Increase the weight of these misclassified points, forcing the next tree to pay more attention to them.
- Train a new tree on this re-weighted data.
- Repeat this process for a specified number of trees. The final prediction is a weighted sum of all tree predictions.
Gradient Boosting is a more generalized and widely used form of boosting. Instead of re-weighting data points, it fits each new tree to the residuals (the errors) of the previous ensemble's predictions. In a regression context, the residual is simply (actual_value - predicted_value). In classification, the concept is similar but involves the gradient of the loss function. The intuition remains the same: each new model focuses on what the ensemble is currently getting wrong.
Unlike Random Forest, where adding more trees generally doesn't cause overfitting, Gradient Boosting can overfit if you add too many trees, as it will start to model the noise in the residuals. To control this, a key hyperparameter called the learning rate (or eta) is used. It shrinks the contribution of each new tree, forcing the model to learn more slowly and improving generalization.
Key Differences for Your Interview
Being able to compare and contrast Random Forest and Gradient Boosting is a common interview question. Here are the key points:
| Feature | Random Forest | Gradient Boosting |
|---|---|---|
| Model Building | Parallel (trees are independent) | Sequential (trees are dependent) |
| Main Goal | Reduce variance by averaging decorrelated trees | Reduce bias (and variance) by sequentially correcting errors |
| Overfitting | Robust to overfitting as n_estimators increases |
Can overfit if n_estimators is too high; requires careful tuning |
| Performance | Strong, reliable baseline performance | Often achieves higher accuracy but requires more tuning |
| Tuning | Simpler to tune (mainly n_estimators, max_features) |
More complex (requires tuning learning_rate, n_estimators, tree depth) |
Conclusion
Today we've journeyed from the intuitive simplicity of a single decision tree to the collective power of ensemble methods. You now have the conceptual tools to explain these fundamental algorithms in an interview setting.
Key Takeaways:
- Decision Trees are non-linear models that create rules by recursively partitioning the feature space to increase class purity, but they are prone to overfitting.
- Pruning is used to control tree complexity and prevent overfitting.
- Random Forests (a form of bagging) build many independent, decorrelated trees and average their predictions. They are robust, easy to tune, and great at reducing variance. The key innovations are bootstrapping and random feature selection.
- Gradient Boosting builds trees sequentially, where each tree corrects the errors of the preceding ensemble. It is often more powerful than Random Forest but is more sensitive to hyperparameter tuning and can overfit.
In our next lesson, we will shift our focus from the models themselves to the data we feed into them. We'll begin our exploration of Feature Engineering and Preprocessing, starting with essential techniques like feature scaling, normalization, and transformations.