Hello! Welcome back to our module on Tree-Based Models.
In our last lesson, we built an intuitive understanding of how decision trees work. We saw that they make predictions by recursively splitting the data into smaller, more "pure" groups. You learned to build and interpret a basic tree, and we noted that the algorithm greedily chooses the "best" split at each step.
Today, we will dive into the core mechanism that powers this decision-making process. Your learning outcome for this lesson is to explain split criteria such as Gini impurity and information gain. We'll explore the mathematical formulas that decision trees use to quantify the "purity" of a node and, therefore, decide which feature and threshold to use for a split. This directly addresses your goal of understanding the "why" behind your models.
Part 1: Quantifying Purity
In the last lesson, we described a "good" split as one that makes the resulting groups (the child nodes) as homogeneous as possible.
- A node is perfectly pure (homogeneous) if all data points in it belong to a single class.
- A node is maximally impure (heterogeneous) if the data points are evenly distributed among all classes.
For a computer to find the best split, we need to translate this idea into a number. This number is called an impurity measure. The goal of the algorithm is to perform a split that leads to the largest reduction in impurity. Two of the most common impurity measures used in classification trees are Gini Impurity and Entropy.
Part 2: Gini Impurity
Gini Impurity is a measure of how often a randomly chosen element from the set would be incorrectly labeled if it were randomly labeled according to the distribution of labels in the subset. It's the default criterion used in scikit-learn's DecisionTreeClassifier.
The formula for Gini Impurity for a set of items with classes is:
where is the probability of a data point belonging to class .
- For a pure node, the probability of the single class is 1 (), and all others are 0. The Gini impurity is .
- For a maximally impure node with two classes (50/50 split), and . The Gini impurity is .
The following video from StatQuest provides a fantastic, step-by-step walkthrough of how to calculate Gini impurity for individual nodes and then for an entire split.
Decision and Classification Trees, Clearly Explained!!!
This video clearly explains the concept of impurity and demonstrates how to calculate Gini Impurity for a split. It's a great visual guide to the math involved.
Watch the video from 05:18 to 09:54. Pay close attention to: The concept of an 'impure' leaf. The formula for Gini Impurity. The process of calculating the Gini Impurity for individual leaves and then combining them into a single score for the split using a weighted average.
How Gini Impurity is Used to Find the Best Split
As the video explained, the process to find the best split using Gini Impurity is:
- Calculate the Gini Impurity of the current (parent) node before any split.
- For every possible split (i.e., for each feature and each unique value/threshold):
a. Calculate the Gini Impurity of each potential child node.
b. Calculate the weighted average Gini Impurity for the split:
where is the total number of samples in the parent node, is the number of child nodes (usually 2), is the number of samples in child node , and is the Gini impurity of child node . - Calculate the Gini Gain (or impurity reduction):
- The algorithm selects the split with the highest Gini Gain.
This image clearly illustrates this comparison process. The algorithm calculates the weighted Gini score for splits on different features and chooses the one with the lowest score (which corresponds to the highest Gini Gain).

Part 3: Entropy and Information Gain
Another popular splitting criterion is Information Gain, which is based on the concept of Entropy from information theory.
Entropy is also a measure of impurity or disorder. Its formula is:
where is the probability of a data point belonging to class .
- For a pure node (), the Entropy is .
- For a maximally impure node with two classes (50/50 split), the Entropy is .
The value log_2(p_i) can be thought of as the "amount of information" needed to identify the class of a new data point. Entropy is the expected value of this information. A pure node requires no new information, so its entropy is zero.
The video below explains Entropy and Information Gain in detail.
Decision Trees Geometric Intuition | Entropy | Gini impurity | Information Gain
This video from CampusX provides a thorough explanation of Entropy and shows how it's used to calculate Information Gain for selecting the best split.
Watch the video from 21:45 to 42:07. Focus on: The definition of Entropy as a measure of impurity or uncertainty. How to calculate Entropy for a given set of class labels. The definition of Information Gain as the reduction in entropy after a split. The step-by-step process of calculating the weighted entropy of the children and finding the Information Gain.
Just like with Gini Gain, the algorithm calculates the Information Gain for every possible split and chooses the one that maximizes it.
Information Gain =
where is the weighted average of the entropy of the child nodes.
Part 4: Gini Impurity vs. Entropy
You might be wondering which criterion to use. In practice, both Gini Impurity and Entropy are effective and often lead to very similar trees. However, there are some subtle differences.
The same video resource provides an excellent comparison.
Decision Trees Geometric Intuition | Entropy | Gini impurity | Information Gain
This final clip from the CampusX video compares Gini Impurity and Entropy, discussing their similarities, differences, and practical implications.
Watch from 42:07 to 50:22. This covers: The formula and calculation for Gini Impurity (a good review). A direct comparison of the behavior and output range of Gini vs. Entropy. The computational trade-offs (Gini is slightly faster).
Here's a summary of the key differences:
| Feature | Gini Impurity | Entropy / Information Gain |
|---|---|---|
| Calculation | Involves squaring probabilities | Involves logarithms |
| Computational Speed | Faster, as it avoids logarithms | Slower due to log computations |
| Output Range (2 class) | [0, 0.5] |
[0, 1] |
| Tendency | Tends to isolate the most frequent class | Tends to produce slightly more balanced trees |
Default in sklearn |
Yes (criterion='gini') |
No (criterion='entropy') |
Despite these differences, choosing between them is rarely the most critical hyperparameter to tune. In most scenarios, the resulting trees will be very similar. Gini's computational efficiency is why it's a common default.
Test your understanding!
Imagine you have a parent node with 20 samples: 10 from Class A and 10 from Class B.
A potential split on Feature X > 5 results in two child nodes:
- Child 1 (True): 8 samples from Class A, 2 samples from Class B. (Total 10)
- Child 2 (False): 2 samples from Class A, 8 samples from Class B. (Total 10)
Calculate the Gini Gain for this split.
Show answer
-
Gini of Parent Node:
- ,
-
Gini of Child 1:
- ,
-
Gini of Child 2:
- ,
-
Weighted Gini of the Split:
-
Gini Gain:
The Gini Gain for this split is 0.18. The algorithm would compare this value to the gains from all other possible splits to find the best one.
Conclusion
You now have the statistical foundation to understand exactly how a decision tree learns. Instead of being a black box, you can see that the tree-building process is a methodical search for splits that maximize purity.
Key Takeaways:
- Decision trees use splitting criteria to find the best feature and threshold to split the data at each node.
- Gini Impurity and Entropy are the two primary metrics used to measure the impurity or disorder of a node in a classification tree. A score of 0 indicates a perfectly pure node.
- The algorithm seeks to maximize the impurity reduction, which is called Gini Gain (when using Gini) or Information Gain (when using Entropy).
- Gini Impurity is computationally faster and the default in libraries like
scikit-learn, but both criteria are effective and typically yield similar results.
Preview of the Next Lesson:
While powerful and interpretable, single decision trees have a significant weakness: they are prone to overfitting and can be unstable, meaning small changes in the training data can lead to a completely different tree. In our next lesson, we will address this by introducing bootstrap aggregating (bagging), an ensemble technique that combines multiple decision trees to create a much more robust and accurate model, leading us directly to the famous Random Forest algorithm.