Information Theory for Machine Learning
measuring information, uncertainty, and statistical relationships within data
Core Concepts Covered
- Entropy, joint entropy, and conditional entropy
- Cross-entropy, Kullback-Leibler (KL) divergence, and mutual info
- Information gain in tree-based algorithms
1. Entropy: Measuring Uncertainty and Surprise
Claude Shannon introduced information theory in 1948 to measure how much information a message carries and how efficiently it can be stored or communicated. In machine learning, the same ideas help us describe uncertainty, measure relationships between variables, and build loss functions.
• Shannon Entropy (): Measures the average amount of uncertainty or 'surprise' in a random variable . If an outcome is highly certain, its entropy is low; if it is completely random, its entropy is maximized. The formula for a discrete variable is:
,
Biased Coin Trace: Let's calculate and compare the entropy of a fair coin vs. a heavily biased coin:
- Fair Coin: . The entropy is: This represents maximum uncertainty; we have no prior bias about the toss.
- Biased Coin: . The entropy is: Because the coin is heavily biased, we are less 'surprised' by the outcomes on average, reducing our statistical entropy from down to bits!
• Joint Entropy (): Measures the combined total uncertainty in a pair of random variables and simultaneously:
• Conditional Entropy (): Measures the remaining uncertainty of variable given that we already know the exact value of variable :
2. KL Divergence and Cross-Entropy Loss
In machine learning, models output predicted probability distributions () trying to approximate the true target distribution (). We need to measure how much these two distributions differ.
• Kullback-Leibler (KL) Divergence (): Measures the extra information/bits required to represent data using distribution instead of the true distribution . It acts as an asymmetric distance measure ():
• Cross-Entropy (): Measures the average bits needed to encode symbols from true distribution using code model . It is mathematically equal to the sum of the true distribution's entropy and the KL divergence:
Cross-Entropy Loss Calculation: Suppose we have a binary classification task. The true label of an image is (it is a cat). Our neural network outputs a prediction (the model is confident it is a cat). Let's calculate the Cross-Entropy Loss (using natural logarithms): If our model was less confident (e.g. ), the loss would rise to nats. In deep classification networks, minimizing the cross-entropy loss function is mathematically equivalent to minimizing the KL divergence, forcing our predictions to match true targets !
3. Information Gain and Decision Tree Splitting
Information theory also guides how a classification tree partitions labeled data. At a node containing sample set , the tree considers candidate rules , such as for a numerical feature or membership in a category. Each rule sends samples into child subsets . A node is pure when nearly all of its samples share one class; its entropy is then close to zero.
For class proportions in node , node entropy is The uncertainty remaining after candidate split is the size-weighted average of the child entropies: Weighting matters because a tiny pure child should not outweigh a large, still-mixed child.
Information Gain is the entropy removed by the split: The training algorithm evaluates the available features and thresholds and greedily selects the candidate with the largest gain. This is a locally best choice at the current node; it does not guarantee the globally smallest or most accurate possible tree.
The same search repeats recursively inside each child. Growth stops when a node is pure, no candidate improves the criterion, or a rule such as max_depth, min_samples_split, min_samples_leaf, or a minimum impurity decrease is reached. An unrestricted tree can memorize noise. Pre-pruning limits growth, while post-pruning removes weak branches after construction, usually with validation data or a complexity penalty. Unit 13 places these mechanics in the broader supervised-learning workflow.
Information Gain Compares Candidate Tree Splits
Read diagram labels
- Candidate A: parent to children
- Parent T
- Positive samples: 6
- Negative samples: 4
- H(T) = 0.971
- Left child
- Right child
- Rule: feature ≤ threshold
- Rule: feature > threshold
- Positive samples: 4
- Negative samples: 0
- Positive samples: 2
- H = 0.000 (pure)
- H = 0.918
- weighted H = 0.551 → IG = 0.420
- Greedy candidate comparison
- parent entropy: 0.971 bits
- Candidate A
- Left child: 4 positive, 0 negative
- Right child: 2 positive, 4 negative
- IG = 0.420
- weighted H = 0.551
- Candidate B
- Left child: 4 positive, 1 negative
- Right child: 2 positive, 3 negative
- IG = 0.125
- weighted H = 0.846
- choose A: largest information gain
Compare Two Candidate Splits
A parent node contains 10 samples: 6 positive and 4 negative. Candidate A creates children with class counts and . Candidate B creates children with counts and . Calculate each information gain and choose the greedy split.
1.The parent entropy is bits.
2.For Candidate A, the child has entropy , while the child has entropy bits.
3.Candidate A leaves bits, so bits.
4.For Candidate B, the child entropies are and . Thus and bits.
The greedy tree selects Candidate A because ; it removes more uncertainty at this node.
Information gain rewards purity across all children in proportion to how many samples each child contains.
Interactive Practice Quiz
Test your understanding with instant feedback
Calculate the Shannon Entropy of a fair, binary coin flip (where success probability and ) using base-2 logarithms:
Which of the following describes why the Kullback-Leibler (KL) Divergence is NOT a true mathematical metric/distance?
In neural classification models, minimizing the Cross-Entropy loss between the true labels and predicted probabilities is mathematically equivalent to minimizing which of the following?
In decision tree models, what does a high 'Information Gain' value for a specific feature split indicate?
What is the Shannon Entropy of a completely deterministic event (where success probability )?
Calculate the Shannon Entropy of a biased coin where the probability of heads is and tails is (using natural logarithms, so the unit is 'nats'):
Which of the following formulas represents the Mutual Information between two random variables?
If we have a true label distribution and model prediction , which expression represents the Cross-Entropy ?
What is the relation between Joint Entropy , individual Entropy , and Conditional Entropy ?
Which of the following is true about Kullback-Leibler (KL) Divergence ?
Further Readings
Explore these highly recommended external references to deepen your understanding
A Mathematical Theory of Communication
https://people.math.harvard.edu/~ctm/home/text/others/shannon/entropy/entropy.pdf
Dive into Deep Learning: Information Theory
https://d2l.ai/chapter_appendix-mathematics-for-deep-learning/information-theory.html
Scikit-learn User Guide: Decision Trees
https://scikit-learn.org/stable/modules/tree.html
