Guide To AI Logo
Unit 12

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 (H(X)H(X)): Measures the average amount of uncertainty or 'surprise' in a random variable XX. 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:

H(X)=xXP(x)log2P(x)H(X) = -\sum_{x \in \mathcal{X}} P(x) \log_2 P(x),

Biased Coin Trace: Let's calculate and compare the entropy of a fair coin vs. a heavily biased coin:

- Fair Coin: P(H)=0.5,P(T)=0.5P(H) = 0.5, P(T) = 0.5. The entropy is: H(X)=(0.5log20.5+0.5log20.5)=(0.5(1)+0.5(1))=1.0 bitH(X) = -(0.5 \log_2 0.5 + 0.5 \log_2 0.5) = -(0.5(-1) + 0.5(-1)) = 1.0 \text{ bit} This represents maximum uncertainty; we have no prior bias about the toss.

- Biased Coin: P(H)=0.8,P(T)=0.2P(H) = 0.8, P(T) = 0.2. The entropy is: H(X)=(0.8log20.8+0.8log20.8)(0.8(0.32)+0.2(2.32))0.256+0.464=0.72 bitsH(X) = -(0.8 \log_2 0.8 + 0.8 \log_2 0.8) \approx -(0.8(-0.32) + 0.2(-2.32)) \approx 0.256 + 0.464 = 0.72 \text{ bits} Because the coin is heavily biased, we are less 'surprised' by the outcomes on average, reducing our statistical entropy from 1.01.0 down to 0.720.72 bits!

Joint Entropy (H(X,Y)H(X, Y)): Measures the combined total uncertainty in a pair of random variables XX and YY simultaneously: H(X,Y)=xyP(x,y)log2P(x,y)H(X, Y) = -\sum_{x} \sum_{y} P(x, y) \log_2 P(x, y)

Conditional Entropy (H(YX)H(Y | X)): Measures the remaining uncertainty of variable YY given that we already know the exact value of variable XX: H(YX)=xyP(x,y)log2P(yx)H(Y | X) = -\sum_{x} \sum_{y} P(x, y) \log_2 P(y | x)

2. KL Divergence and Cross-Entropy Loss

In machine learning, models output predicted probability distributions (QQ) trying to approximate the true target distribution (PP). We need to measure how much these two distributions differ.

Kullback-Leibler (KL) Divergence (DKL(PQ)D_{\text{KL}}(P \parallel Q)): Measures the extra information/bits required to represent data using distribution QQ instead of the true distribution PP. It acts as an asymmetric distance measure (DKL(PQ)DKL(QP)D_{\text{KL}}(P \parallel Q) \neq D_{\text{KL}}(Q \parallel P)): DKL(PQ)=xXP(x)logP(x)Q(x)D_{\text{KL}}(P \parallel Q) = \sum_{x \in \mathcal{X}} P(x) \log \frac{P(x)}{Q(x)}

Cross-Entropy (H(P,Q)H(P, Q)): Measures the average bits needed to encode symbols from true distribution PP using code model QQ. It is mathematically equal to the sum of the true distribution's entropy and the KL divergence: H(P,Q)=H(P)+DKL(PQ)=xXP(x)logQ(x)H(P, Q) = H(P) + D_{\text{KL}}(P \parallel Q) = -\sum_{x \in \mathcal{X}} P(x) \log Q(x)

Cross-Entropy Loss Calculation: Suppose we have a binary classification task. The true label of an image is P=[1.0,0.0]P = [1.0, 0.0] (it is 100%100\% a cat). Our neural network outputs a prediction Q=[0.8,0.2]Q = [0.8, 0.2] (the model is 80%80\% confident it is a cat). Let's calculate the Cross-Entropy Loss (using natural logarithms): H(P,Q)=(1.0ln(0.8)+0.0ln(0.2))=1.0×(0.223)+0=0.223 natsH(P, Q) = -\left( 1.0 \ln(0.8) + 0.0 \ln(0.2) \right) = -1.0 \times (-0.223) + 0 = 0.223 \text{ nats} If our model was less confident (e.g. Q=[0.5,0.5]Q = [0.5, 0.5]), the loss would rise to ln(0.5)=0.693-\ln(0.5) = 0.693 nats. In deep classification networks, minimizing the cross-entropy loss function is mathematically equivalent to minimizing the KL divergence, forcing our predictions QQ to match true targets PP!

3. Information Gain and Decision Tree Splitting

Information theory also guides how a classification tree partitions labeled data. At a node containing sample set TT, the tree considers candidate rules aa, such as xjτx_j \leq \tau for a numerical feature or membership in a category. Each rule sends samples into child subsets TvT_v. A node is pure when nearly all of its samples share one class; its entropy is then close to zero.

For class proportions pcp_c in node TT, node entropy is H(T)=cpclog2pc.H(T)=-\sum_c p_c\log_2 p_c. The uncertainty remaining after candidate split aa is the size-weighted average of the child entropies: H(Ta)=vTvTH(Tv).H(T\mid a)=\sum_v\frac{|T_v|}{|T|}H(T_v). Weighting matters because a tiny pure child should not outweigh a large, still-mixed child.

Information Gain is the entropy removed by the split: IG(T,a)=H(T)H(Ta).\operatorname{IG}(T,a)=H(T)-H(T\mid a). 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

Candidate A: parent to childrenParent TPositive samples: 6Negative samples: 4H(T) = 0.971Left childRight childRule: feature ≤ thresholdRule: feature > thresholdPositive samples: 4Negative samples: 0Positive samples: 2Negative samples: 4H = 0.000 (pure)H = 0.918weighted H = 0.551 → IG = 0.420Greedy candidate comparisonparent entropy: 0.971 bitsCandidate ALeft child: 4 positive, 0 negativeRight child: 2 positive, 4 negativeIG = 0.420weighted H = 0.551Candidate BLeft child: 4 positive, 1 negativeRight child: 2 positive, 3 negativeIG = 0.125weighted H = 0.846choose A: largest information gain
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
Worked Example 1

Compare Two Candidate Splits

Problem

A parent node contains 10 samples: 6 positive and 4 negative. Candidate A creates children with class counts (4,0)(4,0) and (2,4)(2,4). Candidate B creates children with counts (4,1)(4,1) and (2,3)(2,3). Calculate each information gain and choose the greedy split.

Step-by-step solution

1.The parent entropy is H(T)=0.6log2(0.6)0.4log2(0.4)0.971H(T)=-0.6\log_2(0.6)-0.4\log_2(0.4)\approx0.971 bits.

2.For Candidate A, the (4,0)(4,0) child has entropy 00, while the (2,4)(2,4) child has entropy 13log21323log2230.918-\frac{1}{3}\log_2\frac{1}{3}-\frac{2}{3}\log_2\frac{2}{3}\approx0.918 bits.

3.Candidate A leaves H(TA)=410(0)+610(0.918)0.551H(T\mid A)=\frac{4}{10}(0)+\frac{6}{10}(0.918)\approx0.551 bits, so IG(T,A)=0.9710.5510.420\operatorname{IG}(T,A)=0.971-0.551\approx0.420 bits.

4.For Candidate B, the child entropies are H(4/5,1/5)0.722H(4/5,1/5)\approx0.722 and H(2/5,3/5)0.971H(2/5,3/5)\approx0.971. Thus H(TB)=510(0.722)+510(0.971)0.846H(T\mid B)=\frac{5}{10}(0.722)+\frac{5}{10}(0.971)\approx0.846 and IG(T,B)0.125\operatorname{IG}(T,B)\approx0.125 bits.

Final answer and interpretation

The greedy tree selects Candidate A because 0.420>0.1250.420>0.125; 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

QUESTION 01

Calculate the Shannon Entropy H(X)H(X) of a fair, binary coin flip (where success probability P(H)=0.50P(H) = 0.50 and P(T)=0.50P(T) = 0.50) using base-2 logarithms:

QUESTION 02

Which of the following describes why the Kullback-Leibler (KL) Divergence is NOT a true mathematical metric/distance?

QUESTION 03

In neural classification models, minimizing the Cross-Entropy loss H(P,Q)H(P, Q) between the true labels PP and predicted probabilities QQ is mathematically equivalent to minimizing which of the following?

QUESTION 04

In decision tree models, what does a high 'Information Gain' value for a specific feature split indicate?

QUESTION 05

What is the Shannon Entropy H(X)H(X) of a completely deterministic event (where success probability P(A)=1.0P(A) = 1.0)?

QUESTION 06

Calculate the Shannon Entropy H(X)H(X) of a biased coin where the probability of heads is P(H)=0.80P(H) = 0.80 and tails is P(T)=0.20P(T) = 0.20 (using natural logarithms, so the unit is 'nats'):

QUESTION 07

Which of the following formulas represents the Mutual Information I(X;Y)I(X; Y) between two random variables?

QUESTION 08

If we have a true label distribution PP and model prediction QQ, which expression represents the Cross-Entropy H(P,Q)H(P, Q)?

QUESTION 09

What is the relation between Joint Entropy H(X,Y)H(X, Y), individual Entropy H(X)H(X), and Conditional Entropy H(YX)H(Y | X)?

QUESTION 10

Which of the following is true about Kullback-Leibler (KL) Divergence DKL(PQ)D_{\text{KL}}(P \parallel Q)?