Skip to content
Kudos AI
Lire en français
Probability Foundations

Entropy and Information

Measuring uncertainty in bits: Shannon entropy and why the logarithm is base 2, information gain worked on a split, and how cross-entropy and KL divergence relate to entropy and to the loss functions used to train classifiers.

10 min readKudos AI

Prerequisites: Probability from Zero: The Language of Uncertainty

The distribution is dragged from uniform to lopsided while the entropy readout tracks it: a fair coin at 1 bit, a 99/1 coin at 0.0808, a fair four-sided die at 2, and the 0.1957 bits a split buys you.

"How uncertain am I?" sounds like a question about a feeling. It is not. It has a precise numerical answer, measured in bits, and that answer turns out to be the quantity that decides how a decision tree splits and what loss a classifier is trained against. Both fall out of one definition.

A. Measuring surprise in bits

Start with the intuition. A coin known to land heads always carries no uncertainty: observing it tells you nothing you did not already know. A fair coin is maximally uncertain between two options. A coin that lands heads 99% of the time is much closer to the first case than the second - guess heads and you are wrong only 1% of the time.

We want a measure that is 00 for the certain coin, largest for the fair one, and small for the 99% coin. Following Russell & Norvig, the entropy of a random variable VV taking values vkv_k with probabilities P(vk)P(v_k) is

H(V)=∑kP(vk)log⁡21P(vk)=−∑kP(vk)log⁡2P(vk).H(V) = \sum_{k} P(v_k) \log_2 \frac{1}{P(v_k)} = -\sum_{k} P(v_k) \log_2 P(v_k) .

The first form is the more suggestive one. The quantity log⁡21P(vk)\log_2 \frac{1}{P(v_k)} is the surprise of outcome kk: large when the outcome was unlikely, zero when it was certain. Entropy is simply the expected surprise - each surprise weighted by how often it actually happens.

Why base 2. The unit is the bit, and base 2 makes it literal: entropy is the average number of yes/no questions needed to pin down the outcome. One fair coin flip takes one question, so it should measure exactly 11 bit. It does:

H(Fair)=−(0.5log⁡20.5+0.5log⁡20.5)=1.H(\text{Fair}) = -\big(0.5 \log_2 0.5 + 0.5 \log_2 0.5\big) = 1 .

A fair four-sided die has four equally likely outcomes, which takes two questions: H=2H = 2 bits. And the loaded coin behaves as intuition demanded:

H(Loaded)=−(0.99log⁡20.99+0.01log⁡20.01)≈0.08 bits.H(\text{Loaded}) = -\big(0.99 \log_2 0.99 + 0.01 \log_2 0.01\big) \approx 0.08 \text{ bits}.

For a Boolean variable it is convenient to write B(q)B(q) for the entropy of something true with probability qq:

B(q)=−(qlog⁡2q+(1−q)log⁡2(1−q)).B(q) = -\big(q \log_2 q + (1-q) \log_2 (1-q)\big).
qqB(q)B(q)
0.501.0000
0.750.8113
0.900.4690
0.990.0808
1.000.0000

Symmetric about q=0.5q = 0.5, maximal there, and zero at certainty - exactly the shape we asked for. (By convention 0log⁡0=00 \log 0 = 0, which is also its limit.)

Python

Runs in your browser. The first run downloads the Python runtime (~10 MB), then it is cached.

Entropy and information

Everything in bits (base 2).

H(p) entropy
1.0000 bits
H(p, q) cross entropy
1.7370 bits
KL(p ‖ q) divergence
0.7370 bits

H(p, q) = H(p) + KL(p ‖ q) → 1.7370 = 1.0000 + 0.7370

Maximum for 2 outcomes: 1.0000 bits

p - the true distribution

A50.0%
B50.0%

q - the believed distribution

A90.0%
B10.0%

Presets

Entropy is highest when every outcome is equally likely and falls to zero once one outcome is certain. Cross entropy is what a model is trained to minimise; it splits exactly into the entropy of the data - which no model can remove - plus the KL divergence, the part caused by believing the wrong distribution.

B. Information gain: how much a question tells you

Entropy becomes useful the moment you ask what a test buys you. Suppose a training set has pp positive and nn negative examples. Its entropy is B ⁣(pp+n)B\!\left(\frac{p}{p+n}\right).

Now test an attribute AA with dd values, splitting the set into subsets E1,…,EdE_1, \dots, E_d, where EkE_k holds pkp_k positives and nkn_k negatives. Going down branch kk leaves B ⁣(pkpk+nk)B\!\left(\frac{p_k}{p_k+n_k}\right) bits still to resolve, and a random example takes that branch with probability pk+nkp+n\frac{p_k+n_k}{p+n}. So the expected entropy remaining after the test is

Remainder(A)=∑k=1dpk+nkp+n B ⁣(pkpk+nk),\text{Remainder}(A) = \sum_{k=1}^{d} \frac{p_k + n_k}{p + n}\, B\!\left(\frac{p_k}{p_k + n_k}\right),

and the information gain is the expected reduction in entropy:

Gain(A)=B ⁣(pp+n)−Remainder(A).\text{Gain}(A) = B\!\left(\frac{p}{p+n}\right) - \text{Remainder}(A).

Worked example. Twelve examples, six positive and six negative, so the starting entropy is B(0.5)=1B(0.5) = 1 bit exactly. An attribute splits them into a branch of 5 (4 positive, 1 negative) and a branch of 7 (2 positive, 5 negative).

Branch entropies: B(4/5)=B(0.8)=0.7219B(4/5) = B(0.8) = 0.7219 and B(2/7)=B(0.2857)=0.8631B(2/7) = B(0.2857) = 0.8631.

Weights: 5/12=0.41675/12 = 0.4167 and 7/12=0.58337/12 = 0.5833.

Remainder=0.4167×0.7219+0.5833×0.8631=0.8043,\text{Remainder} = 0.4167 \times 0.7219 + 0.5833 \times 0.8631 = 0.8043 , Gain=1.0000−0.8043=0.1957 bits.\text{Gain} = 1.0000 - 0.8043 = 0.1957 \text{ bits}.

So this attribute resolves about a fifth of a bit out of the one bit of uncertainty we started with - a real but modest improvement.

Gain near zero flags an irrelevant attribute. If a test splits the data into subsets whose class proportions all look like the parent's, it has told you nothing, and the arithmetic above returns approximately zero. That makes information gain a usable signal for pruning as well as for splitting.

The figure below runs the same arithmetic on a different split: Russell & Norvig's restaurant data, also twelve examples with six positive and six negative, and two of its attributes. Patrons gains 0.54090.5409 bits; Type gains exactly 00, the irrelevant attribute of the note above. Every branch is drawn as a bar and every number recomputed rather than quoted. The third control moves examples inside Patrons' one branch that is still mixed - the only place the gain can change without changing the question - and it shows something a single worked split cannot: the expected entropy left is highest when that branch is evenly split, while the gain is not simply lowest there, because moving an example changes the entropy the split started from as well.

Interactive: what a question is worth, in bits

Same twelve examples. Only the question changes.

NoneSomeFull
Entropy before
1.0000
Expected entropy after
0.4591
Information gain
0.5409
Branches
3 of 12

Six positive and six negative, so the set starts at 1.0000 bit of uncertainty. Patrons splits it three ways and two of the three branches come out already decided; only Full stays mixed, at B(1/3) = 0.9183. Weighted by how often an example lands in each branch, the expected entropy left is 0.4591, so the question is worth 0.5409 bits. A decision tree tries every attribute it can test and keeps the one with the largest gain: that greedy choice is the whole learning algorithm.

C. A terminology trap worth naming

Decision Trees and Ensembles introduces the splitting criterion

D=−∑k=1Kp^mklog⁡p^mkD = -\sum_{k=1}^{K} \hat p_{mk} \log \hat p_{mk}

and calls it cross-entropy, following the statistical-learning convention. Read against this article, that is the formula for entropy - a single distribution scored against itself.

Information theory reserves cross-entropy for a quantity involving two distributions. Both names are standard in their own literature, and neither is wrong, but they are not the same object and the collision causes real confusion. When you meet "cross-entropy" as a tree-splitting criterion, read it as node entropy; the next section is about the other thing.

D. Cross-entropy and KL divergence

Suppose the truth is pp but you act on a belief qq. Cross-entropy measures the average surprise you actually incur:

H(p,q)=−∑ip(xi)log⁡2q(xi).H(p, q) = -\sum_{i} p(x_i) \log_2 q(x_i) .

The weights are the true probabilities pp; the surprises are computed from your belief qq. If q=pq = p this collapses to H(p)H(p). Otherwise it is strictly larger - you are systematically more surprised than necessary.

The excess has its own name. The KL divergence between PP and QQ is

KL(P,Q)=∑iP(xi)log⁡2P(xi)Q(xi),\text{KL}(P, Q) = \sum_{i} P(x_i) \log_2 \frac{P(x_i)}{Q(x_i)} ,

and the three quantities are related by

H(p,q)=H(p)+KL(p ∥ q).H(p, q) = H(p) + \text{KL}(p \,\|\, q).

In words: your total surprise is the irreducible uncertainty in the world, plus a penalty for being wrong about it. Since H(p)H(p) is fixed by reality, minimising cross-entropy over your model is exactly minimising KL divergence from the truth.

Check it numerically. Let the truth be a fair coin, p=(0.5,0.5)p = (0.5, 0.5), while you believe q=(0.9,0.1)q = (0.9, 0.1):

H(p)=1.0000,H(p,q)=1.7370,KL(p ∥ q)=0.7370.H(p) = 1.0000, \qquad H(p, q) = 1.7370, \qquad \text{KL}(p \,\|\, q) = 0.7370 .

And 1.0000+0.7370=1.73701.0000 + 0.7370 = 1.7370, as claimed.

Python

Runs in your browser. The first run downloads the Python runtime (~10 MB), then it is cached.

KL is not a distance. It is not symmetric - KL(P,Q)≠KL(Q,P)\text{KL}(P, Q) \neq \text{KL}(Q, P) in general - and it does not satisfy the triangle inequality. Calling it a "divergence" rather than a distance is deliberate. Note also that it blows up if q(x)=0q(x) = 0 where p(x)>0p(x) > 0: assigning zero probability to something that then happens is infinitely surprising.

Below, the source is fixed and the model is yours to move. Watch which of the two read-outs refuses to budge: the entropy is a property of the source, and no setting of the sliders touches it. Everything above that floor is the divergence, and it is the only thing training can ever reduce. The second panel breaks the cost down symbol by symbol, which is where the arithmetic stops being an average and starts being a diagnosis - the bits are rarely wasted where you would guess.

Interactive: the bits a wrong model costs

The source is fixed. Move the model and watch the floor stay put.

0.000.300.60ABCD
source pmodel q

Where the bits go: p(x) log2(1/q(x))

0.000.601.20ABCD
unavoidablewasted
H(p)
1.4905 bits
H(p, q)
2.0000 bits
KL(p || q)
0.5095 bits
KL(q || p)
0.5952 bits

You are paying 0.5095 bits per symbol more than the source requires - 6.4% of a byte thrown away on every symbol, forever. Most of it comes from A: the model gives it a 2.00-bit code word and the source keeps producing it, which wastes 0.758 bits of the average on that symbol alone. Note that this is not the symbol the model gets most wrong - it is the one that is wrong and common.

E. Why classifiers are trained on cross-entropy

This is where the theory earns its keep. A classifier outputs a probability distribution over labels; the ground truth is also a distribution - usually all mass on one label. Training the model means making its distribution match the true one, and cross-entropy is precisely the quantity measuring the gap. Chollet puts it directly: cross-entropy is a quantity from information theory measuring the distance between probability distributions, here between the ground-truth distribution and the model's predictions, and it is usually the right choice when a model outputs probabilities.

With the truth one-hot on class cc, all terms but one vanish and the loss for that example is just

−log⁡q(c),-\log q(c),

the surprise assigned to the correct answer. Confident and right costs nearly nothing; confident and wrong costs a great deal. That asymmetry - supplied by the logarithm, not bolted on - is what makes cross-entropy a better training signal than accuracy, which is flat almost everywhere and gives gradient descent nothing to descend.

This is the same reasoning that motivates the log-loss objective in Logistic Regression and Classification: the loss is not an arbitrary convenient function, it is the expected surprise of the model's beliefs under the true distribution.

The same entropy also measures what a noisy channel can carry, which is where the figure below goes one step beyond this article. It is a binary channel that flips each bit with probability ff, and its capacity is 1−B(f)1 - B(f) bits per use. Set the flip probability to 0.1 and read 0.5310; to 0.5 and read exactly zero; past it, and watch the capacity climb back, because a channel that always lies can be inverted. The second panel is a separate example: two fair bits and a target that is their exclusive or, where either input alone carries exactly 0 bits about the target and the pair carries 1. And the processing slider sends the output through a second channel in series: its composite flip probability is f(1−g)+g(1−f)f(1-g) + g(1-f), never closer to certainty than ff was, so the information never rises.

Interactive: what a channel carries, and what it never gets back

Exact, from the joint distribution. No sampling anywhere.

1 bit0.5
Bits per use
0.5310
After processing
0.3199
Lost to processing
0.2111
Composite flip
0.1800

A channel that flips with probability 0.10 is right 90% of the time and still carries only 0.5310 bits per use. Accuracy and information are not the same currency. Now send the output through a second channel at 0.10: the composite flip probability is 0.1800 and what survives is 0.3199 bits. It fell, and it always will. That is the data processing inequality, shown here with a cascade because the arithmetic is checkable, but it holds for any function of the received signal whatever.

Key takeaways

  • Entropy H(V)=−∑kP(vk)log⁡2P(vk)H(V) = -\sum_k P(v_k)\log_2 P(v_k) is expected surprise, measured in bits; base 2 makes it the average number of yes/no questions.
  • A fair coin is exactly 11 bit, a fair four-sided die 22 bits, a 99% coin about 0.080.08 bits, and a certain outcome 00.
  • Information gain is the expected entropy reduction from a test; our split moved 11 bit to 0.80430.8043, a gain of 0.1957 bits.
  • Statistical learning calls the tree criterion −∑p^log⁡p^-\sum \hat p \log \hat p "cross-entropy"; information theory calls that entropy. Same formula, different names, genuine confusion.
  • Cross-entropy H(p,q)=−∑plog⁡qH(p,q) = -\sum p \log q scores a belief qq against a truth pp, and decomposes as H(p)+KL(p∥q)H(p) + \text{KL}(p \| q) - irreducible uncertainty plus a penalty for being wrong.
  • KL divergence is asymmetric and unbounded; it is not a distance.
  • Minimising cross-entropy is minimising KL from the truth, which is why it is the standard classification loss.

What's next

Entropy quantifies uncertainty inside a probabilistic model. A different tradition represents knowledge with statements that are simply true or false, and reasons about what must follow - Logic and Knowledge Representation.

References & further reading

  • Stuart Russell, Peter Norvig, Artificial Intelligence: A Modern Approach, Pearson (3rd edition), 2010· Kudos AI reference library
  • François Chollet, Deep Learning with Python, Manning (2nd edition, MEAP), 2020· Kudos AI reference library

Copyrighted works are cited for reference only and are not hosted here; please consult the publisher for access.

Related reading

4 min readProbability Foundations

Which Wrong Distribution Do You Want?

One bimodal target, one Gaussian, and two directions of the same divergence. Minimising KL(P||Q) puts the Gaussian across both modes with almost no mass where the target actually lives; minimising KL(Q||P) puts it on one mode at a value of 0.6931 nats, which is ln 2 to four decimals and not a coincidence. Each fit is judged catastrophic by the other objective, 2.0976 against 15.2799.

Machine LearningMathematics
7 min readInformation Theory

The Bound That Is Actually Reached

Entropy is not a summary of a distribution but a floor that the best code meets to the last decimal, the surcharge for using the wrong distribution is exactly the loss every classifier already minimises, and mutual information puts a hard ceiling on everything downstream of a sensor. Three results, each unusually sharp.

MathematicsMachine Learning
3 min readProbability Foundations

The Two Features That Look Like Noise

A variable that determines another with a correlation of exactly 0.0000000000, and a pair of features whose every pairwise mutual information with the target is exactly zero while the two together determine it completely. Univariate screening discards both, and the second case is the one that matters: the features it removes are removed because they matter.

Machine LearningMathematics
← Back to all articles