Machine Learning Foundations · Chapter 6 · ~15 min · Intermediate
Decision Trees: Entropy & Information Gain
Learn how computers automatically build if/else flowcharts from data. Master Claude Shannon's Information Entropy, calculate Gini Impurity, and prevent runaway overfitting using pre-pruning stopping criteria.
- Formulate Shannon Entropy H(S) as a mathematical measure of uncertainty
- Calculate Gini Impurity and understand its speed advantage over Entropy
- Evaluate Information Gain across candidate orthogonal split partitions
- Regularize decision trees using max_depth and min_samples_leaf pre-pruning
Decision Trees: Entropy & Information Gain: learn it visually
Learn how computers automatically build if/else flowcharts from data. Master Claude Shannon's Information Entropy, calculate Gini Impurity, and prevent runaway overfitting using pre-pruning stopping criteria.
1Quantifying Chaos: Shannon Entropy
To know where to split a dataset, a decision tree needs an objective mathematical way to measure impurity (how mixed up the labels are).
In 1948, Claude Shannon introduced Entropy:
where p_i is the proportion of samples belonging to class i.
- Pure Node (All Hired):
p = 1.0 → H = 0.0(zero uncertainty!). - Maximum Chaos (50% Hired, 50% Rejected):
-(0.5 × -1 + 0.5 × -1) = 1.0(maximum disorder).
Wrong idea: Splitting on raw majority accuracy percentage
Evaluating candidate splits based on raw classification accuracy frequently leads to ties and fails to isolate pure homogeneous subgroups.
Real system: Shannon Entropy & Gini Impurity reduction
Entropy (-∑ p · log2 p) and Gini (1 - ∑ p²) measure exact node disorder, maximizing Information Gain at each recursive decision boundary.
What is the Shannon Entropy of a decision tree node containing 100 samples that all belong to the exact same class?
2Gini Impurity: The Fast Alternative
While Shannon Entropy uses logarithms, the CART (Classification and Regression Trees) algorithm introduced by Leo Breiman uses Gini Impurity:
Gini measures the probability that a randomly chosen element would be incorrectly labeled if it were randomly labeled according to the distribution of labels in the subset. Because it avoids computing expensive log2 transcendental functions, Gini executes significantly faster on massive datasets!
Why do modern machine learning frameworks often use Gini Impurity as the default criterion over Shannon Entropy?
3Information Gain: Choosing the Winning Split
At every node, the tree evaluates every candidate feature and split point. It selects the split that yields the largest reduction in entropy, termed Information Gain:
4Hands-On: 2D Feature Partition & Split Optimizer
Experiment with hiring candidate data below. Switch between Experience and Test Score, and adjust the threshold slider to find the boundary that maximizes Information Gain:
2D Decision Boundary Partition & Information Gain Explorer
5Bug Hunt: Fix Gini Impurity & Prevent Runaway Overfitting
A junior engineer implemented custom tree splitting logic but inverted the Gini formula and trained an unconstrained model that memorized 100% of training outliers while failing catastrophically on test data.
Repair the Gini calculation and apply proper pre-pruning hyperparameters:
🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.
Need a hint?
- Fix Gini formula:
gini = 1 - np.sum(probabilities ** 2). - Set
max_depth=5to constrain tree depth. - Set
min_samples_leaf=5to ensure leaf cohorts are statistically meaningful.
6Chapter Knowledge Challenge
Check your intuition on decision trees, information gain, and tree depth constraints:
What is the primary visual symptom of an overfitted decision tree on a 2D classification dataset?
How does setting min_samples_leaf=10 protect against decision tree overfitting?
7Recap
- Shannon Entropy measures uncertainty:
H(S) = - ∑ p_i log2(p_i). - Gini Impurity provides a faster alternative avoiding logarithms:
1 - ∑ p_i^2. - Information Gain guides recursive binary splitting by maximizing parent-to-child purity reduction.
- Pre-Pruning (
max_depth,min_samples_leaf) prevents decision trees from memorizing training flukes.
Ready for Chapter 7? Mark this chapter as complete to proceed: