Stop 04 · Probability & StatisticsTopics · 9/9

Entropy & KL Divergence

Part of the Machine Learning Roadmap

In short

Decision trees pick splits by reducing entropy, and almost every classifier is trained by minimising cross-entropy. These three ideas, entropy, cross-entropy and KL divergence, are the language of uncertainty in ML.

Entropy measures how uncertain or mixed a distribution is, cross-entropy measures how well one distribution predicts another, and KL divergence measures how different two distributions are. They drive decision tree splits and classification losses.

The jaadu reaches blindfolded into a big glass jar of mixed Indian sweets beside a second jar holding only round laddoos.

Think of it like this

The sweet shop jar

A jar with only laddoos has no surprise: you always know what you'll pull out. Entropy is zero. A jar with equal laddoos, barfis, jalebis and pedas is maximum surprise. Now suppose you guessed the jar was mostly laddoos when it really was mixed. KL divergence measures how badly your guess fits the real jar, and cross-entropy is the total surprise you feel using the wrong guess.

Watch it happen

Entropy of different jars (in bits)

100% laddoo
0
90/10 mix
1
50/50 mix
2
4 equal kinds
3

1/5Entropy is H = -Σ p log2 p. It measures average surprise per draw.

The big ideas

What you need to know

Entropy = average surprise

Pure groups have low entropy, mixed groups have high entropy. Trees choose splits that make child nodes purer.

Cross-entropy = cost of a wrong guess

H(p, q) uses the true distribution p but your predicted q. It's the classification loss: lower means better predictions.

KL = the extra cost

KL(p || q) = cross-entropy minus entropy. It's zero only when q matches p exactly, and never negative.

KL is not symmetric

KL(p || q) and KL(q || p) usually differ, so it's not a true distance. Order matters.

See it in code

Walk through the code

python
1import numpy as np2from scipy.stats import entropy3 4p = np.array([0.5, 0.5])      # true jar: half laddoo, half barfi5q = np.array([0.9, 0.1])      # our guess: mostly laddoo6 7H_p = entropy(p, base=2)                  # 1.08cross = -np.sum(p * np.log2(q))           # 1.7379kl = entropy(p, q, base=2)                # 0.73710 11print(H_p, round(cross, 3), round(kl, 3))12print(round(cross - H_p, 3) == round(kl, 3))   # True13 14# Binary log loss for one example with true label 1 and predicted 0.815print(-np.log(0.8))                       # 0.223

1/5p is the true distribution. q is what the model believes.

Entropy, cross-entropy and KL divergence on the sweet jar, plus the log loss a classifier sees.

Save yourself the pain

Common mistakes

Oops

Letting a model predict exactly 0 probability for a class.

Do this instead

log(0) is minus infinity, so one confident wrong answer explodes the loss. Libraries clip probabilities for this reason.

Oops

Calling KL divergence a distance.

Do this instead

It isn't symmetric and doesn't obey the triangle rule. Say 'divergence' and keep the order of p and q clear.

Your mission

Score a tree split

~20 min

Quick check

When is KL(p || q) equal to zero?

Go deeper

  1. 1Compute the entropy of a fair coin and of a coin that lands heads 90% of the time.
  2. 2Calculate cross-entropy loss by hand for one prediction of 0.8 when the true label is 1.
  3. 3Use scipy.stats.entropy to compute a KL divergence between two small distributions.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge