Decision Trees

Entropy, information gain, splits, depth, decision boundaries

10 lessons · 24 past questions · 60.5 marks in the papers collected here

Open the interactive lessons →Decision Trees past questions

Diagrams, animations, quizzes and checked solutions are on the interactive pages.

Contents

  1. Orientation: what you will learn and why
  2. Big picture: concept map, comparison table and the recipe
  3. Start here: the maths you need, from zero
  4. What a decision tree is
  5. Entropy: measuring how mixed a node is
  6. Calculating entropy in the exam: method, given logs, ranges
  7. Information gain: scoring a question
  8. Numeric features: questions of the form x ≤ θ
  9. Tree structure: depth, regions and decision boundaries
  10. Properties: depth, overfitting, stability

1. Orientation: what you will learn and why

A decision tree is a machine that asks a chain of yes/no questions about a data point and then announces a label. This topic teaches you how to measure how good a question is, how to pick the best one, and how to read and reason about the finished tree.

What you will be able to do

Why this topic matters for Quiz 2

The numbers below are counted only from the past-year questions (PYQs) supplied in this project. They are not a prediction.

What the PYQs askPYQsMarks
Computing entropy of a node, and facts about entropy1229.5
Information gain and choosing the best question412
Tree structure: depth, regions, boundaries, prediction715
True/false properties of decision trees14

In total this topic has 24 PYQs worth 60.5 marks, spread over 8 of the 10 papers in the bank (7 MCQ, 1 MSQ, 16 numeric). Entropy arithmetic alone is the largest block, so the first goal of this page is to make entropy completely automatic.

How this topic connects

Nothing from earlier topics is assumed, only school-level fractions and percentages. Two ideas here are used elsewhere: proportions of a class (Naive Bayes) and depth as a complexity knob that is chosen by cross-validation (see the Cross-validation topic).

Tags used on this site: [SRC] comes from your files, [DERIVED] is an answer I worked out because the bank gives no key, [ADDED] is my own explanation or example, [FLAG] marks a source error or ambiguity. A question I made up is called "exam-style [ADDED]", never a PYQ.
How to use this page

Read a section, press Show first step in each worked example and try to predict the next step before pressing Next step, then do the quick check at the end of the section. Attempt every PYQ yourself before opening its solution.

What is new in this upgrade [ADDED]

The route through this topic
what a tree isentropyinformation gaintree shape and overfitting

2. Big picture: concept map, comparison table and the recipe

[ADDED] Read this page first. It shows how entropy, information gain, thresholds and depth fit together.

The concept map

Nodepoints with labelsEntropy0 pure, 1 half-halfQuestionfeature ≤ threshold?Information gainparent − weightedPick the bestlargest gainRecurse on each childuntil pure, or depth limit reachedPredictionfollow Yes/No answers to a leaf; output its majority labelGeometry and riskeach question cuts space with a line parallel to an axis, so regions are boxes. More depth: more boxes, lower training error,higher risk of overfitting. Depth counts edges, not nodes.
A tree is grown greedily: score every candidate question by information gain, take the best, split, and repeat in each child. Prediction follows the answers to a leaf.

Decision tree next to the other models of the course

Decision treek-NNLinear modelNaive Bayes
Boundary shapeaxis-parallel rectangles (staircase)jagged to smooth, set by $k$one straight line / planestraight or curved
Needs training points to predict? (PYQ definition)no, the tree is the modelyesnono
Cost of one predictionfollow one path: at most the depth$n$ distancesone dot producta product over features
Main knobdepth, minimum leaf size$k$penalty $\lambda$smoothing
Sensitive to feature scale?no (only the order matters)yesweights adjustno
Easy to read as rules?yesnoweightstables of probabilities
Overfits whentoo deep (memorises points)$k$ too smalltoo many featuresrarely

The exam recipe, in five lines

Mnemonics and recognition cues [ADDED]

Analogy and its limit [ADDED]

AnalogyWhat it explains wellWhere it breaks
The game of twenty questionswhy we pick the question that splits the possibilities most evenly (largest information gain)a tree asks only threshold questions about one feature at a time, never "is it a mix of A and B?"; and it picks questions greedily, one at a time
Sorting mail into binsleaves are bins; a point lands in exactly onebins are boxes aligned with the axes, so a slanted boundary needs many tiny bins

Recall questions

Entropy of a node with $3$ ones out of $12$ points?

$p=0.25$, so $H=0.8113$ bits.

Can information gain be negative?

No. The weighted child entropy is never above the parent entropy.

A tree with a root and two leaves: depth?

$1$ (one edge on the longest path).

Tips and tricks
  • For every split ask: how many points go each way, and how mixed is each side?
  • Weight each child's entropy by its share of the points.

3. Start here: the maths you need, from zero

Picture it

A logarithm counts how many halvings or doublings it takes.

Only four small ideas are needed. If any feels shaky, spend five minutes here; everything later depends on it.

Idea 1. A proportion is "part divided by whole"

Definition: proportion

If a node has $n$ data points and $k$ of them have label $1$, the proportion of label 1 is $p = k/n$. The proportion of label 0 is $1-p$, because every point has exactly one of the two labels.

Plain words: "what fraction of the points are positive?" You can also be told a percentage; $75\%$ means $p=0.75$.

Worked example: reading a proportion

Problem. A node has 3 points with label 1 and 5 points with label 0. What is $p$?

What is asked. The proportion of label 1.

Idea / formula used. $p = \dfrac{\text{points with label 1}}{\text{all points}}$.

Count all points: $3 + 5 = 8$. (The denominator is the total, not the number of zeros.)
Divide: $p = 3/8 = 0.375$.
The proportion of label 0 is $1 - p = 5/8 = 0.625$. Check: $0.375 + 0.625 = 1$ ✓.
Answer: $p = 3/8 = 0.375$
Dividing by the number of zeros ($3/5$) instead of the total. Always divide by the whole node.

Idea 2. What $\log_2$ means

Definition: log base 2

$\log_2(x)$ is the power to which $2$ must be raised to get $x$. In symbols, $\log_2(x) = y$ means $2^{y} = x$.

Plain words: "how many times do I double 1 to reach $x$?" Memorise this small table, because the exam uses these values all the time.

$x$$2^{?} = x$$\log_2(x)$
8$2^3$3
4$2^2$2
2$2^1$1
1$2^0$0
1/2 = 0.5$2^{-1}$-1
1/4 = 0.25$2^{-2}$-2
1/8 = 0.125$2^{-3}$-3

Notice that for any number between 0 and 1, $\log_2$ is negative. Proportions are always between 0 and 1, so the logs in this topic are always negative, and the minus signs in the entropy formula make the final answer positive.

For numbers that are not powers of 2 use a calculator: $\log_2(x) = \ln(x)/\ln(2)$ (or press the log2 button). Some values you will meet: $\log_2(0.75) = -0.415$, $\log_2(0.6) = -0.737$, $\log_2(0.4) = -1.322$, $\log_2(0.25) = -2.000$.

Trap: natural log

Do not use $\ln$ or $\log_{10}$ unless the question says so. Using $\ln$ gives an answer that is only $0.693$ times the correct one (too small). The PYQs say "use $\log_2$" or give $\log_2$ values.

Worked example: using the log table

Problem. Compute $\log_2(1/8) + \log_2(4)$.

What is asked. A sum of two logs.

Idea / formula used. $\log_2(1/8) = -3$ and $\log_2(4) = 2$ from the table.

$1/8 = 2^{-3}$, so $\log_2(1/8) = -3$.
$4 = 2^{2}$, so $\log_2(4) = 2$.
Add: $-3 + 2 = -1$.
Answer: $-1$
Thinking $\log_2(1/8)$ is $+3$. Fractions below 1 give negative logs.

Idea 3. A weighted average gives more say to bigger groups

Definition: weighted average

If group A has $n_A$ items with average $a$ and group B has $n_B$ items with average $b$, the combined average is $\dfrac{n_A\,a + n_B\,b}{n_A+n_B} = \dfrac{n_A}{n_A+n_B}a + \dfrac{n_B}{n_A+n_B}b$. The fractions $\frac{n_A}{n_A+n_B}$ are the weights and they add up to 1.

Worked example: class averages

Problem. A class of 10 students averages 60 marks, another class of 30 students averages 80. What is the average of all 40 students?

What is asked. The combined average.

Idea / formula used. Weighted average with weights $10/40$ and $30/40$.

Weights: $10/40 = 0.25$ and $30/40 = 0.75$.
Combine: $0.25 \times 60 + 0.75 \times 80 = 15 + 60 = 75$.
Compare with the plain average of the two averages, $(60+80)/2 = 70$. It is different, because the bigger class counts more.
Answer: $75$, not $70$
Averaging the two averages without weights. This exact slip is the most common error in information gain, where the two "averages" are the entropies of the two child nodes.

Idea 4. The convention $0 \cdot \log_2 0 = 0$

$\log_2(0)$ is not defined (no power of 2 equals 0). But a class with proportion 0 contributes nothing, so we agree that the term $0\cdot\log_2 0$ is $0$. Result: a node whose points all have the same label has entropy exactly $0$.

Why this matters in the exam: when a child node is pure you do not compute any log; you just write entropy = 0.
Tips and tricks
  • $\log_2(a/b)=\log_2 a-\log_2 b$, so a few given logs cover most fractions.
  • $0\log_2 0$ counts as 0.
Memory line

Entropy is the average surprise, with surprise measured by a logarithm.

From counts to entropy
count each classturn counts into fractionstake log base 2weight and add

4. What a decision tree is

A decision tree classifier answers one question at a time. Each answer sends the data point down a branch, until it reaches a final box that states the label.

Everyday analogy

The game "20 Questions": you narrow down an animal by asking yes/no questions, each chosen to cut the possibilities as much as possible. A decision tree plays that game with data points. Where the analogy stops: in 20 Questions a human chooses each question by guessing; a decision tree chooses it by a numerical score (information gain, later in this topic).

Vocabulary

WordMeaningIn the picture below
NodeA box holding a group of training points.Every box
Root (top node)The first node; it holds all training points.The box "x ≤ 3 ?"
Internal node / question / splitA node that asks a yes/no question about one feature, such as $x \le 3$.Boxes with a question
BranchOne answer (Yes or No) leading to a child node.Lines labelled Yes / No
LeafA node that asks nothing and states a label.Round boxes 0 and 1
Depth of the treeNumber of edges on the longest path from root to a leaf (this is the definition the PYQs use).Here it is 2
YesNoYesNo01x ≤ 1 ?1x ≤ 3 ?
A small tree. Root asks "x ≤ 3?". If No, predict 1 at once (path length 1). If Yes, ask "x ≤ 1?" and then predict 0 or 1 (path length 2). The longest path has 2 edges, so the depth is 2. A tree that is a single leaf has depth 0; one question gives depth 1.

How a tree predicts

Definition: prediction by following the tree

Start at the root. Answer its question using the data point's feature values. Follow the matching branch. Repeat until you reach a leaf. The label written in the leaf is the prediction.

Example with the tree above: the point $x = 2$: is $2 \le 3$? Yes. Is $2 \le 1$? No. The "No" branch of the lower node is leaf 1, so the prediction is 1. The point $x = 5$: is $5\le 3$? No, so the prediction is the leaf on that branch, 1. The point $x=0.5$: Yes, then $0.5 \le 1$ Yes, leaf 0.

A leaf's label is the majority label of the training points that reached it. If a leaf is pure (all one label), the label is obvious.

How a tree is built (the idea in five lines)

  1. Put all training points in the root.
  2. If the node is pure (all one label), stop: make it a leaf.
  3. Otherwise, list the possible questions (for a numeric feature: $x \le \theta$ for several thresholds $\theta$).
  4. Score each question by its information gain, and keep the one with the highest score.
  5. Split the points by that question, and repeat steps 2 to 4 on each child.

This is called a greedy method: it picks the best question for the current node only, without planning ahead. Sections 4 to 7 teach the score in step 4. Section 8 comes back to the finished tree and its shape.

Trap: depth counts edges, not nodes

The tree in the picture has 3 levels of boxes but depth 2. If a PYQ says "recall that depth is the number of edges on the longest path", count the lines, not the boxes.

Worked example (simple): follow a tree

Root asks $x_1\le3$. Yes leads to a second question $x_2\le2$ (Yes: label 1, No: label 0). No leads straight to the leaf label 1. Point $(2,3)$: $2\le3$ Yes, then $3\le2$ No, so label 0. Point $(5,1)$: $5\le3$ No, so label 1. Point $(3,1)$ sits on the threshold: $3\le3$ is Yes, then $1\le2$ Yes, so label 1. The tree has depth 2, three leaves and two questions (questions $=$ leaves $-1$).

Tips and tricks
  • Trace one point from the root, writing the answer at each question node.
  • Leaves give predictions; internal nodes only ask questions.
Memory line

Questions inside, answers at the leaves.

5. Entropy: measuring how mixed a node is

To choose a good question we need a number that says how impure (mixed) a node is. The number used in this course is entropy.

Intuition

Imagine a node with 10 patients. If all 10 are sick, you can predict "sick" for the next patient with no doubt. The node is pure. If 5 are sick and 5 are healthy, the node is a coin toss; you are as unsure as you can possibly be. Entropy is a score of that unsureness: 0 when pure, largest when evenly mixed.

Everyday analogy

A bag of marbles. If every marble is red, guessing the colour of a marble you pull out takes zero yes/no questions. If half are red and half blue, you need exactly one yes/no question ("is it red?"). Entropy counts such questions, on average, and its unit is called a bit. Where the analogy stops: for an uneven bag such as 80% red the entropy is $0.722$ bits, a fraction of a question. That is an average over very many draws using clever question orders, not a literal count for one marble.

Definition

Definition: entropy of a two-class node

Let $p$ be the proportion of points with label 1 in the node. The entropy of the node is

$$E(p)\;=\;-p\log_2 p\;-\;(1-p)\log_2(1-p)$$

(the PYQs also write it as $H$ or "Entropy$(D)$"; it is the same thing).

In plain words: for each class, take its proportion, multiply by $-\log_2$ of that proportion (a big number if the class is rare, because rare classes are surprising), and add the two classes together.

Formula: entropy

$$E(p) = -p\log_2 p - (1-p)\log_2(1-p)$$

  • $p$: proportion of label 1 in the node, $0\le p\le 1$.
  • $1-p$: proportion of label 0.
  • $\log_2$: log base 2 (negative for numbers below 1; the minus signs make each term positive).
  • Conditions: two classes; if $p=0$ or $p=1$ then $E=0$ by the convention $0\log_2 0=0$.
  • The result is in bits, between 0 and 1 for two classes.

Small substituted example: $p = 1/4$. $E = -\tfrac14(-2) - \tfrac34(-0.415) = 0.5 + 0.311 = 0.811$.

Why this matters in the exam: 12 of the 24 PYQs in this topic are direct entropy calculations or facts about entropy.

Why the formula has this shape (why it works)

The term $-\log_2 p$ is the "surprise" of seeing a class that occurs with proportion $p$: for $p = 1/2$ the surprise is 1 bit, for $p=1/8$ it is 3 bits, for $p = 1$ it is 0 bits. Multiplying by $p$ weights each surprise by how often it happens. So entropy is the average surprise. A pure node has no surprise at all; an evenly mixed node has the most.

Values to remember

$p$Entropy $E(p)$
0.000.000
0.100.469
0.200.722
0.250.811
0.300.881
0.400.971
0.501.000
0.600.971
0.700.881
0.750.811
0.800.722
0.900.469
1.000.000
0.00.10.20.30.40.50.60.70.80.91.000.51p = 0.5, H = 1 (maximum)p = 0.25, H = 0.811p = 0.1, H = 0.469p = proportion of label 1 in the nodeentropy H(p) in bits
The entropy curve. It starts at 0 when $p=0$ (all label 0), climbs to its peak of 1 at $p=0.5$, and falls back to 0 at $p=1$. The curve is a mirror image around $p=0.5$. Three highlighted points show $p=0.1$ ($0.469$), $p=0.25$ ($0.811$) and the peak. Reading it: the closer a node is to 50/50, the higher the entropy.

Four facts that PYQs test

  1. Pure node: $E = 0$. All points have one label.
  2. Maximum at $p=0.5$: $E=1$. Equal numbers of positives and negatives. (Two classes, log base 2.)
  3. Symmetry: $E(p) = E(1-p)$. A node with 75% positives and one with 75% negatives have the same entropy, $0.811$. So you do not need to know which class is the majority.
  4. Only proportions matter, not counts. 3 positives and 1 negative, and 300 positives and 100 negatives, both have $p=0.75$ and entropy $0.811$.
Extra, not asked in PYQs [ADDED]

For $K$ classes with proportions $p_1,\dots,p_K$, entropy is $-\sum_k p_k\log_2 p_k$, and its maximum is $\log_2 K$ (when all classes are equally likely). All PYQs in this bank use two classes.

Worked examples

Worked example: simple: pure and perfectly mixed nodes

Problem. Find the entropy of (a) a node with 5 points of label 1 and 0 of label 0; (b) a node with 4 points of label 1 and 4 of label 0.

What is asked. Two entropies.

Idea / formula used. $E(p)$ with the conventions for $p=0$ or $1$.

(a) $p = 5/5 = 1$. All points share a label, so the node is pure and $E = 0$. No log is needed.
(b) $p = 4/8 = 0.5$. $E = -0.5\log_2 0.5 - 0.5\log_2 0.5 = -0.5(-1) - 0.5(-1) = 0.5 + 0.5 = 1$.
Sanity check: pure gives the minimum (0), evenly mixed gives the maximum (1). ✓
Answer: (a) $0$; (b) $1$
Writing $E=1$ for the pure node. Pure means "no doubt", which is entropy 0.
Worked example: foundation: a 1-in-4 node

Problem. A node has 1 point with label 1 and 3 points with label 0. Compute its entropy to 3 decimals.

What is asked. Entropy of the node.

Idea / formula used. $E(p) = -p\log_2 p - (1-p)\log_2(1-p)$ with $p = 1/4$.

Proportions: $p = 1/4 = 0.25$ and $1-p = 3/4 = 0.75$.
Logs: $\log_2(0.25) = -2$ (since $2^{-2}=0.25$) and $\log_2(0.75) = -0.415$ (calculator).
First term: $-0.25 \times (-2) = 0.500$.
Second term: $-0.75 \times (-0.415) = 0.311$.
Add: $0.500 + 0.311 = 0.811$.
Answer: $E = 0.811$ bits
Forgetting that the second term also has a minus sign, which gives a negative second term and a wrong total.
Worked example: exam level: counts, not proportions

Problem. A node holds 50 points: 20 have label 1 and 30 have label 0. Find its entropy to 3 decimals.

What is asked. Entropy from counts.

Idea / formula used. First convert counts to a proportion, then use $E(p)$.

Total $= 20 + 30 = 50$. $p = 20/50 = 0.4$, $1-p = 0.6$.
$\log_2(0.4) = -1.322$ and $\log_2(0.6) = -0.737$.
$-0.4(-1.322) = 0.529$ and $-0.6(-0.737) = 0.442$.
Sum: $0.529 + 0.442 = 0.971$.
Check with symmetry: $E(0.4) = E(0.6)$. From the table, $E(0.6) = 0.971$ ✓. The answer is close to 1 because 40/60 is nearly even.
Answer: $E = 0.971$ bits
Plugging the counts 20 and 30 straight into the formula instead of the proportions 0.4 and 0.6.
Worked example: tricky: the minority could be either class

Problem. A node has 70% of its points in one class and 30% in the other. You are not told which class is the larger. Can you find the entropy? If yes, what is it?

What is asked. Whether entropy is determined, and its value.

Idea / formula used. Symmetry $E(p) = E(1-p)$.

Case 1: label 1 is the 70% class, so $p=0.7$. Case 2: label 1 is the 30% class, so $p = 0.3$.
Case 1: $E(0.7) = -0.7\log_2 0.7 - 0.3\log_2 0.3 = 0.7(0.515) + 0.3(1.737) = 0.881$.
Case 2: $E(0.3) = -0.3\log_2 0.3 - 0.7\log_2 0.7$. It is the same two terms in the other order, so it also equals $0.881$.
Both cases agree, so the missing information does not matter.
Answer: Yes. $E = 0.881$ bits
Answering "not enough information" because the majority class is unnamed. Entropy only needs the split of the proportions, not the names.

Common mistakes with entropy

Entropy ladder [ADDED]

Proportion of label 1Entropy (bits)Reading
0.51.0000most mixed
0.250.8113mostly one class
0.10.4690nearly pure
00.0000pure

The curve is symmetric: $p=0.1$ and $p=0.9$ give the same entropy, because it does not matter which class is called positive. It falls fast near the ends ($0.5\to0.25$ loses 0.189 bits) and slowly near the middle.

Are $p=0.2$ and $p=0.8$ equally mixed?

Yes, entropy is symmetric in $p\leftrightarrow1-p$.

What is the entropy of a node with $5$ ones and $0$ zeros?

$0$ (pure; $0\log_20=0$).

Memory line

Pure node 0, even two-class node 1.

Another impurity measure: Gini [ADDED]

Entropy is the course's main measure, and other measures such as the Gini index can score a question too. For two classes, $\text{Gini}(p)=2p(1-p)$: it is $0$ for a pure node and largest, $0.5$, at $p=0.5$. Use it only when a question names it.

Worked example: Gini gain

Parent $5$ and $5$: Gini $0.5$. Left child $4$ of one class: $0$. Right child $1$ and $5$: $2\cdot\tfrac16\cdot\tfrac56=0.278$. Weighted: $0.6\times0.278=0.167$. Gain $=0.5-0.167=0.333$.

6. Calculating entropy in the exam: method, given logs, ranges

Picture it

A surprise meter: a certain outcome surprises nobody.

Questions rarely ask "what is entropy?". They hide it inside a story (loans, patients, spam). This section is the exact routine.

The four-step routine

  1. Find the two counts of the node in question (which node: root? left child? right child?).
  2. Convert to $p$ = (count of the class you call positive) / (total). Either class works by symmetry.
  3. Evaluate $-p\log_2 p-(1-p)\log_2(1-p)$ with a calculator, or with the log values printed in the question.
  4. Sanity check: the answer must be between 0 and 1. Near-even nodes give near 1; lopsided nodes give small values. Compare with the table in the previous section.

When the question gives you log values

Several PYQs print values such as $\log_2(0.4) = -1.32$ and $\log_2(0.6) = -0.74$. Use exactly those numbers: they are rounded, so your result may differ from a calculator in the 3rd decimal place, and the accepted answer is a range that allows for this.

Worked example: entropy with printed logs

Problem. A node has 40% label 1 and 60% label 0. The question says: use $\log_2(0.4) = -1.32$ and $\log_2(0.6) = -0.74$. Find the entropy to two decimals.

What is asked. Entropy using the printed logs.

Idea / formula used. $E = -p\log_2 p - (1-p)\log_2(1-p)$ with the printed values.

$p = 0.4$, $1-p = 0.6$.
$-0.4 \times (-1.32) = 0.528$.
$-0.6 \times (-0.74) = 0.444$.
Total $= 0.972 \approx 0.97$.
Calculator check: exact $E(0.4) = 0.971$, so the printed-log answer is off by only $0.001$. Both round to $0.97$.
Answer: $\approx 0.97$ (exact value 0.971)
Mixing printed and calculator logs in one question. Pick one source and stay with it. If the printed logs do not contain what you need, use the calculator.

Rounding and "accepted range"

The bank lists answers like "0.95 to 0.99". Any value inside the range is marked right. So: do not panic about the third decimal; do panic about a wrong formula. When asked for "three decimal places" give three (e.g. 0.811); when asked for two, give two.

Entropies worth memorising

Node split$p$Entropy
50 : 500.51.000
60 : 40 (or 40 : 60)0.60.971
70 : 300.70.881
75 : 250.750.811
80 : 200.80.722
90 : 100.90.469
100 : 010.000
Trap: extra information in the story

A question may describe four features, hundreds of applicants and a bank. For the root entropy only the class counts matter; features and loan amounts are decoration.

Tips and tricks
  • Entropy is symmetric: $H(p)=H(1-p)$, with its maximum 1 at $p=0.5$ and 0 at $p=0$ or $1$.
  • Useful values: $H(0.1)\approx0.469$, $H(0.25)\approx0.811$, $H(1/3)\approx0.918$, $H(0.4)\approx0.971$.
Memory line

Symmetric, highest in the middle, zero at the ends.

Entropy in the exam
find the class share puse H(p) = H(1 - p)plug in the given logscheck the answer lies in 0 to 1

7. Information gain: scoring a question

Entropy scores one node. A question turns one node into two children, so we compare the parent's entropy with the children's entropy. The drop is the information gain.

Intuition

Before the question you are unsure (entropy of the parent). After the question you are in one of two smaller groups, each hopefully cleaner. Information gain is "how much less unsure am I, on average?" A great question leaves two nearly pure groups; a useless question leaves two groups that look just like the parent.

Everyday analogy

Sorting a mixed bag of red and blue beads into two cups. A good sorting rule (e.g. "is it heavy?") puts mostly red in one cup and mostly blue in the other. A bad rule (e.g. "is it Tuesday?") leaves both cups looking like the original bag. Where the analogy stops: in the tree the "cups" are the two child nodes, and a big cup must count more than a small cup when we judge the result.

Definition and formula

Definition: information gain of a question

Let $D$ be the set of points in the parent node. A question splits $D$ into $D_{\text{Yes}}$ (points answering Yes) and $D_{\text{No}}$ (points answering No). Let $\gamma$ be the fraction of points that go to the Yes side: $\gamma = |D_{\text{Yes}}|/|D|$. Then

$$\text{IG} = E(D) - \gamma\,E(D_{\text{Yes}}) - (1-\gamma)\,E(D_{\text{No}})$$

In plain words: parent entropy minus the size-weighted average of the two child entropies. This is exactly the formula printed in the Mar 2025 PYQ ("Information Gain = Entropy(D) − γ Entropy(D_Yes) − (1−γ) Entropy(D_No)").

Formula: information gain

$$\text{IG}=E(D)-\sum_{\text{children }c}\frac{n_c}{n}\,E(D_c)$$

  • $E(D)$: entropy of the parent node.
  • $n$: number of points in the parent; $n_c$: number in child $c$; so $n_c/n$ is the child's weight, and the weights sum to 1.
  • $E(D_c)$: entropy of child $c$.
  • Conditions: every point goes to exactly one child, so $n_c$'s add to $n$. For a two-way split the sum has two terms and $\gamma = n_{\text{Yes}}/n$.

Small substituted example: parent $E=1$; children of sizes 3 and 1 with entropies $0.918$ and $0$ give $\text{IG} = 1 - \tfrac34(0.918) - \tfrac14(0) = 0.311$.

Why this matters in the exam: 4 PYQs ask for information gain or for the comparison of two questions, and several others are its building blocks (child entropies).

The six steps

  1. Count the parent: (positives, negatives).
  2. Compute the parent entropy $E(D)$.
  3. Apply the question; write the counts of each child: (positives, negatives) for Yes and for No.
  4. Compute the entropy of each child (pure child $\Rightarrow$ 0).
  5. Compute the weights $\gamma = n_{\text{Yes}}/n$ and $1-\gamma$.
  6. Weighted sum of child entropies; subtract from $E(D)$.

What information gain can and cannot be

Worked examples

Worked example: simple: a perfect question

Problem. A node has 4 points with label 1 and 4 with label 0. A question sends all four 1s to Yes and all four 0s to No. Find the information gain.

What is asked. IG of that question.

Idea / formula used. $\text{IG} = E(D) - \gamma E(D_{\text{Yes}}) - (1-\gamma)E(D_{\text{No}})$.

Parent: $(4,4)$, $p=0.5$, so $E(D) = 1$.
Yes child: $(4,0)$, pure, $E=0$. No child: $(0,4)$, pure, $E=0$.
Weights: $\gamma = 4/8 = 0.5$ and $1-\gamma = 0.5$.
$\text{IG} = 1 - 0.5(0) - 0.5(0) = 1$.
Answer: $\text{IG}=1$ (the largest possible here)
Saying IG is 0 because the children have entropy 0. The children's entropy is 0; the gain is the parent's entropy minus that.
Worked example: exam level: which of two questions is better?

Problem. A node has 20 points of label 1 and 20 of label 0. Question A sends $(15\text{ of label 1},\,5\text{ of label 0})$ to Yes and $(5,\,15)$ to No. Question B sends $(20,\,10)$ to Yes and $(0,\,10)$ to No. Which question has higher information gain? Give both gains to 3 decimals.

What is asked. IG(A), IG(B), and the winner.

Idea / formula used. IG with weights; compare.

Parent $(20,20)$: $p=0.5$, $E(D)=1$ (same for both questions).
Question A. Yes child $(15,5)$: $p = 0.75$, $E = 0.811$. No child $(5,15)$: $p=0.25$, $E=0.811$. Sizes 20 and 20, so weights $0.5$ and $0.5$.
Weighted entropy of A: $0.5(0.811) + 0.5(0.811) = 0.811$. So $\text{IG}(A) = 1 - 0.811 = 0.189$.
Question B. Yes child $(20,10)$: size 30, $p = 20/30 = 0.667$, $E = 0.918$. No child $(0,10)$: size 10, pure, $E = 0$. Weights $30/40=0.75$ and $10/40 = 0.25$.
Weighted entropy of B: $0.75(0.918) + 0.25(0) = 0.689$. So $\text{IG}(B) = 1 - 0.689 = 0.311$.
Compare: $0.311 > 0.189$, so Question B is better. It produces one perfectly pure child, even though its other child is large and mixed.
Answer: $\text{IG}(A)=0.189$, $\text{IG}(B)=0.311$; ask Question B first.
Preferring A because "both children are fairly clean". A pure child of weight 0.25 is worth more than two moderately clean children here. The numbers decide, not the feel.
Worked example: tricky: forgetting the weights

Problem. A node has 6 points of label 1 and 2 of label 0. A question gives Yes child $(1,1)$ and No child $(5,1)$. Compute the information gain correctly, and show what goes wrong if the two child entropies are averaged without weights.

What is asked. The correct IG, and the symptom of the unweighted error.

Idea / formula used. Weights are the child sizes divided by the parent size.

Parent $(6,2)$: $p = 0.75$, $E(D) = 0.811$.
Yes child $(1,1)$: size 2, $p=0.5$, $E=1$. No child $(5,1)$: size 6, $p = 5/6$, $E = 0.650$.
Correct weights: $2/8 = 0.25$ and $6/8 = 0.75$. Weighted entropy $= 0.25(1) + 0.75(0.650) = 0.738$.
$\text{IG} = 0.811 - 0.738 = 0.074$.
Wrong way: plain average $(1 + 0.650)/2 = 0.825$, giving $0.811 - 0.825 = -0.014$, which is negative. Since IG can never be negative, a negative result is your alarm that the weights were skipped.
Answer: $\text{IG} = 0.074$
Averaging the child entropies with equal weight. The tiny child (2 points) is not allowed to count as much as the big child (6 points).
Worked example: tricky: a useless question has zero gain

Problem. A node has 4 points of label 1 and 2 of label 0. A question sends $(2,1)$ to Yes and $(2,1)$ to No. What is the information gain?

What is asked. IG of a question that does not change the mix.

Idea / formula used. If the children have the same proportion as the parent, nothing was learned.

Parent $(4,2)$: $p = 2/3$, $E(D) = 0.918$.
Yes child $(2,1)$: $p = 2/3$, $E = 0.918$. No child $(2,1)$: also $0.918$. Weights $0.5, 0.5$.
$\text{IG} = 0.918 - 0.5(0.918) - 0.5(0.918) = 0$.
Answer: $\text{IG} = 0$
Thinking IG must be positive for any split that separates points into two non-empty groups. Splitting into groups is not enough; the groups must differ in their mix.

Choosing among many thresholds: a bar chart [ADDED]

Ten points on a line: $x=1,\dots,10$ with labels $0,0,0,1,0,1,1,1,1,1$ (six ones). Parent entropy: $H(0.6)=0.9710$. Every threshold between neighbouring values is a candidate.

0.144x ≤ 1.50/1 | 6/90.322x ≤ 2.50/2 | 6/80.557x ≤ 3.50/3 | 6/70.256x ≤ 4.51/4 | 5/60.610x ≤ 5.51/5 | 5/50.420x ≤ 6.52/6 | 4/40.281x ≤ 7.53/7 | 3/30.171x ≤ 8.54/8 | 2/20.079x ≤ 9.55/9 | 1/1Information gain of each threshold questionunder each bar: ones/points on the Yes side | on the No sidebest
Information gain for each cut. The best is $x\le5.5$ with gain $0.610$. It is the cut where the labels switch cleanly: the Yes side has one 1 among five points, the No side is pure.
QuestionOnes among Yes sideOnes among No sideInformation gain
$x\le1.5$0 of 16 of 90.1445
$x\le2.5$0 of 26 of 80.3219
$x\le3.5$0 of 36 of 70.5568
$x\le4.5$1 of 45 of 60.2564
$x\le5.5$1 of 55 of 50.6100
$x\le6.5$2 of 64 of 40.4200
$x\le7.5$3 of 73 of 30.2813
$x\le8.5$4 of 82 of 20.1710
$x\le9.5$5 of 91 of 10.0790

Worked check for $x\le5.5$. Yes side: $5$ points, one $1$: $H(0.2)=0.7219$. No side: $5$ points, all $1$: $H=0$. Weights $5/10$ and $5/10$. Weighted child entropy $=0.5\cdot0.7219+0.5\cdot0=0.3610$. Gain $=0.9710-0.3610=0.6100$ ✓ (the code recomputes all nine).

Note the pattern: the best cut sits at a place where the label changes. A cut inside a run of equal labels (for example $x\le1.5$ or $x\le8.5$) has a smaller gain.

The weights: a common slip, as a table [ADDED]

Child sizes (of 10)Correct weightsCommon wrong weights
$5$ and $5$$0.5,\ 0.5$$0.5,\ 0.5$ (no difference)
$2$ and $8$$0.2,\ 0.8$$0.5,\ 0.5$ (plain average)
$1$ and $9$$0.1,\ 0.9$$0.5,\ 0.5$ (plain average)
Parent entropy $0.97$, weighted child entropy $0.97$. Information gain?

Zero. The question tells you nothing.

Which question wins: gain $0.31$ or gain $0.42$?

The one with $0.42$.

Interactive explorer

Set the label counts in the two children of a split. The curve is the entropy $E(p)$; the dots show the parent and both children, and the table gives the information gain.

Tips and tricks
  • A split that leaves both children with the parent's class share has gain 0.
  • When comparing splits of one node, the smallest weighted child entropy wins; the parent term is common to all.
Memory line

Gain is parent entropy minus the weighted child entropy.

8. Numeric features: questions of the form x ≤ θ

Picture it

A height bar at a ride: everyone is on one side of the line or the other.

For a numeric feature such as income or temperature there is no fixed list of questions. The tree tries thresholds $\theta$ and asks "$x \le \theta$?". Yes goes left, No goes right.

Definition: threshold question

A question $x\le\theta$ splits the points into $D_{\text{Yes}}=\{x_i\le\theta\}$ and $D_{\text{No}}=\{x_i>\theta\}$. For several features, $f_k\le\theta$ uses feature number $k$.

Two observations make this practical:

  1. Only the order of the points matters. Any two thresholds that sit between the same pair of neighbouring values give the same split and the same IG. For example, with sorted values $2,4,6,\dots$ the thresholds $4.1$, $5$ and $5.9$ all give "$\{2,4\}$ versus the rest".
  2. So the candidate questions are one threshold per gap between neighbouring sorted values; the midpoint (for example $(4+6)/2=5$) is the usual choice [ADDED].

Procedure

  1. Sort the points by the feature.
  2. List the thresholds (midpoints of the gaps).
  3. For each, write the Yes counts and No counts, then the IG (six steps from the last section).
  4. Pick the largest IG. A tie means either question is equally good as far as the score goes.
Worked example: simple: a clean cut

Problem. Six points: $x = 2,4,6,8,10,12$ with labels $0,0,0,1,1,1$. Find the best threshold.

What is asked. The threshold with the highest IG.

Idea / formula used. Compute IG for each gap midpoint $3,5,7,9,11$.

Parent $(3,3)$, $E(D) = 1$.
The labels switch between 6 and 8, so try $\theta = 7$: Yes $=\{2,4,6\}$ all label 0 (pure), No $=\{8,10,12\}$ all label 1 (pure).
$\text{IG}(7) = 1 - 0.5(0) - 0.5(0) = 1.000$. This is the maximum possible, so no other threshold can beat it.
Check one other, $\theta=5$: Yes $=\{2,4\}$ (pure), No $=\{6,8,10,12\}$ has $(3\text{ ones},1\text{ zero})$, $E = 0.811$, weight $4/6$. $\text{IG}(5) = 1 - \tfrac46(0.811) = 0.459$, smaller than 1 ✓.
Answer: Best threshold: any $\theta$ between 6 and 8 (e.g. $7$), $\text{IG}=1$
Choosing the threshold by the first label change without computing; sometimes a threshold that does not sit at the label change has equal or better IG on messy data.
24681012357911filled = label 1, hollow = label 0, dashed = candidate threshold
The six points of the next example on a number line (labels 0,0,1,0,1,1) with the five candidate thresholds marked. A threshold splits the line into a left part (Yes) and a right part (No).
Worked example: exam level: a table of candidates, and a tie

Problem. Six points: $x = 2,4,6,8,10,12$ with labels $0,0,1,0,1,1$. Compare all candidate thresholds and pick the best.

What is asked. IG for each threshold.

Idea / formula used. IG = parent entropy $-$ weighted child entropy for each $\theta$.

Parent: three 1s and three 0s, $E(D)=1$. Since $E(D)$ is common to all candidates, we compare weighted child entropy (smaller is better).
Fill the table (counts are written as (label-1 count, label-0 count)):
$\theta$Yes countsNo countsWeighted child entropyIG
3(0, 1)(3, 2)0.8090.191
5(0, 2)(3, 1)0.5410.459
7(1, 2)(2, 1)0.9180.082
9(1, 3)(2, 0)0.5410.459
11(2, 3)(1, 0)0.8090.191
Worked row, $\theta=5$: Yes $=\{2,4\}$ gives $(0,2)$, pure, $E=0$. No $=\{6,8,10,12\}$ gives $(3,1)$, $E=0.811$, weight $4/6$. Weighted entropy $= 0.541$, so $\text{IG}=0.459$.
Worked row, $\theta=9$: Yes $=\{2,4,6,8\}$ gives $(1,3)$, $E=0.811$, weight $4/6$. No $=\{10,12\}$ gives $(2,0)$, pure. Weighted entropy again $0.541$, so $\text{IG}=0.459$.
Largest IG: $\theta=5$ and $\theta=9$ tie at $0.459$. The mirror-image structure of the data causes the tie. Either question is acceptable.
Answer: $\theta = 5$ or $\theta = 9$, both with $\text{IG} = 0.459$
Declaring a single winner when the table shows a tie, or stopping at the first positive IG you find. Always fill the whole table.
Edge cases to remember
  • "$\le$" puts a point equal to $\theta$ on the Yes side.
  • A threshold smaller than every value (or larger than every value) puts all points on one side. That question does not split anything (IG = 0). So a question of the form $f_k \le \theta$ does not always create two non-empty groups.
Memory line

Try thresholds midway between neighbouring values.

9. Tree structure: depth, regions and decision boundaries

Picture it

Cutting a field with fences that run only north-south or east-west.

This section is about the shape of a finished tree: what region each leaf owns, how deep the tree must be, and how many questions it needs.

Splits are axis-aligned, so regions are boxes

Every question looks at one feature: $x_1\le 3$ is a vertical line in the plane, $x_2>2$ is a horizontal line. So each leaf owns a rectangle (in 3 features, a box). The decision boundary of a tree is made of horizontal and vertical segments only, never a slanted line.

YesNoYesNo10x2 > 2 ?0x1 ≤ 3 ?
A tree: root asks $x_1\le3$. On Yes it asks $x_2>2$ (Yes: label 1, No: label 0). On No it predicts 0 immediately.
100x1x2
The same tree as regions in the $(x_1,x_2)$ plane. The vertical dashed line is $x_1=3$. Left of it, the horizontal dashed line $x_2=2$ separates the top rectangle (label 1) from the bottom rectangle (label 0). Right of $x_1=3$ everything is label 0. Three test points are drawn in the colour of their predicted label: $(1,5)\to1$, $(1,1)\to0$, $(4,5)\to0$.
Definition: how to turn a tree into regions

For each leaf, read the questions along its path from the root, writing each as an inequality ($\le$ for Yes, $>$ for No). The leaf's region is the set of points satisfying all of them together.

For the leaf with label 1: path = ($x_1\le3$ Yes, $x_2>2$ Yes) so the region is $\{x_1\le 3,\ x_2>2\}$. For the right leaf: $\{x_1>3\}$.

Depth and the number of questions

TreeInternal nodes (questions)LeavesDepth (edges)
One leaf only010
One question, two leaves121
Tree in the picture above232
Any binary tree$L-1$$L$length of longest path

A tree with $L$ leaves always has exactly $L-1$ questions. This is useful: "How many splits are needed?" is the same as "How many leaves are needed, minus 1?".

How deep must the tree be to carve out a box?

Suppose in 3 features all points of label 1 lie inside a box and all points of label 0 lie outside. The box has 6 faces (a lower and an upper limit in each of the 3 features). To declare "label 1" the tree must have checked all six limits. Each node checks only one limit, so the path to the "inside" leaf needs at least 6 questions, i.e. at least 6 edges. In $d$ features the minimum depth is $2d$.

YesNoYesNoYesNoYesNoYesNoYesNo10x3 < g ?0x3 > e ?0x2 < d ?0x2 > c ?0x1 < b ?0x1 > a ?
The simplest tree for a box in 3 features: a "spine" of six questions (two per feature), each with an immediate leaf 0 on the No side. The leaf 1 at the bottom is reached only by answering Yes six times. Its path has 6 edges, so the depth is 6. A cleverer arrangement cannot shorten the path to the inside leaf, because six different inequalities must all be tested on it.

When the first question has zero gain: the checkerboard (XOR) pattern

Take four equal cells: positive in the top-right and bottom-left, negative in the other two. Any single vertical or horizontal line leaves both sides 50/50, so every first question has IG = 0. A greedy tree still must ask one of them (they tie), and then the second-level questions finish the job.

+ R1− R2+ R3− R4x1x2
Four regions with equal numbers of points: R1 and R3 positive, R2 and R4 negative. Splitting on $x_1=0$ gives left $=R_2\cup R_3$ (one negative cell and one positive cell) and right $=R_1\cup R_4$ (also one of each). Both sides stay 50/50.
YesNoYesNoYesNo+−x2 < 0 ?−+x2 < 0 ?x1 < 0 ?
The tree that fits the pattern exactly: split on $x_1<0$, then on $x_2<0$ on each side. 3 questions, 4 pure leaves, depth 2. Why no fewer? Pure rectangles cannot span two diagonal cells (a rectangle touching R1 and R3 would also cover parts of R2 and R4), so each of the 4 cells needs its own leaf, and 4 leaves means $4-1=3$ questions.

Can a tree always reach zero training error?

If no two training points are identical with different labels, a deep enough tree can separate every point, so training error can be 0. A depth limit stops this and is what protects against overfitting (next section).

Worked example: exam style [ADDED]: read a tree, predict, describe regions

Problem. Use the tree and region picture above. (a) Predict the label of $(3,\,2.5)$. (b) Predict the label of $(3.5,\,2.5)$. (c) Describe the region where the tree predicts 1.

What is asked. Two predictions and one region.

Idea / formula used. Follow the path; "$\le$" includes the boundary value.

(a) Root: $x_1 = 3\le 3$? Yes. Next: $x_2 = 2.5 > 2$? Yes. Leaf 1.
(b) Root: $x_1=3.5\le3$? No. Leaf 0.
(c) Label 1 requires Yes at the root and Yes at the second node: $x_1\le3$ and $x_2>2$. It is the top-left rectangle in the picture. All other points get 0.
Answer: (a) 1; (b) 0; (c) $\{x_1\le3,\ x_2>2\}$
Treating $x_1=3$ as "No" because it sits on the boundary line. With "$\le$" the boundary belongs to Yes.

Depth against error: what a real tree does [ADDED]

0123456780.00.10.20.30.40.5maximum depth allowederror ratetraining error (60 points)test error (400 new points)
A greedy entropy tree grown on 60 random training points (two overlapping groups) and tested on 400 new points from the same source. Allowing more depth lowers the training error to 0.00 by depth 8. The test error is lowest at depth 5 (0.225) and is slightly higher at depth 8 (0.263). The data are random numbers generated in the code (fixed seed), shown to illustrate the shape, not a PYQ.

Reading the picture: depth $0$ is a single leaf predicting the majority (error 0.50 on balanced data). The first few levels capture the real pattern. After that, extra depth carves boxes around single noisy points: training error keeps falling (nothing is left to explain) while test error stops improving.

Questions about depth and structure: lookup table [ADDED]

QuestionAnswer rule
Number of leaves of a full binary tree of depth $d$at most $2^d$
Number of internal nodes (questions) of a full binary tree of depth $d$at most $2^d-1$
Minimum depth to isolate one box in 2-Dup to 4 questions: two thresholds per axis, so one path of length 4 can suffice
Checkerboard $2\times2$ (XOR)two levels: the first question has zero gain, the second splits each half
Can a tree reach zero training error?yes, if no two points have the same features with different labels
Depth of a tree is $3$. Largest possible number of leaves?

$2^3=8$.

Why does a first split on XOR data have zero gain?

Each child has the same label mix as the parent, so the weighted child entropy equals the parent entropy.

Tips and tricks
  • A tree with $L$ leaves that splits in two has $L-1$ question nodes.
  • Questions on one feature at a time give boundaries parallel to the axes.
Memory line

Each split is one cut parallel to an axis.

10. Properties: depth, overfitting, stability

Picture it

Memorising every past answer helps with those questions only.

Some questions ask no calculation, only whether a statement about decision trees is true. Here are the facts, each with its reason.

Depth is the complexity knob

Max depth set toWhat happensName
Too low (e.g. 1)The tree cannot make enough splits to capture the pattern. High error on training and test data.Underfitting
Too high (unlimited)The tree carves a tiny region around every training point, memorising noise. Training error near 0, test error high.Overfitting
ModerateCaptures structure without memorising noise.Good generalisation
Trap: the common swap

"Too low depth leads to overfitting" is FALSE. Low depth is too simple, so it underfits. Say it as "shallow = simple = underfit; deep = flexible = overfit".

Other facts

StatementTrue?Reason
Depth is a hyperparameter that is typically chosen using cross-validation.TrueIt is not learned from the data by the splitting rule; we try several values and keep the one with best validation error (see the Cross-validation topic).
Decision trees are sensitive to small changes in the training data and can give different tree structures.TrueThe first split is a greedy choice. A small change in the data can flip which question wins at the root, and everything below changes.
Decision trees can handle both numerical and categorical features.TrueNumeric: questions $x\le\theta$. Categorical: questions such as "colour = red?".
A question $f_k\le\theta$ always makes two non-empty groups.FalseA $\theta$ beyond the extremes sends everyone to one side.
Training error can always be driven to 0 if depth is unlimited.True when no two identical points have different labelsEach point can get its own leaf.
Four statement-style PYQs sit in the bank's "Model Evaluation" topic and mix decision-tree facts with k-NN facts. They are solved in the Cross-validation topic; the facts you need are all in the table above.

Stability and what limits depth [ADDED]

KnobEffect on the treeEffect on error
smaller maximum depthfewer, bigger boxestraining error up, variance down
larger maximum depthmore, smaller boxestraining error down, risk of overfitting
minimum points per leafstops tiny boxessmooths the boundary
remove one noisy pointa different early split can change the whole treetrees are unstable (high variance)
Which model is easier to read as if-then rules: a tree or k-NN?

A tree.

What does it mean that trees are unstable?

Small changes in the data can change the first split and so the whole tree.

Worked example (tricky): why trees are unstable

Points $x=1,2,3,4$ with labels $0,0,1,1$: the best split is $x\le2.5$, both children are pure, and the gain is $1$. Change the label of $x=3$ to $0$, giving $0,0,0,1$: the best split moves to $x\le3.5$ (gain $0.811$). One changed label moved the threshold, which is what "decision trees are unstable" means.

Tips and tricks
  • A deeper tree can only fit the training data as well or better, not necessarily new data.
  • Statements with the word "always" about test error deserve suspicion.
Memory line

Deeper fits training better, not necessarily the test.

What depth does
shallow tree: may underfitmore depth: training error fallsvery deep: memorises noisepick depth by validation

Practise this topic →

Weeks covering this topic

Other MLT topics