Chapter 6 of 13
Course Progress0 of 13 completed (0%)

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.

Decision Trees: Entropy & Information GainLearn 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.UNDERSTANDDecision TreesFOLLOW THE FLOWEntropyAPPLY ITInformation GainDecision Trees: Entropy & Information GainLearn 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.UNDERSTANDDecision TreesFOLLOW THE FLOWEntropyAPPLY ITInformation Gain

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:

H(S) = - ∑i=1k pi · log2(pi)

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

Raw Accuracy Split → Blind to subtle purity improvementsCannot differentiate between (90/10) vs (50/50) child nodes if overall accuracy is identical

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

IG(S, A) = Entropy(Parent) - ∑ (N_v / N) · Entropy(Child)Mathematically selects the partition that produces the purest distinct children

Entropy (-∑ p · log2 p) and Gini (1 - ∑ p²) measure exact node disorder, maximizing Information Gain at each recursive decision boundary.

Concept Check

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(S) = 1 - ∑i=1k pi2

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!

Concept Check

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:

Information Gain = Entropy(Parent) - [ (NLeft/N) · Entropy(Left) + (NRight/N) · Entropy(Right) ]

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

Split: Experience ≤ 3.5
Years Experience (X)Test Score (Y)
Parent Entropy:0.980
Left Child Entropy:0.000 (4 pts)
Right Child Entropy:0.544 (8 pts)
Information Gain:+0.617
Choose Split Feature:
3.5 years
Low Threshold (Few Left)BalancedHigh Threshold (Few Right)

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:

Python Bug Hunt · Gini Impurity & Tree Pruning
Python

🎯 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=5 to constrain tree depth.
  • Set min_samples_leaf=5 to ensure leaf cohorts are statistically meaningful.

6Chapter Knowledge Challenge

Check your intuition on decision trees, information gain, and tree depth constraints:

Concept Check 1 of 2
1

What is the primary visual symptom of an overfitted decision tree on a 2D classification dataset?

Concept Check 2 of 2
2

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:

Enjoying this free lesson? Your support helps us build more free animated chapters.

Support Us