🌏 中文版
Video status: Videos included. This article is written from the official MOOC handout 209_handout.pdf and transcripts of the four official videos, with formulas following the handout. The transcripts are machine transcripts made for this article (Whisper), not official captions, and no human has proofread them. The videos are taught in Mandarin; the slides are in English. Each section opens with its official video and the approximate time ranges. Page numbers refer to the number at the lower right of each slide (0/22 to 22/22), and wherever the text says "in the lecture" it is paraphrasing what is said in the video.
This is Techniques Lecture 9 in Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques. The Roadmap on page 1 of the handout divides Techniques into three ways of handling features, and this lecture belongs to the second, Combining Predictive Features: Aggregation Models. The summary of the previous lecture, Techniques Lecture 8: AdaBoost, on that same page is: re-weight each data point to get diverse hypotheses, then combine them linearly so that weak algorithms become strong. This lecture fills in the last kind of aggregation not yet covered: using different hypotheses under different conditions, where both the conditions and the hypotheses have to be learned from data.
The prerequisites are the classification of aggregation in Techniques Lecture 7: Blending and Bagging, the decision stump and AdaBoost-Stump from the previous lecture, and two tools from Foundations: Foundations Lecture 14: Regularization ( plus times a complexity term) and validation from Foundations Lecture 15: Validation. After reading, you should be able to write a tree in both the path form and the recursive form, compute every branching step of C&RT by hand on a small data set, prune the grown tree back one leaf at a time and choose , and say which step of the algorithm categorical features and missing values each change.
This lecture has few formulas. Its content is concentrated in one recursive algorithm and its four design choices:
- How many branches at a time: C&RT chooses 2.
- What a leaf returns: the constant that minimizes .
- How to branch: with a decision stump, picking the one that leaves the purest parts.
- When to stop: when nothing more can be split, with pruning afterwards.
What kind of hypothesis a decision tree is: aggregation that divides the work by condition
(Video 00:00–01:11 reviews the previous lecture, 01:11–04:01 is the aggregation table, 04:01–09:18 presents the path view with the MOOC-watching example, 09:18–12:22 the recursive view, 12:22–16:45 the pros and cons of decision trees, and Fun Time follows.)
The last cell of the table
Page 2 of the handout collects Techniques Lectures 7 and 8 into one table. Aggregation has two aspects: blending means you already have some and only learn how to combine them; learning means you learn the and combine them at the same time. There are three ways to combine:
| aggregation type | blending (get first, then aggregate) | learning (aggregate while getting ) |
|---|---|---|
| uniform (one vote per ) | voting/averaging | Bagging |
| non-uniform (different numbers of votes) | linear | AdaBoost |
| conditional (a different depending on the situation) | stacking | Decision Tree |
The lower-right cell is this lecture. In the lecture, Lin describes that cell this way: different are to be used in different situations, and both those and the conditions for "which situation" have to be learned. The handout positions the decision tree as a traditional learning model that realizes conditional aggregation. The lecture adds some history: decision trees appeared very early, possibly even earlier than the term "machine learning", because they imitate how people make decisions, and they have been in continuous use in medicine and business.
The tree for watching MOOC lectures: the path view
The example on page 3 of the handout is whether to watch online lectures after getting home from work. Written out in words, the tree on the slide is:
- The root asks about the quitting time.
- Earlier than 18:30: next ask whether there is a date. With a date, do not watch (N); without one, watch (Y).
- Between 18:30 and 21:30: watch (Y).
- Later than 21:30: next ask about the homework deadline. More than 2 days before the deadline, do not watch (N); more than 2 days past it, do not watch (N); in between, watch (Y).
The lecture goes through the reason for every branch: leaving work early with no date, a lecture goes well with dinner; after working very late, you sleep first if the deadline is still far away, and once it is more than two days overdue a large part of the score has already been deducted and watching will not help you catch up, so you only force yourself to watch when the deadline is close or has just passed.
This tree has 6 leaves (2 under the date node, 1 attached directly to the middle branch of the root, 3 under the deadline node), and therefore 6 paths from the root to a leaf. The handout writes it in aggregation form:
- is the base hypothesis, the leaf at the end of path . Here every leaf is a constant (Y or N).
- is the condition: 1 when it holds and 0 when it does not.
Listing the 6 paths (the numbering is added by this article, following the slide from left to right):
| condition | leaf | |
|---|---|---|
| 1 | quitting time earlier than 18:30, and has a date | N |
| 2 | quitting time earlier than 18:30, and has no date | Y |
| 3 | quitting time between 18:30 and 21:30 | Y |
| 4 | quitting time later than 21:30, and more than 2 days before the deadline | N |
| 5 | quitting time later than 21:30, and within 2 days either side of the deadline | Y |
| 6 | quitting time later than 21:30, and more than 2 days past the deadline | N |
The bottom row of the slide shows only 5 leaves. The Y of path 3 is drawn one level higher, attached directly under the root, and it is easy to miss when counting. Writing every path as a row prevents that.
Every reaches exactly one leaf, so exactly one of the six is 1, and the sum is the value of that leaf. For example, if you leave work at 22:00 today and the homework is due tomorrow: , the others are 0, and Y.
Compare this with the previous two lectures. In Bagging and AdaBoost every votes on every , and the weights are fixed numbers. A decision tree's "weight" changes with , and only one speaks each time.
The handout also notes on this page that a tree is usually with simple internal nodes. In the lecture, Lin explains that simplicity is prized throughout the world of decision trees: the simpler the test at an internal node the better, the simpler the decision at a leaf the better, and the complicated part is all buried in the links and paths between nodes. The lecture also mentions that a leaf does not have to be a constant, and that in practice people put simple models such as linear regression at the leaves; this lecture uses constants only.
The recursive view: a tree is a root plus some sub-trees
Page 4 writes it another way. Take the tree apart at the root: the root has a branching criterion whose value is an integer between 1 and , telling you which branch to take; under each branch hangs a smaller tree .
- : the full-tree hypothesis.
- : the branching criteria.
- : the sub-tree hypothesis at the -th branch.
In the MOOC-watching example, , and returns 1, 2, or 3 according to the quitting time; is the small two-leaf "has a date?" tree, is a single leaf Y, and is the small three-leaf "deadline?" tree. and can themselves be taken apart once more with the same formula.
The handout's note is tree = (root, sub-trees), just like what your data structure instructor would say. In the lecture, Lin calls this a recursive definition: a tree is defined in terms of several smaller trees. The two views describe the same function. The path view makes it easy to see that this is conditional aggregation, and the recursive view makes it easy to write an algorithm; the C&RT algorithm follows the recursive formula.
Pros and cons, stated up front
Page 5 is titled Disclaimers about Decision Tree and lays out the practical reality of decision trees.
Usefulness:
- human-explainable, widely used in business and medical data analysis. The lecture uses a doctor's visit as the example: if the doctor says your medical record was mapped into an infinite-dimensional space, a fat boundary was found, and the result was mapped back, that is SVM, and the patient cannot follow it; if the doctor says this indicator of yours is above so much and that one is below so much, so the judgment is no cancer, the patient can follow.
- simple; the handout writes even freshmen can implement one. The lecture says this is actual experience: Lin teaches the freshman data structures course at NTU, and the homework for the unit on trees had students write a decision tree.
- efficient in prediction and training. The reason given in the lecture is that the computation is simple at internal nodes and leaves alike, and a few steps lead to the answer.
However:
- heuristic: mostly little theoretical explanations. The lecture is blunter: much of the reasoning in the algorithms is just "it seems natural", and there are many different discussions of why they work well.
- heuristics (plural): one decision tree algorithm may contain five, six, or seven clever ideas from earlier people, and it is not very clear whether each should be used or what happens if it is changed, so beginners get a headache just choosing these settings.
- arguably no single representative algorithm. In the lecture, Lin explains that, unlike algorithms such as SVM and AdaBoost, decision trees usually have no single algorithm that is truly representative; some are more popular and some less, but the reason for popularity is sometimes only historical and does not mean the design is better.
The handout's conclusion is mostly heuristic but useful on its own. The lecture therefore says up front: there are more decision tree models than can be taught, and this course picks one version that is relatively simple and connects to the rest of the course; in practice you are quite likely to try other decision tree algorithms.
Fun Time: reading code as a tree
Question (page 6): The following C-like code can be viewed as a decision tree of three leaves.
if (income > 100000) return true;
else {
if (debt > 50000) return false;
else return true;
}
What is the output of the tree for (income, debt) = (98765, 56789)?
- true
- false
- 98765
- 56789
Answer: 2. Trace the code: 98765 is not greater than 100000, so go into the else; 56789 is greater than 50000, so return false. The lecture reads false as not issuing you the card: the income does not pass the threshold, and the debt exceeds its threshold. Choices 3 and 4 are numbers from the input, and the leaves of this tree only return true or false. Choice 1 appears only if you reach one of the other two leaves.
The handout's answer adds one sentence: the tree expresses a complicated boolean condition .
An addition in this article: write it once in each view. The path view has three paths:
| condition | leaf | |
|---|---|---|
| 1 | income > 100000 | true |
| 2 | income ≤ 100000 and debt > 50000 | false |
| 3 | income ≤ 100000 and debt ≤ 50000 | true |
The input in the question gives , and the output is = false. The paths that output true are paths 1 and 3, and together they are the "or" condition the handout writes. In the recursive view, : the root's looks at income, the sub-tree on one side is the single leaf true, and the sub-tree on the other side is the two-leaf tree that looks at debt. This tree comes back in the numerical example later.
How to grow a tree from data: the four choices in C&RT
(Video 00:00–02:50 is the basic recursive algorithm, 02:50–05:21 the two simple choices in C&RT, 05:21–09:08 branching by purifying, 09:08–12:57 impurity functions, 12:57–14:46 the termination criteria, and Fun Time follows.)
The skeleton of the recursive algorithm
The formula of the recursive view turns directly into an algorithm (page 7). The input is data , and the output is a tree:
DecisionTree():
- If the termination criteria are met: return the base hypothesis .
- Else:
- Learn the branching criteria .
- Split to parts: .
- Build a sub-tree recursively for each part: DecisionTree().
- Return .
In the lecture, Lin calls this a typical divide and conquer: separate the data, learn a small tree on each part, and join them back together. Lin also warns that if you write only steps 1 to 4, your data structures instructor will say you deserve a spanking, because the recursion has no stopping condition and the computer will go on forever.
This skeleton leaves four blanks, which the handout calls four choices: the number of branches , how the branching criteria are learned, when to stop (the termination criteria), and what a leaf returns (the base hypothesis). Each decision tree algorithm is one way of filling these four blanks. This lecture presents the Classification and Regression Tree, written C&RT in the handout; what is said aloud in the lecture is CART, and it refers to the same algorithm.
Two simple choices: a binary tree, and the optimal constant at the leaves
Page 8 fills in the two easier blanks first.
. Every split makes only two parts, and what grows is a binary tree.
is the -optimal constant, where counts only the data that reach this leaf:
- binary/multiclass classification (0/1 error): the majority of .
- regression (squared error): the average of .
The handout gives only the conclusions, and the lecture only mentions that the one about the average is not hard to prove, without writing out the steps. Both are derived below (the derivations are added by this article).
Classification. The leaf has data points, of which belong to class . If the leaf returns the constant , the ones it gets wrong are the points of the other classes, and . To make it smallest, choose the class with the largest , written . The error rate is then
Regression. When the leaf returns the constant , . It is a quadratic function of that opens upward; differentiate with respect to and set the result to 0:
For example, if the values at the leaf are 1, 2, 2: the average is , and the total squared error is . Returning 2 instead gives a total of , and returning 1 gives , both larger.
The same page carries a disclaimer: the C&RT here is based on selected components of CART™ of California Statistical Software. The lecture's explanation is that CART is that company's registered trademark, the algorithm they actually use is much more complicated, and the lecture presents only the more essential parts.
Branching: purifying the data with a decision stump
Page 9 fills in the third blank. Since there are only two parts, the internal node uses the decision stump from the previous lecture: look at a single feature and make one cut at some threshold. Unlike the previous lecture, the stump here outputs 1 or 2 (which branch to take), and the handout calls it a -output decision stump.
There are many candidate stumps; which one should be picked? The lecture's reasoning runs like this: when the tree is cut all the way down, a leaf returns a constant. When is a constant a good prediction? When the data left there are very "pure", meaning the values are all the same (classification) or all close together (regression). So every branching step should make the two resulting parts as pure as possible, which the handout calls purifying:
is the -th part of the data produced by stump , is the number of points, and impurity is smaller when the part is purer. Each part's impurity is multiplied by its own size and the products are added. The lecture explains the weighting: for the larger part, whether it is pure matters more; for the smaller part it matters less. The handout sums this page up as bi-branching by purifying.
How impurity is measured
Page 10 gives four formulas in two columns.
Left column: by of the optimal constant. The idea in the lecture is direct: the tree returns a constant in the end, and how well that constant does is itself a statement of how pure the data are.
Regression error:
Classification error:
Right column: for classification specifically. First rewrite the classification error; it is what was derived above,
The handout notes beside it optimal only. The lecture's explanation is that this formula looks only at the class with the largest proportion; with ten classes, how the other nine are distributed has no effect on the result. Replace the max with a sum over all classes, and square the proportions, and you have the Gini index:
The handout's note is all considered together. The reading given in the lecture: square each class's proportion and add them up; the larger this sum, the purer the data, and subtracting it from 1 makes it an impurity.
The handout says the popular choices are Gini for classification and regression error for regression. The lecture stresses that these are all heuristics you are free to choose.
This article adds three things the handout does not write but which a little computation shows. Write the proportion of class as .
- The range of Gini. With only one class, and Gini is . With classes each taking , Gini is , which is its maximum; with two classes the maximum is .
- Another way to write Gini. Because , we have . Each class contributes "the proportion that is this class" times "the proportion that is not this class".
- What the weighted sum means. Both impurities in the left column are an , that is, "total error divided by the number of points". Multiplying back by the size gives the total error: for classification, how many points of this part a majority vote gets wrong; for regression, the total squared error. So with these two impurities, the branching criterion is the same as minimizing the overall number of mistakes (or the total squared error) after "one cut, with one optimal constant on each side". Gini has no such direct correspondence; it is a separately designed quantity. The numerical example below shows that the two can pick different splits.
Termination: the two cases where the algorithm is forced to stop
Page 11 fills in the last blank. C&RT sets no extra stopping rule and stops only when it cannot go on. The handout calls this 'forced' to terminate:
- All the same. The impurity is already 0, and further cuts cannot make it purer. Return directly; this leaf makes no mistakes.
- All the same. No stump can separate them; the lecture says there is simply nowhere to put the knife.
A tree grown until it is forced to stop is called a fully-grown tree. All four blanks are now filled, and the handout writes C&RT in one sentence: fully-grown tree with constant leaves that come from bi-branching by purifying.
An addition in this article: this recursion always stops. Every branching step splits the data into two parts, neither of them empty (the threshold is placed between two different feature values), so each part has fewer points than before, and at the slowest it shrinks to a single point, whose is of course "all the same". There are at most leaves and at most internal nodes.
Numerical example: 14 data points, growing the tree to the end
What follows is an example made up for this article, not from the slides. The setting continues the card-issuing decision from Fun Time, with the unit changed to ten thousands: is annual income, is debt, means the card is approved, and means it is not.
| (income) | (debt) | (income) | (debt) | |||||
|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 8 | 4 | 7 | |||
| 2 | 4 | 1 | 9 | 2 | 9 | |||
| 3 | 6 | 3 | 10 | 6 | 9 | |||
| 4 | 8 | 1 | 11 | 8 | 7 | |||
| 5 | 12 | 1 | 12 | 12 | 7 | |||
| 6 | 12 | 3 | 13 | 14 | 7 | |||
| 7 | 14 | 1 | 14 | 14 | 9 |
There are 9 points with and 5 with . The impurity is Gini. With two classes, a part with points of and points of has Gini , and after multiplying by the size it is . All the tables below are computed with this formula.
Candidate splits at the root. The income values that occur are 2, 4, 6, 8, 12, 14; put one threshold (the midpoint) between each pair of adjacent values, 5 in all. The debt values that occur are 1, 3, 7, 9, giving 3 thresholds. Wherever a threshold sits between two adjacent values, the two resulting parts are the same, so only these 8 splits need to be considered:
| split | the "≤" part ( / ) | the ">" part ( / ) | weighted impurity |
|---|---|---|---|
| 1 / 1 | 8 / 4 | ||
| 2 / 2 | 7 / 3 | ||
| 3 / 3 | 6 / 2 | ||
| 4 / 4 | 5 / 1 | ||
| 7 / 4 | 2 / 1 | ||
| 3 / 1 | 6 / 4 | ||
| 6 / 1 | 3 / 4 | ||
| 8 / 3 | 1 / 2 |
Take the row as the example: the points with debt 1 or 3 are points 1 to 7, 6 with and 1 with , giving ; the points with debt 7 or 9 are points 8 to 14, 3 with and 4 with , giving . The sum is , the smallest of the 8 splits. Without a split, the value for the whole data set is . The root chooses .
The part with debt ≤ 5 (points 1 to 7). It has 6 points with and 1 with ; the values are not all the same, so keep splitting. The only is point 7, , whose income is the highest in this part. Split at : one side is the 6 points with and the other is point 7 alone, the impurity is 0 on both sides, and the weighted sum is 0. Every other split leaves at least one on the same side as point 7, so its weighted sum is greater than 0 (for example, gives , and gives ). Choose . After the split the values on each side are all the same, so stop.
The part with debt > 5 (points 8 to 14). The 4 points with have incomes 2, 4, 6, 8, and the 3 points with have incomes 12, 14, 14. Split at : one side has the 4 points with and the other the 3 points with , and the weighted sum is 0. Choose it, and both sides stop.
The fully-grown tree has 3 internal nodes and 4 leaves:
- Debt ≤ 5?
- Yes: income ≤ 13?
- Yes → approve (6 points, all )
- No → do not approve (1 point, point 7)
- No: income ≤ 10?
- Yes → do not approve (4 points, all )
- No → approve (3 points, all )
- Yes: income ≤ 13?
What this result means. All 14 points are classified correctly, so . Converted to units of ten thousand, the Fun Time input is : the debt is greater than 5 and the income does not exceed 10, so it reaches "do not approve", the same as the Fun Time answer false. But this tree has one leaf held up by a single data point: "someone whose debt is not high but whose income is above 130,000 is not approved". That rule goes against common sense. It looks more like something is wrong with point 7 itself (noise), yet the tree made one more cut just to classify it correctly. This is what pruning has to deal with.
What happens with classification error instead. Take the same 8 splits and use classification error as the impurity; the weighted sum is then "how many points are wrong in total when each side uses a majority vote". gets points wrong, also gets wrong, and the other 6 all get 5 wrong, the same as not splitting at all (5 of the 14 points are , and guessing for everything gets 5 wrong). Classification error cannot tell from , nor can it see that cuts out a part that is 5 to 1. Gini counts the proportion of every class, and the scores of these three splits are 5.143, 5.697, and 5.667, which can be ranked. This is what the handout's note "all considered together" looks like in actual numbers.
Fun Time: the Gini index for two classes
Question (page 12): For the Gini index, . Consider , and let , where is the number of examples with . Which of the following formula of equals the Gini index in this case?
Answer: 1. The proportions of the two classes are and . Substitute:
The handout's answer only says to simplify and the answer should pop up. The other three choices can be ruled out by plugging in one number: at Gini is , and the four choices are , , , and , so only choice 1 is right. Choices 2 and 3 have one more visible problem: swapping the names of the two classes (replacing with ) should not change the impurity, but these two formulas do change.
The used in the numerical example is the result of this question, with .
A fully-grown tree needs pruning, and categorical features and missing values fit in too
(Video 00:00–01:26 reviews C&RT and discusses multiclass, 01:26–07:10 is pruning, 07:10–09:51 categorical features, 09:51–12:44 missing values and the surrogate branch, and Fun Time follows.)
One algorithm, three kinds of problems
Page 13 puts the four choices back into the skeleton to get the complete C&RT: if it cannot branch anymore, return the -optimal constant; otherwise learn a stump by purifying, split the data to 2 parts, recurse on each side, and join the results.
On this page the handout stresses that it can easily handle binary classification, regression, & multi-class classification. The lecture gives the reason: going from binary classification to multiclass, only two places in the algorithm are affected, the constant at a leaf (it becomes the majority among several classes) and the computation of impurity (Gini was written for classes to begin with). In the lecture, Lin says multiclass is almost as easy for it as binary classification, which makes the decision tree a rather special algorithm. An addition in this article: comparing pages 8 and 10, switching to regression changes the same two places, the constant becomes the average and the impurity becomes the regression error; the three steps of branching, splitting the data, and recursing do not change at all.
Pruning: regularizing on the number of leaves
Page 14 starts by pointing out the problem with a fully-grown tree:
- As long as all are different, .
- But it overfits (large ), because the low-level sub-trees are built with very little data .
This article supplies the reason for the first point: as long as a node still holds two data points with different , their are different, so at least one feature has different values for them, and a threshold halfway between those two values on that feature is a usable stump, so the algorithm does not stop. Therefore every leaf where it does stop holds identical , and every data point is classified correctly.
The lecture raises this question from a student's point of view: cutting this way, the data can always be completely separated, and is always 0; recalling the theory in Foundations Lecture 7: The VC Dimension, being able to reach every time is not good news, because it means the VC dimension is infinite and the model complexity is not under control. Also, the further down the cuts go, the less data each node has, and a decision made on so little data is quite likely overfitting. The single-point leaf in the numerical example is exactly this situation.
The remedy is the approach taught in Foundations Lecture 14: Regularization, which needs a quantity that measures how complex a tree is. The lecture's reasoning: a tree is too complex because too many cuts were made, and too many cuts mean too many leaves, so use the number of leaves:
What we want is a regularized decision tree:
The handout says the resulting tree is called a pruned decision tree. The lecture explains that to prune is to cut back or trim: do not let the tree grow to its full height, trim it shorter with fewer leaves, in the hope that such a tree overfits less.
The trouble is "all possible ". One run of C&RT produces only one tree, and listing every possible split and every possible tree structure is computationally out of reach. The handout's approach is to consider only one sequence of trees:
- = the fully-grown tree.
- , such that is one-leaf removed from .
The lecture is more concrete: suppose the fully-grown tree has 10 leaves; try taking off each leaf in turn and see which 9-leaf tree has the smallest , and that is ; then start from and take off a second leaf to get , and continue all the way down. "Taking off a leaf" means merging it with the neighbouring leaf, and after the merge the new leaf has its constant recomputed. This way a 10-leaf tree produces only about 10 candidate trees, and is then used to choose among them. In the lecture, Lin explains that these are probably good enough trees, and choosing among them is sufficient.
What remains is how to set . The handout has only one line: systematic choice of ? validation. That is the approach of Foundations Lecture 15: Validation: treat as a parameter and choose it with a validation set.
Numerical example, continued: pruning that tree back
This part is also made up for this article. Start from the 4-leaf tree grown from the 14 data points above; it is .
The handout does not specify how to remove a leaf when "the neighbour is not a leaf". The reading this article adopts is: find an internal node whose two children are both leaves and merge those two leaves into one, which reduces the number of leaves by 1.
. Two internal nodes qualify:
- Merge the two leaves under "income ≤ 13?": together they have 6 points with and 1 with , the new leaf returns , and 1 more point is wrong. .
- Merge the two leaves under "income ≤ 10?": together they have 3 points with and 4 with , the new leaf returns , and 3 more points are wrong. .
The smaller is the first, so is: approve if debt ≤ 5; otherwise look at income, do not approve if ≤ 10 and approve if > 10.
. Only "income ≤ 10?" is left to merge, and after merging . has only the root: approve if debt ≤ 5, otherwise do not approve.
. Merge the two leaves of the root: 9 points with and 5 with , return , . is a single leaf: always approve.
Next compute for different , with the smallest in each column in bold:
| tree | number of leaves | ||||
|---|---|---|---|---|---|
| 4 | 0.120 | 0.400 | 0.800 | ||
| 3 | 0.161 | 0.371 | 0.671 | ||
| 2 | 0.346 | 0.486 | 0.686 | ||
| 1 | 0.387 | 0.457 | 0.557 |
A very small keeps the fully-grown tree, a very large prunes down to a single leaf, and a in between selects . The boundaries can be solved directly: beats when , that is, ; beats when , that is, . is never the smallest under any : it has one leaf fewer than but its is higher, so it pays off only when ; yet when is that large, , with one leaf fewer still and an only higher, pays off more.
Choosing with validation. Suppose another 10 data points were held out and took no part in training (also made up):
The mistakes of the four trees on these 10 points:
| tree | points it gets wrong | |
|---|---|---|
| : debt ≤ 5 and income > 13, judged "do not approve" by the single-point leaf | ||
| none | ||
| , : debt > 5 is never approved | ||
| , , : always approve |
The smallest belongs to , which corresponds to every value in , for example .
What this result means. , with , does worse on new data than , with , and the difference is the leaf that grew for the sake of point 7. The first pruning step removes exactly that leaf: taking it off costs only 1 more mistake, while taking off the other side costs 3 more, and the handout's argmin gives up the cheapest leaf first. The condition under which approves is "debt ≤ 5, or income > 10", the same boolean condition the Fun Time tree expresses (with the unit changed to ten thousands), except that it asks about debt first and income second. Two trees with different structures can be the same function.
The data in this example were designed so that every step can be computed by hand, and a validation set of only 10 points is far from enough in a real problem; it demonstrates the procedure and cannot serve as evidence of how well C&RT performs.
Categorical features: replacing the threshold with a subset
Page 15 handles another kind of input. So far the features have all been numbers (numerical features), for example blood pressure 130, 98, 115, 147, 120, and cutting at some threshold with a decision stump is natural:
When the bracket holds, ; when it does not, .
But a feature can also be a category (categorical features), for example a major symptom that is one of fever, pain, tired, sweaty. The lecture points out that such features are troublesome for linear models: a linear model ends up computing a weighted score, so its inputs had better be numbers to begin with, and categories have no order of magnitude. For a decision tree only the "how to cut" step is affected, and it is enough to replace the threshold with a subset, which the handout calls a decision subset:
The lecture's example is the weather: sunny or cloudy goes one way, rainy the other. The method is to enumerate all subsets and see which split is best; the rest of the algorithm does not change. The handout's conclusion is that C&RT (& general decision trees) handles categorical features easily.
An addition in this article: how many subsets have to be tried? categories have subsets; removing the empty set and the full set (they do not separate the data) leaves ; and its complement produce the same two parts with the numbering swapped, so there are distinct splits. That is 7 for and 511 for , so the cost of enumeration rises quickly as the number of categories grows.
A small example (made up for this article). 12 patients, with the major symptom as the only feature, and meaning a diagnosis of some disease:
| symptom | number with | number with |
|---|---|---|
| fever | 3 | 0 |
| pain | 1 | 2 |
| tired | 0 | 3 |
| sweaty | 2 | 1 |
The weighted Gini of the 7 splits (again with ):
| the part ( / ) | the other part ( / ) | weighted impurity | |
|---|---|---|---|
| {fever, sweaty} | 5 / 1 | 1 / 5 | |
| {fever} | 3 / 0 | 3 / 6 | |
| {tired} | 0 / 3 | 6 / 3 | |
| {fever, pain} | 4 / 2 | 2 / 4 | |
| {pain} | 1 / 2 | 5 / 4 | |
| {sweaty} | 2 / 1 | 4 / 5 | |
| {fever, tired} | 3 / 3 | 3 / 3 |
The best is . Cutting out fever alone gives one completely pure part, but the remaining 9 people are 3 to 6, and overall that is worse than moving sweaty over as well. The worst, {fever, tired}, puts the symptom leaning most toward and the one leaning most toward on the same side, and the result is the same as not splitting (the value for 12 people at 6 to 6 is also 6).
Missing values: storing substitute splits during training
The scenario on page 16: the best branch at some node is , but at prediction time this patient has no weight on record.
The handout first asks what a human would do: go get the weight; or use a threshold on height instead, because threshold on height ≈ threshold on weight. The lecture turns it into an emergency-room scene: a patient is brought in and nobody weighed them, but a decision still has to be made about treatment and dosage. If there is time, weigh them; if not, estimate the height by eye. People weighing 50 kg or less are roughly 155 cm or shorter, so the cut that was meant to be on weight is made on height instead. The number 155 is an example given aloud in the lecture and is not in the handout.
C&RT builds this idea into the algorithm and calls it the surrogate branch:
- During training, besides the best branch , maintain several surrogate branches , which use other features and whose results are close to those of .
- During prediction, when the feature needs is missing, use a surrogate branch instead.
In the lecture, Lin explains that a surrogate is a stand-in, put simply a substitute; how the substitute is found comes down, broadly, to how similar the parts it produces are to the parts produced by the original split. The handout's conclusion is C&RT: handles missing features easily. The lecture adds in passing that this has to do with the character of decision trees: the algorithm is heuristic to begin with, so adding one more heuristic for a practical problem like incomplete data is easy.
A small example (made up for this article). The handout gives no formula for "how similar", so the most direct measure is used here: how many training points the surrogate branch and the best branch send to the same side. The weight (kg) and height (cm) of 8 patients:
| patient | weight | height | weight ≤ 50? |
|---|---|---|---|
| A | 42 | 148 | yes |
| B | 45 | 151 | yes |
| C | 47 | 153 | yes |
| D | 50 | 163 | yes |
| E | 53 | 157 | no |
| F | 60 | 162 | no |
| G | 66 | 165 | no |
| H | 72 | 170 | no |
The candidate surrogate branches are "height ≤ θ", with θ at the midpoints of adjacent heights:
| θ | number sent to the same side as "weight ≤ 50" | patients sent to the wrong side |
|---|---|---|
| 149.5 | 5 | B, C, D |
| 152 | 6 | C, D |
| 155 | 7 | D |
| 159.5 | 6 | D, E |
| 162.5 | 5 | D, E, F |
| 164 | 6 | E, F |
| 167.5 | 5 | E, F, G |
The most similar is "height ≤ 155", which agrees on 7 of the 8 patients; only D, who is taller and thinner, is sent to the other side. During training it is stored as this node's . At prediction time, a patient arrives with no weight and a height of 150 cm, so is used: 150 ≤ 155, and the patient goes to the "weight ≤ 50" side.
Fun Time: how to read a decision subset
Question (page 17): For a categorical branching criteria with . Which of the following is the explanation of the criteria?
- if -th feature is of type 1 or type 6, branch to first sub-tree; else branch to second sub-tree
- if -th feature is of type 1 or type 6, branch to second sub-tree; else branch to first sub-tree
- if -th feature is of type 1 and type 6, branch to second sub-tree; else branch to first sub-tree
- if -th feature is of type 1 and type 6, branch to first sub-tree; else branch to second sub-tree
Answer: 2. Two things have to be checked. First, means equals some element of ; the handout's answer notes that "" is an "or"-style condition on the elements of S in human language. A feature has only one value at a time and cannot be both type 1 and type 6, so the conditions in choices 3 and 4 never hold. Second, when the bracket holds its value is 1, and adding 1 gives , the second sub-tree; when it does not hold, , the first sub-tree. Choice 1 gets these two numbers the wrong way round.
When the lecture goes over this question, what is said is that you go right when the condition holds and left when it does not, treating the second sub-tree as the one on the right. But earlier in the same video, in the missing-value example, what is said aloud is that 50 kg or less goes left. Left and right are only a spoken habit of drawing trees, and the two passages are not consistent; the handout's formula has no left or right, only numbers: 2 when the bracket holds and 1 when it does not. For exercises and for code, go by the formula. The numerical examples in this article state directly what follows when a condition holds and when it does not, to avoid confusion.
What it looks like when it runs: side by side with AdaBoost-Stump
(Video 00:00–03:24 is the step-by-step demonstration on simple data, 03:24–05:10 complicated data, 05:10–06:39 the specialties of C&RT and C4.5, 06:39–07:07 Fun Time, and the summary follows.)
A simple data set: one cut at a time
Page 18 demonstrates on a two-dimensional binary classification data set; 5 circles and 5 crosses can be counted in the figure. The handout breaks the process into a series of figures, which in words are:
- The first cut is a vertical line toward the left. The left side holds only 2 circles and is already pure; the right side is still mixed, so keep cutting.
- The second cut is a vertical line inside the right region, leaving 3 crosses between the two cuts.
- The third cut is again a vertical line, just to the right of the second, enclosing a narrow strip that contains 2 circles.
- The fourth cut switches to a horizontal line and cuts only the rightmost region: 1 circle above, 2 crosses below.
The lecture explains the recursive structure while cutting: after each cut, each side has to grow a small tree of its own before being joined back. At the fourth cut Lin remarks that the first three cuts were all vertical, and this time the algorithm takes a look and finds that a horizontal cut is easier. After four cuts all 5 regions are pure and there is nowhere left to cut; each region returns its own constant (the handout's figure colours the circle regions blue and the cross regions red), and they are joined back level by level into the whole tree.
The last figure puts C&RT side by side with AdaBoost-Stump from the previous lecture. Both use horizontal and vertical lines, and the difference is in the extent of those lines. The lecture's explanation: every stump of AdaBoost-Stump votes over the entire plane, so every cut it makes runs across or down the whole plane; a C&RT cut is made under a condition, for example the fourth cut exists only under the condition "already in the rightmost region" and does not extend into other regions. Under this figure the handout writes C&RT: 'divide-and-conquer'.
A complicated data set: conditional cuts are more economical
Page 19 switches to a data set with many points and a curved boundary. The lecture compares the two with an animation, each adding 10 cuts at a time:
- AdaBoost-Stump, after 10 rounds, already shows the rough shape of the curved boundary, and every further 10 rounds fine-tunes the boundary.
- C&RT cuts downward recursively. The first 10 cuts decide only whether a few regions are circles or crosses, and the other regions are still undecided; with every further 10 cuts, more and more regions are decided. By the fourth advance the whole tree is finished, and the boundary has that same curved shape. In the lecture, Lin says some places in the figure look less than perfect only because of the plotting resolution.
What the handout shows on this page is the final result on both sides, with the AdaBoost-Stump figure labelled t = 90, and the conclusion is C&RT: even more efficient than AdaBoost-Stump. The lecture explains why: C&RT cuts under all sorts of different conditions and can cut more finely, unlike AdaBoost-Stump, where every cut has to be a complete horizontal or vertical line.
This article adds one way of looking at it: this matches the formula of the path view. AdaBoost's is a weighted sum of all the , and every acts on every ; a decision tree's lets each leaf look after only its own region. To fix the boundary in one corner of the plane, the tree only needs one more cut in that corner.
The five specialties of C&RT
Page 20 lists the practical specialties in five points:
- human-explainable.
- multiclass easily.
- categorical features easily.
- missing features easily.
- efficient non-linear training (and testing).
The handout continues: almost no other learning model shares all such specialties, except for other decision trees. The lecture uses SVM for contrast: SVM can be non-linear, but it is comparatively less efficient, and handling categorical data with it is not easy.
The same page mentions another popular decision tree algorithm, C4.5, with different choices of heuristics. In the lecture, Lin explains that C&RT made its own design at several key points, C4.5 made different decisions at those points, and so it became another algorithm. This is the "no single representative algorithm" from the discussion of pros and cons.
Fun Time: what C&RT lacks without pruning
Question (page 21): Which of the following is not a specialty of C&RT without pruning?
- handles missing features easily
- produces explainable hypotheses
- achieves low
- achieves low
Answer: 4. The handout's answer: the first two choices are easy; the third comes from the fact that fully grown C&RT greedily minimizes (almost always to 0); but as you may imagine, overfitting may happen and may not always be low. The lecture says the same: without pruning the tree just keeps growing, is very low, and not necessarily so.
The numerical example walks through all four choices. Choice 3: has . Choice 4: it gets 1 point wrong on the validation data, worse than the pruned . Choice 1 relies on the surrogate branch and choice 2 on the structure of the tree itself, and neither has anything to do with whether the tree is pruned.
The summary on page 22 reduces each of the four sections to one line: the decision tree hypothesis expresses path-conditional aggregation; the algorithm is recursive branching until termination to base; the heuristics in C&RT are pruning, categorical branching, and surrogate; in action it is explainable and efficient. At this point the lecture says that the basic aggregation models have all been covered. The preview of the next lecture is aggregation of aggregation: combining the aggregation models learned in these lectures once more.
Self-check
1. A node holds 10 data points, with 5, 3, and 2 points in three classes. Compute the Gini index and the classification error. If this node is a leaf, what does it return?
Answer: the proportions are 0.5, 0.3, 0.2. Gini is . The classification error is . The leaf returns the first class (the majority). You can check the upper bound along the way: the maximum Gini for three classes is , and 0.62 is already very close, which means this data is quite impure.
2. A regression problem with one feature and 5 data points : , , , , . Using regression error as the impurity, where does the root of C&RT split? What does each of the two leaves return?
Answer: the weighted impurity of each of the four splits is the sum of the total squared errors on the two sides.
| split | the "≤" part: average, total squared error | the ">" part: average, total squared error | total |
|---|---|---|---|
| , | , | ||
| , | , | ||
| , | , | ||
| , | , |
The split is at , and the two leaves return and . Without a split the average is and the total squared error is . A fully-grown tree would keep splitting (the values are not yet all the same); this question asks only about the root.
3. Binary classification with the labels written as , where the proportion of is . What is the "regression error" impurity in this case? How is it related to the Gini index?
Answer: the average is . Each point with contributes , and their proportion is ; each point with contributes , and their proportion is . So the impurity is , exactly half of the Gini index . The two differ only by a constant factor, so they rank splits in the same order. For example, at : and .
4. A fully-grown tree has 6 leaves. For the sequence of trees obtained by pruning, the number of leaves and are, in order, (6, 0), (5, 0.02), (4, 0.05), (3, 0.15), (2, 0.20), (1, 0.40). Which tree is chosen at ? And at ?
Answer: at , is, in order, 0.24, 0.22, 0.21, 0.27, 0.28, 0.44, so the 4-leaf tree is chosen. At it is 0.06, 0.07, 0.09, 0.18, 0.22, 0.41, so the fully-grown tree is chosen: each leaf removed saves only 0.01, while goes up by at least 0.02.
5. The handout says "when all are different, the fully-grown tree has ". If two data points have exactly the same but different , what happens? Can this tree's still be 0?
Criterion: the answer should say that these two points end up on the same side however the data are cut (no stump can separate them) and finally land in the same leaf; that leaf is forced to stop because "all are the same" and returns the majority, at least one of the two is wrong, and so . An answer that also points out this corresponds to the second termination condition on page 11 is complete.
Exercises and corresponding reading
The corresponding week in two semesters. The Fall 2024 course page puts this lecture in W12 (11/18), the same week as AdaBoost, Random Forest, and GBDT, and the slide code is 209u. The Fall 2026 course page also puts it in W12 (11/25); the slides for that week were not yet online when checked on 2026-09-30, so this article uses only the MOOC handout.
Textbook. Neither course page maps this lecture to a chapter of Learning from Data. The further reading listed on the course page is Loh's survey, Classification and Regression Trees, which compares the various decision tree algorithms side by side, and Classification and Regression Trees by Breiman et al., the original CART book, for which the course page links to the Amazon book page. This lecture takes only selected components of CART; if you want to know how algorithms such as C4.5 each fill in the four choices, Loh's survey is the quicker way in.
Homework. In Fall 2024 HW7 (released 2024-12-02, due 12-16), the problem directly related to this lecture is Q2: after several impurity functions (classification error, squared error, entropy, closeness) are each normalized by dividing by their maximum, which one is equivalent to the Gini index? There are no official solutions. Self-check question 3 works as a warm-up: first write the impurity as a formula in the class proportions, then compare it with the Gini index. This lecture had no programming problem in Fall 2024. The guide to the whole assignment is in Techniques Homework and Final Project.
If you want to get your hands dirty. Write a recursive C&RT yourself, run it on the 14 data points of the numerical example, and check the 8 scores at the root; the program is not long, and the lecture says a freshman can write it. Afterwards you can compare with scikit-learn's DecisionTreeClassifier (what follows was actually run on scikit-learn 1.9.1): with criterion="gini", the tree grown from these 14 points is the same as the one computed by hand in this article, with the root split at debt 5, the splits below at income 13 and income 10, and 4 leaves in total. max_depth limits the height of the tree directly. ccp_alpha defaults to 0, which means no pruning; set to a positive number, it prunes with "the impurity of the leaves plus α times the number of leaves", the same idea as this lecture's but different in the details: it computes impurity instead of , and it can remove a whole sub-tree at once. So on these same 14 points, the sequence of trees it prunes to has 4, 3, and 1 leaves, and does not line up tree by tree with this article's to .
How other courses on this site cover the same topic. Harvard CS181 HW4: Decision Trees, Random Forests, and Mixture of Experts, CMU 10-301 HW2: Decision Trees.
Next lecture. Techniques Lecture 10: Random Forests puts this lecture's fully-grown trees into Bagging: the tendency of a single tree to overfit is handled by having many trees vote.
Course video sources
Original videos: Decision Tree Hypothesis, Decision Tree Algorithm, Decision Tree Heuristics in C&RT, Decision Tree in Action
Checked: 2026-10-13. The video IDs, titles, and lengths were taken from the official playlist that day; the videos have no caption track on YouTube, and what this article reports from the lecture comes from transcripts made for it.
Update Log
- 2026-10-10: Marked the video status and checked the recording sources and how to obtain them.
- 2026-10-10: Re-checked the video status. The embedded videos match the lectures in the official playlist.
- 2026-10-13: Originally one article together with Lectures 10 and 11; changed to one article per lecture. This article covers only Techniques Lecture 9 and was rewritten from the handout and the video transcripts.
References
- Techniques Lecture 9 handout, 209_handout.pdf: the main source for this article. The content of the four sections, the four Fun Time questions, the tree on page 3, and the demonstration figures on pages 18 and 19 all come from this handout.
- Machine Learning Techniques YouTube playlist: the IDs and titles of the four videos; what the text reports as said "in the lecture" is paraphrased from machine transcripts of these four videos.
- Hsuan-Tien Lin > MOOCs: the official entry for handouts and recordings.
- Machine Learning, Fall 2024 course page: the corresponding week, the 209u slides, and the further reading list.
- Machine Learning, Fall 2026 course page: the corresponding week.
- Fall 2024 Homework 7 (hw7.pdf): the content of Q2.
- Loh, Classification and Regression Trees: further reading listed on the course page, a survey of decision tree algorithms.
- Breiman et al., Classification and Regression Trees (Amazon book page): further reading listed on the course page, the original CART book.
- Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques: Course Overview: where this lecture sits in the whole course and the reading routes.