Welcome back! In our last lesson, we completed our exploration of Naive Bayes classifiers, learning how to apply them to both numerical and text data and how to evaluate their performance using a comprehensive set of metrics. We saw that Naive Bayes is a probabilistic model, making predictions based on the calculated probabilities of features given a certain class.
Today, we pivot to an entirely different family of supervised learning algorithms: tree-based models. We will start with the fundamental building block of this family, the Decision Tree. Unlike the probabilistic approach of Naive Bayes, a decision tree makes predictions by learning a hierarchical set of simple, if-then-else rules. Our goal for this lesson is to understand the mechanics of how a decision tree is constructed from data. We will explore its core components, the mathematical criteria it uses to make splits, and the essential techniques for controlling its complexity to prevent overfitting.
The Intuition Behind Decision Trees
At its heart, a decision tree is an intuitive model that mimics human decision-making. Imagine trying to classify an email as spam or not. You might ask a series of questions: "Does the subject line contain the word 'winner'?", "Is the sender in my contact list?", "Does the email contain many capitalized words?". Based on the answers, you follow a path to a final decision. A decision tree formalizes this process.
It's a flowchart-like structure where:
- Nodes represent a test on a feature (e.g., "petal width <= 0.8 cm?").
- Branches represent the outcome of the test (e.g., True/False).
- Leaf Nodes represent a class label (the final decision).
The algorithm learns this structure automatically from your training data through a process called recursive binary splitting.

The video below from StatQuest with Josh Starmer provides an excellent introduction to the terminology and structure of decision trees.
Decision and Classification Trees, Clearly Explained!!!
This video, "Decision and Classification Trees, Clearly Explained!!!," introduces the basic concepts and terminology of tree-based models.
Please watch the first part of the video (this segment), which defines what a decision tree is, differentiates between classification and regression trees, and labels the key components: the root node, internal nodes (or branches), and leaf nodes.
Building a Tree: The Splitting Process
The fundamental question in building a decision tree is: at each node, which feature and which split-point should we choose to divide the data? The goal is to select a split that makes the resulting child nodes as pure as possible. A pure node is one that contains data points from only a single class.
The algorithm uses a top-down, greedy approach. "Top-down" means it starts at the root node with the entire dataset and recursively splits it. "Greedy" means that at each step, it chooses the locally optimal split—the one that creates the purest child nodes at that moment—without looking ahead to see if a different split might lead to a better overall tree. Given your background in algorithms, you'll recognize this as a common heuristic for building complex structures efficiently.
To quantify "purity," we need a mathematical metric. The two most common metrics are Gini Impurity and Information Gain (which is based on Entropy).
Splitting Criterion 1: Gini Impurity
Gini Impurity is the default criterion in scikit-learn. It measures the probability of incorrectly classifying a randomly chosen element in the dataset if it were randomly labeled according to the class distribution in that node. The formula for a given node is:
where is the number of classes and is the probability of class in the node.
- A Gini Impurity of 0 means the node is perfectly pure (all samples belong to one class).
- A Gini Impurity of 0.5 (for a binary classification problem) indicates the highest level of impurity (samples are evenly split between the two classes).
The algorithm calculates the Gini Impurity for every possible split across every feature. It then chooses the split that results in the lowest weighted average Gini Impurity of the child nodes. The StatQuest video provides a brilliantly clear, step-by-step walkthrough of this entire calculation process for both categorical and numerical features.
Decision and Classification Trees, Clearly Explained!!!
This video provides a detailed, step-by-step example of how a decision tree is built using Gini Impurity as the splitting criterion.
Please watch the core section of the video (this segment). It demonstrates: How to calculate the Gini Impurity for individual leaves. How to calculate the weighted average Gini Impurity for a split. How the algorithm handles numerical features by testing splits at the midpoint between sorted values. How it compares the Gini impurity across different features to select the best one for the root node.
Splitting Criterion 2: Entropy and Information Gain
An alternative to Gini Impurity is a concept from information theory: Entropy. Entropy measures the level of uncertainty or randomness in a node. The formula is:
Like Gini, an entropy of 0 signifies a perfectly pure node. The maximum value depends on the number of classes; for binary classification, it is 1.
The decision tree algorithm uses entropy to calculate Information Gain, which is the reduction in entropy achieved by splitting the data on a particular feature.
The algorithm aims to maximize the Information Gain, which is equivalent to minimizing the weighted average entropy of the children. The following article provides a detailed numerical example of this calculation.
This article from Towards Data Science walks through the mathematical details of splitting criteria using a toy dataset.
Focus on the following sections to understand Entropy and Information Gain: Start with the definition of Entropy and its formula. Next, read the section explaining what Information Gain is and how it relates to the parent and child node entropies. Finally, follow the detailed walkthrough under the heading Calculating the Root Node. It shows how to calculate the Information Gain for each potential feature split and select the best one. Don't worry about memorizing the exact calculations, but focus on understanding the process.
While Gini Impurity and Information Gain are calculated differently, they generally produce very similar trees in practice. Gini is often preferred as the default because it's slightly faster to compute, as it avoids the logarithmic calculation.
Controlling Complexity: Overfitting and Pruning
If we allow a decision tree to grow until every leaf node is perfectly pure, it will likely overfit the training data. The tree will learn the noise and specific quirks of the training set, creating an overly complex model that fails to generalize to new, unseen data.
To combat this, we must control the tree's complexity using a process called pruning. There are two main approaches:
-
Pre-pruning (Early Stopping): This involves setting constraints on the tree's growth before training. We can define stopping conditions, such as:
max_depth: The maximum number of levels the tree can have.min_samples_split: The minimum number of samples a node must have before it can be split.min_samples_leaf: The minimum number of samples a leaf node must contain.
If a split would violate any of these conditions, it is not performed.
-
Post-pruning: This involves growing the tree to its full depth and then trimming back branches that provide little explanatory power. The most common method is Cost Complexity Pruning (CCP). CCP introduces a tuning parameter,
ccp_alpha, which acts as a penalty for the number of leaf nodes. Asalphaincreases, the algorithm is incentivized to prune away leaves, trading a small increase in training error for a simpler, more generalizable model.
The article we looked at earlier provides a good overview of these techniques.
This resource explains the problem of overfitting in decision trees and the primary methods used to control it.
Please read the final sections of the article: Start at Overfitting and Decision Trees to understand why this is a major challenge for this algorithm. Read the section on Pruning, including the subsections on Pre-pruning and the introduction to Post-pruning. Finally, read the explanation of Cost complexity pruning. Focus on the high-level concept: the alpha parameter controls the trade-off between the tree's complexity and its fit to the data.
Conclusion
In this lesson, we have dissected the inner workings of the decision tree algorithm. You've learned how these intuitive, flowchart-like models are constructed from data by making a series of greedy, locally optimal splits.
Key Takeaways:
- Decision trees are built using recursive binary splitting, a top-down, greedy process that aims to create the purest possible child nodes at each step.
- The "best" split is determined using a mathematical criterion to measure impurity. The two most common are Gini Impurity (probability of misclassification) and Information Gain (reduction in entropy).
- Unconstrained trees are highly prone to overfitting.
- Pruning is the essential technique to control tree complexity. This can be done through pre-pruning (setting hyperparameters like
max_depthto stop growth early) or post-pruning (using methods like Cost Complexity Pruning to trim a fully grown tree).
In our next lesson, we will move from theory to practice. You will apply what you've learned to build, visualize, and evaluate a decision tree classifier in Python using scikit-learn. We will also discuss the primary strengths and weaknesses of this fundamental model, which will set the stage for understanding more advanced ensemble methods.