Entropy, information gain, splits, depth, decision boundaries
Open the interactive lessons →Decision Trees past questions
Contents
- Orientation: what you will learn and why
- Big picture: concept map, comparison table and the recipe
- Start here: the maths you need, from zero
- What a decision tree is
- Entropy: measuring how mixed a node is
- Calculating entropy in the exam: method, given logs, ranges
- Information gain: scoring a question
- Numeric features: questions of the form x ≤ θ
- Tree structure: depth, regions and decision boundaries
- 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
- Explain, in plain words, what a node, a split, a leaf and the depth of a tree are.
- Compute the entropy of a node from counts or percentages, to two or three decimal places, with a calculator or with the log values the exam gives you.
- Compute information gain for a split and decide which of two questions should be asked first.
- Handle numeric features: questions of the form $x \le \theta$.
- Read a tree and draw its regions; read regions and rebuild a tree; find minimum depth and the minimum number of splits.
- Judge true/false statements about decision trees (depth, overfitting, stability).
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 ask | PYQs | Marks |
|---|---|---|
| Computing entropy of a node, and facts about entropy | 12 | 29.5 |
| Information gain and choosing the best question | 4 | 12 |
| Tree structure: depth, regions, boundaries, prediction | 7 | 15 |
| True/false properties of decision trees | 1 | 4 |
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).
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]
- A big picture section (next): concept map, comparison with the other models, recipe, mnemonics.
- Figures: information gain of every threshold as a bar chart, and training error against test error as depth grows (a real greedy tree on generated data).
- Tables, recall questions, a pattern map of the PYQs, variations to practise and a flowchart. Everything new is tagged [ADDED].
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
Decision tree next to the other models of the course
| Decision tree | k-NN | Linear model | Naive Bayes | |
|---|---|---|---|---|
| Boundary shape | axis-parallel rectangles (staircase) | jagged to smooth, set by $k$ | one straight line / plane | straight or curved |
| Needs training points to predict? (PYQ definition) | no, the tree is the model | yes | no | no |
| Cost of one prediction | follow one path: at most the depth | $n$ distances | one dot product | a product over features |
| Main knob | depth, minimum leaf size | $k$ | penalty $\lambda$ | smoothing |
| Sensitive to feature scale? | no (only the order matters) | yes | weights adjust | no |
| Easy to read as rules? | yes | no | weights | tables of probabilities |
| Overfits when | too deep (memorises points) | $k$ too small | too many features | rarely |
The exam recipe, in five lines
- 1. Write the counts in each node: (ones, total). Convert to proportions.
- 2. Entropy of a node: $-p\log_2p-(1-p)\log_2(1-p)$. Pure: $0$. Half-half: $1$.
- 3. For a question, entropy of each child, weighted by child size over parent size.
- 4. Information gain = parent entropy minus the weighted sum.
- 5. Biggest gain wins. For several thresholds, only the weighted child entropy needs comparing (the parent is common).
Mnemonics and recognition cues [ADDED]
- "Pure is zero, half-half is one." Entropy of a two-class node runs from $0$ to $1$ bits.
- "Gain = Parent minus Weighted children." (G = P − W.)
- "Weights are sizes." A child with $3$ of $10$ points has weight $0.3$, not $0.5$.
- "Edges, not nodes." A tree with only a root has depth $0$.
- "Staircase, not slope." Tree boundaries are horizontal and vertical segments in 2-D.
- Cue "XOR / checkerboard": the first question has zero gain, but two levels solve it.
Analogy and its limit [ADDED]
| Analogy | What it explains well | Where it breaks |
|---|---|---|
| The game of twenty questions | why 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 bins | leaves are bins; a point lands in exactly one | bins 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).
- 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
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"
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$.
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}}$.
Idea 2. What $\log_2$ means
$\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$.
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.
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.
Idea 3. A weighted average gives more say to bigger groups
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.
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$.
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$.
- $\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.
Entropy is the average surprise, with surprise measured by a logarithm.
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.
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
| Word | Meaning | In the picture below |
|---|---|---|
| Node | A 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 / split | A node that asks a yes/no question about one feature, such as $x \le 3$. | Boxes with a question |
| Branch | One answer (Yes or No) leading to a child node. | Lines labelled Yes / No |
| Leaf | A node that asks nothing and states a label. | Round boxes 0 and 1 |
| Depth of the tree | Number of edges on the longest path from root to a leaf (this is the definition the PYQs use). | Here it is 2 |
How a tree predicts
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)
- Put all training points in the root.
- If the node is pure (all one label), stop: make it a leaf.
- Otherwise, list the possible questions (for a numeric feature: $x \le \theta$ for several thresholds $\theta$).
- Score each question by its information gain, and keep the one with the highest score.
- 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.
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.
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$).
- Trace one point from the root, writing the answer at each question node.
- Leaves give predictions; internal nodes only ask questions.
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.
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
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.
$$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.00 | 0.000 |
| 0.10 | 0.469 |
| 0.20 | 0.722 |
| 0.25 | 0.811 |
| 0.30 | 0.881 |
| 0.40 | 0.971 |
| 0.50 | 1.000 |
| 0.60 | 0.971 |
| 0.70 | 0.881 |
| 0.75 | 0.811 |
| 0.80 | 0.722 |
| 0.90 | 0.469 |
| 1.00 | 0.000 |
Four facts that PYQs test
- Pure node: $E = 0$. All points have one label.
- Maximum at $p=0.5$: $E=1$. Equal numbers of positives and negatives. (Two classes, log base 2.)
- 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.
- 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$.
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
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$.
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$.
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)$.
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)$.
Common mistakes with entropy
- Dropping a minus sign, or computing only one of the two terms.
- Using $\ln$ instead of $\log_2$.
- Using counts instead of proportions inside the formula.
- Writing $E=0.5$ for a 50/50 node (confusing $p$ with $E(p)$; the entropy there is 1).
- Believing entropy keeps growing as $p$ grows. It rises until $p=0.5$ and then falls.
- Trying to evaluate $\log_2 0$ for a pure node instead of using $E=0$.
Entropy ladder [ADDED]
| Proportion of label 1 | Entropy (bits) | Reading |
|---|---|---|
| 0.5 | 1.0000 | most mixed |
| 0.25 | 0.8113 | mostly one class |
| 0.1 | 0.4690 | nearly pure |
| 0 | 0.0000 | pure |
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$).
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.
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
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
- Find the two counts of the node in question (which node: root? left child? right child?).
- Convert to $p$ = (count of the class you call positive) / (total). Either class works by symmetry.
- Evaluate $-p\log_2 p-(1-p)\log_2(1-p)$ with a calculator, or with the log values printed in the question.
- 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.
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.
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 : 50 | 0.5 | 1.000 |
| 60 : 40 (or 40 : 60) | 0.6 | 0.971 |
| 70 : 30 | 0.7 | 0.881 |
| 75 : 25 | 0.75 | 0.811 |
| 80 : 20 | 0.8 | 0.722 |
| 90 : 10 | 0.9 | 0.469 |
| 100 : 0 | 1 | 0.000 |
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.
- 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$.
Symmetric, highest in the middle, zero at the ends.
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.
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
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)").
$$\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
- Count the parent: (positives, negatives).
- Compute the parent entropy $E(D)$.
- Apply the question; write the counts of each child: (positives, negatives) for Yes and for No.
- Compute the entropy of each child (pure child $\Rightarrow$ 0).
- Compute the weights $\gamma = n_{\text{Yes}}/n$ and $1-\gamma$.
- Weighted sum of child entropies; subtract from $E(D)$.
What information gain can and cannot be
- $\text{IG}\ge 0$ always. A question can never make the average entropy larger. If you get a negative number, you forgot the weights or made an arithmetic slip.
- $\text{IG}=0$ when both children have the same proportion as the parent (the question told you nothing).
- $\text{IG}=E(D)$ (its maximum) when both children are pure.
- Because $E(D)$ is the same for every candidate question at a node, the best question is the one with the smallest weighted child entropy, equivalently the largest IG.
Worked examples
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}})$.
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.
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.
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.
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.
| Question | Ones among Yes side | Ones among No side | Information gain |
|---|---|---|---|
| $x\le1.5$ | 0 of 1 | 6 of 9 | 0.1445 |
| $x\le2.5$ | 0 of 2 | 6 of 8 | 0.3219 |
| $x\le3.5$ | 0 of 3 | 6 of 7 | 0.5568 |
| $x\le4.5$ | 1 of 4 | 5 of 6 | 0.2564 |
| $x\le5.5$ | 1 of 5 | 5 of 5 | 0.6100 |
| $x\le6.5$ | 2 of 6 | 4 of 4 | 0.4200 |
| $x\le7.5$ | 3 of 7 | 3 of 3 | 0.2813 |
| $x\le8.5$ | 4 of 8 | 2 of 2 | 0.1710 |
| $x\le9.5$ | 5 of 9 | 1 of 1 | 0.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 weights | Common 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$.
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.
- 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.
Gain is parent entropy minus the weighted child entropy.
8. Numeric features: questions of the form x ≤ θ
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.
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:
- 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".
- 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
- Sort the points by the feature.
- List the thresholds (midpoints of the gaps).
- For each, write the Yes counts and No counts, then the IG (six steps from the last section).
- Pick the largest IG. A tie means either question is equally good as far as the score goes.
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$.
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$.
| $\theta$ | Yes counts | No counts | Weighted child entropy | IG |
|---|---|---|---|---|
| 3 | (0, 1) | (3, 2) | 0.809 | 0.191 |
| 5 | (0, 2) | (3, 1) | 0.541 | 0.459 |
| 7 | (1, 2) | (2, 1) | 0.918 | 0.082 |
| 9 | (1, 3) | (2, 0) | 0.541 | 0.459 |
| 11 | (2, 3) | (1, 0) | 0.809 | 0.191 |
- "$\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.
Try thresholds midway between neighbouring values.
9. Tree structure: depth, regions and decision boundaries
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.
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
| Tree | Internal nodes (questions) | Leaves | Depth (edges) |
|---|---|---|---|
| One leaf only | 0 | 1 | 0 |
| One question, two leaves | 1 | 2 | 1 |
| Tree in the picture above | 2 | 3 | 2 |
| 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$.
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.
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).
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.
Depth against error: what a real tree does [ADDED]
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]
| Question | Answer 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-D | up 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.
- 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.
Each split is one cut parallel to an axis.
10. Properties: depth, overfitting, stability
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 to | What happens | Name |
|---|---|---|
| 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 |
| Moderate | Captures structure without memorising noise. | Good generalisation |
"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
| Statement | True? | Reason |
|---|---|---|
| Depth is a hyperparameter that is typically chosen using cross-validation. | True | It 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. | True | The 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. | True | Numeric: questions $x\le\theta$. Categorical: questions such as "colour = red?". |
| A question $f_k\le\theta$ always makes two non-empty groups. | False | A $\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 labels | Each point can get its own leaf. |
Stability and what limits depth [ADDED]
| Knob | Effect on the tree | Effect on error |
|---|---|---|
| smaller maximum depth | fewer, bigger boxes | training error up, variance down |
| larger maximum depth | more, smaller boxes | training error down, risk of overfitting |
| minimum points per leaf | stops tiny boxes | smooths the boundary |
| remove one noisy point | a different early split can change the whole tree | trees 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.
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.
- 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.
Deeper fits training better, not necessarily the test.