Decision Trees
A series of simple questions can narrow down a decision. A decision tree learns which questions to ask from the examples and places a prediction at the end of each route.
24 sections · 15 questions · 4 coding challenges
The arrow keys move between sections too.
Could We Reach an Answer by Asking Smaller Questions?
BOTIE
The 1984 Classification and Regression Trees book brought together a systematic treatment of learning tree-shaped prediction rules. It addressed both how to split observations and how to avoid keeping every split that improves the training fit. The problem was not merely drawing a flowchart; it was finding a useful flowchart from data.
At each step, a tree asks a question about an input and divides the examples according to the answer. Each resulting group can ask another question. The final groups provide predictions. A sequence of simple questions can describe quite different regions, but continuing until every training case is isolated usually records details that will not repeat. Stopping and pruning are therefore part of the method.
Sources for this history
Which Question Should Come First?
Imagine identifying an object by asking yes-or-no questions. Each answer rules out some possibilities and tells you which question to ask next. A decision tree makes predictions in that same inspectable sequence.
The questions must be useful for the data, and we need to know when to stop asking.
One question divides the examples
Ask whether a measurement exceeds a chosen threshold. Every observation goes to one of two groups. A tree starts with this split rather than with a smooth prediction curve.
Question
- Is the measurement above the threshold?
Two routes
- No: left group
- Yes: right group
The split score formalizes a useful question
We can now introduce impurity as a measure of how mixed a group is, then measure how much a candidate split reduces it. The tree searches for questions using that score.
The worked examples show both the route a prediction takes and the limits of growing a very detailed tree.
Explore the full example
Follow one point from the first question to its final leaf. Then compare how changing the tree's depth changes its prediction regions.
…
Part 1. A Model Built from Questions
1. A prediction made from questions
Press the ideal case button in the box above. Eleven people, five children and six adults, and the whole model is one question.
Is height less than 151.5 cm?
yes → predict child
no → predict adultThat is a decision tree with one question in it. The plane is cut in two at 151.5 on the height axis, everyone to the left is called a child and everyone to the right an adult, and the printed tree under the plot is the same rule as text. A tree predicts by routing an observation through a sequence of questions, and here the sequence has length one.
2. Roots, nodes, branches, leaves, and paths
| part | what it is |
|---|---|
| root | the first question, asked of everyone |
| internal node | a later question, asked of whoever reached it |
| branch | one possible answer, yes or no |
| leaf | a final prediction, with no question left to ask |
| depth | how many questions lie along a path from the root |
| path | the sequence of questions one observation follows |
A tree is a hierarchy with questions at the nodes and predictions at the leaves. Load the muddled crowd above and set the depth cap to two for a tree that has all of these, a root, two internal questions beneath it, and four leaves.
3. Walking one observation through the tree
A grown tree is a route map, and the way to feel that is to send someone down it. The crowd below is sixteen people chosen so that both questions matter, and the tree is kept two questions deep so the whole map fits in one look.
Press a visitor or move the sliders, and the route lights up as the tree asks its questions.
A person’s height reaches the root question. The answer picks a branch. On one branch a second question checks weight. The person reaches a leaf, and the leaf supplies the prediction. Nothing else in the tree is consulted. Prediction uses only the questions encountered along that observation’s own path.
4. Different observations, different paths
Send the small child down and they are settled by the root alone, since everyone under 153.5 cm here is a child, and their weight is never read. Send the slender teenager down and the root cannot settle them, so a second question about weight is asked. Same tree, one question for one person and two for another.
Path length varies across observations, and a shorter path is not a better or more confident answer. It is a cheaper one. The tree asked as much as it needed to reach a leaf and no more, which is a quiet efficiency, and it says nothing about which prediction is more likely to be right.
Part 2. Questions as Geometric Splits
5. Thresholds as geometric cuts
Every question compares one measurement with one number. “Height less than 151.5” is a vertical line on the plane, with everyone sent left or right of it. “Weight less than 55” is a horizontal line, with everyone sent below or above. A threshold question divides the current region along one feature axis, and it can only ever divide it that way.
6. Building rectangular regions
Start with one vertical cut. Add a horizontal cut to one side only. That side is now two rectangles and the other side is still one, and every further question splits one existing rectangle into two. The tree below grows one split at a time, with its rectangles growing alongside.
…
Slide from zero to eight splits and watch each new question carve one rectangle. Every leaf is one rectangle and every rectangle is one leaf, and the path to a leaf is the list of cuts that fenced its rectangle off. A standard axis-aligned tree builds its decision map by repeatedly dividing existing rectangles into rectangles, which is why the map can never hold a slanted edge.
7. Trees versus linear boundaries
Fit both models to one crowd and the difference is the shape of the boundary. Logistic regression draws one straight line at whatever angle the data asks for. The tree draws vertical and horizontal cuts, and where the true boundary is slanted it needs a staircase of them.
…
On the slanted crowd the line does in one cut what the tree needs several rectangles to approximate. On the muddled crowd, where the classes interleave in a way no line can follow, the tree reaches an accuracy of 0.857 with nine rectangles and the line manages 0.429. Neither is the better model in general. They make different geometric assumptions, and each is right where its assumption is.
Part 3. What Makes a Useful Question?
8. Useful and useless questions
Before any formula, compare three questions on the clean crowd by eye. The inspector below starts on a poor one.
The slider snaps to the 0 candidates the search tries for this feature, each halfway between two neighbouring distinct values.
| question | left side | right side | what it did |
|---|---|---|---|
| height < 121 | 2 children | 3 children, 6 adults | peeled two children off and left the rest nearly as mixed as before |
| height < 146 | 4 children | 1 child, 6 adults | most of the way there, one child on the wrong side |
| height < 151.5 | 5 children | 6 adults | both sides pure |
A useful question creates child groups whose mixtures are cleaner than the parent’s. Which of the three is best needs no arithmetic. What needs arithmetic is ranking the seventeen other candidates the search also tries, and that is what the next Part builds.
Cleaner is the right test because of what a node is for. A node that stops splitting becomes a leaf, and a leaf predicts its majority for everyone who reaches it, so every person in the minority of a mixed node is a mistake the tree is committed to making. The third question above leaves no minority on either side, and so gets all eleven people right on its own. The first leaves three children among six adults on its nine-person side, three mistakes waiting to happen, and the second leaves one. A question is useful to the extent that knowing its answer tells you something about the label, and a smaller mixture on each side is what that means.
9. Pure and mixed nodes
A node is pure when every observation in it has the same label. Five children and no adults is pure. Four children and one adult is mildly mixed. Five and five is as mixed as two classes can be. The split score has to measure the degree of mixture, so that a question can be scored by how much mixture it removes, and section 10 is that measure.
The measure has to be a number rather than a judgement, so that nineteen candidates can be ranked rather than three compared by eye, and two things are fixed about it before any formula is chosen. It must be zero for a pure node, since there is no mixture left to remove, and it must be largest at an even split, since five and five is the most mixed two classes can be. Any measure with those two properties, rising as the mixture does, ranks the three questions above the same way. Which one is used decides the arithmetic in between, and the next Part picks the one with the simplest story behind it.
Part 4. Gini Impurity
10. Gini impurity
Write the share of children in a node as p and the share of adults as 1 − p.
G = 1 − p² − (1 − p)²Worked example
| node | p | G |
|---|---|---|
| pure, all children | 1 | 1 − 1 − 0 = 0 |
| evenly mixed | 0.5 | 1 − 0.25 − 0.25 = 0.5 |
| three quarters children | 0.75 | 1 − 0.5625 − 0.0625 = 0.375 |
Each pair is two labels drawn from the mixture with replacement, so the same person can be drawn twice. Drawing two different people without replacement gives a slightly different number.
Zero for a pure node, one half at an even mixture, and symmetric, so a node that is three quarters adults scores the same 0.375 as one that is three quarters children.
11. The probability meaning of Gini
The formula is a thought experiment written down. Treat the node’s class shares as a probability distribution, draw one label from it, draw a second label from it independently, and ask whether the two differ.
P(the two agree) = Σₖ pₖ²
G = P(the two differ) = 1 − Σₖ pₖ²A pure node cannot produce a disagreeing pair. An even mixture disagrees half the time. The simulator under the curve draws pairs from the mixture and its disagreement rate settles on the Gini value as the pairs accumulate.
Keep in mind
The draw is from the class distribution with replacement, so the same person can be drawn twice. Picking two different people out of the node without replacement is a slightly different calculation, and for a node of five children and six adults it gives 6/11 rather than 60/121. Gini is the first of these, and the difference vanishes as nodes get large.
12. Calculating root impurity
Five children and six adults.
p_child = 5/11 p_adult = 6/11
G_root = 1 − (5/11)² − (6/11)² = 1 − 25/121 − 36/121 = 60/121 ≈ 0.496Close to the two-class maximum of 0.5, so the root is highly mixed, which is right. What the number does not yet say is which question to ask. It describes the mixture before any question, and a question is scored by how much of it is removed.
Part 5. Scoring One Split
13. Scoring one candidate split
Go back to the inspector in section 8 and pick the middling question, height less than 146. The left side holds four children and nothing else, so its Gini is zero. The right side holds one child and six adults.
G_right = 1 − (1/7)² − (6/7)² = 1 − 1/49 − 36/49 = 12/49 ≈ 0.245A candidate split creates two new nodes, each with its own mixture and its own impurity, and the inspector shows both containers with their counts, shares and Gini for whichever question is chosen.
14. Why child impurities are weighted
Height less than 121 makes a tiny pure node of two and a large mixed node of nine. A plain average of the two impurities, 0 and 0.444, is 0.222, and it gives the two-person node as much say as the nine-person one. So the children are weighted by how many observations each holds.
G_after = (n_left / n)·G_left + (n_right / n)·G_rightheight < 121
G_after = (2/11)·0 + (9/11)·0.444 = 0.364Higher than the plain average, because most people are in the mixed node and a randomly chosen person is far more likely to land there. The containers in the inspector are drawn at the width of their share for this reason.
15. Impurity reduction
gain = G_parent − (n_left / n)·G_left − (n_right / n)·G_rightParent impurity is the mixture before the question. Weighted child impurity is what remains after it. The gain is what the question removed.
| question | G_after | gain |
|---|---|---|
| height < 121 | 0.364 | 0.496 − 0.364 = 0.132 |
| height < 146 | 0.156 | 0.496 − 0.156 = 0.340 |
| height < 151.5 | 0 | 0.496 − 0 = 0.496 |
The perfectly separating split leaves both children pure, so its gain is the whole of the parent's impurity.
The tree prefers the question that removes the most weighted impurity right now, and section 20 is about the word now.
Part 6. Searching for the Best Question
16. Generating candidate thresholds
For a continuous feature, sort the observations by it, find each pair of neighbouring distinct values, and put a candidate threshold halfway between them. A threshold anywhere else in the same gap divides the observations identically, so it cannot score differently, and a threshold beyond the ends divides nothing.
Worked example
| sorted heights | candidates |
|---|---|
| 145, 150, 153, 160 | 147.5, 151.5, 156.5 |
| the clean crowd's eleven heights | 119, 121, 133.5, 146, 151.5, 157.5, 160.5, 170, 179, 181.5 |
Eleven distinct heights give ten candidates, and the slider in the inspector snaps to exactly those. The weights give nine more. Nineteen questions in all, which is the whole search space for this crowd.
17. Searching across features
Score every candidate on every feature and the search is a leaderboard. The chart below is every question the root weighed on the clean crowd, placed by its threshold and raised by its gain.
Every question the root weighed is drawn, and the tallest one became the tree's first split.
The winner is the tallest mark, height below 151.5 at a gain of 0.496. The selected question is the feature-and-threshold pair with the largest immediate impurity reduction, and nothing more subtle than that.
18. Resolving equal split scores
Two candidates can earn exactly the same gain. On the clean crowd, height below 146 and weight below 47 both score 0.340, because both peel off the same four children. A complete implementation has to say what happens then, and the usual answers are feature order, threshold order, the order the search scanned in, or a seeded random choice.
The rule matters because different tie policies produce different trees with identical immediate gain, and the multiclass and ensemble pages both met cases where two implementations broke a tie differently and grew apart from there. Every tree on this page keeps the first candidate scanned, with a small tolerance so that two gains reached by different arithmetic and differing in the last bits count as a tie.
Check yourself
Questions on Parts 1 to 6
Question 1 of 5
A person settled by the root alone has been given a more confident answer than one who needed a second question.
Part 7. Growing the Tree Recursively
19. Recursive tree growth
After the root splits, send each observation to the left or right child, treat each child as a smaller dataset, and run the same candidate search inside it. Repeat until a stopping rule applies. The growth widget in section 6 is this, split by split, on the muddled crowd.
The first three splits on the muddled crowd
| split | at depth | people in the node | question | gain |
|---|---|---|---|---|
| 1 | 0 | 25, 12 children and 13 adults | height < 147.5 | 0.188 |
| 2 | 1 | 9, 8 children and 1 adult | weight < 52.5 | 0.049 |
| 3 | 1 | 16, 4 children and 12 adults | height < 158 | 0.075 |
Each split is chosen from its own node's people alone. The root's gain is modest, since this crowd was built so that no single question separates it.
A decision tree is built by applying one local search, over and over, to smaller and smaller subsets of the data. There is no global step anywhere in it.
20. Greedy construction
At each node the search takes the best immediate gain. It does not construct every possible tree, weigh what a split might make possible later, or guarantee the smallest or best tree. Searching every complete tree is impractical, so the construction makes locally strong choices, and locally strong is not globally optimal.
The crowd below is the case that shows it. Four children on one diagonal, four adults on the other, so the classes are perfectly separable by two questions, height and then weight. But the root cannot see that far.
Every question the root weighed is drawn, and the tallest one became the tree's first split.
Every one of the six candidate questions leaves both sides exactly as mixed as the parent, so every gain is zero, the search admits nothing, and the tree stays one leaf calling everyone the same class at an accuracy of 0.5. A search with one step of lookahead would find the two-question tree that gets all eight right. The greedy search cannot, because the first question earns nothing on its own, and it is only worth asking for what it makes the second question able to do.
Part 8. Making a Prediction at a Leaf
21. Predictions at pure and mixed leaves
A pure leaf is the easy case. Every training observation in it is a child, it predicts child, and its observed shares are one and zero. A tree may stop before a leaf is pure, though, and then the leaf predicts its majority and can report its observed shares.
A leaf of six children and two adults
prediction = child
P̂(child | leaf) = 6/8 = 0.75 P̂(adult | leaf) = 2/8 = 0.25Click any leaf in the section 6 widget, in the tree or on the map, and its card gives who reached it, the counts, the majority and the shares. Set the depth cap to two there and several leaves are mixed.
Keep in mind
Those shares are estimates from the training observations that happened to reach the leaf. A leaf of four people gives shares in steps of a quarter, and a leaf of one gives certainty about a single person, which is not certainty about anything. Small leaves produce unstable and overconfident estimates, and when the probabilities matter rather than the majority, smoothing or calibration on held-out data is the usual repair.
22. Reading the ideal tree
| value | |
|---|---|
| root question | height < 151.5 |
| root Gini | 60/121 ≈ 0.496 |
| left leaf | 5 children, 0 adults, Gini 0, predicts child |
| right leaf | 0 children, 6 adults, Gini 0, predicts adult |
| gain | 0.496 |
| training accuracy | 1.000 |
| leaves | 2 |
| depth | 1 |
When one split leaves both children pure, the tree stops, because no question inside a pure node can remove any mixture. Every number in the table is linked to the box at the top of the page with the ideal case loaded.
Part 9. Stopping Tree Growth
23. Why tree growth must stop
With no restriction a tree keeps dividing until every leaf is pure, or every observation is alone in its own rectangle, or no eligible split is left. On the muddled crowd that takes ten splits and eleven leaves, seven questions deep, to reach a training accuracy of 1.000, with the last two splits each fencing off a single person at depth six. The growth widget below caps the depth at six, so it stops two splits short of that, at nine leaves and 0.857 with its two deepest leaves still mixed. Pure training leaves do not mean good predictions for new observations, and a tree needs rules for when more specialisation is no longer justified.
24. Pre-pruning controls
Growth can be held back before it happens, and there are several controls, each limiting it differently.
| control | what it limits |
|---|---|
| maximum depth | the longest path from the root |
| minimum samples to split | a node with fewer observations becomes a leaf |
| minimum samples per leaf | no split may leave a child smaller than this |
| minimum impurity decrease | a split must earn at least this much gain |
| maximum leaves | the total number of rectangles |
…
Change one control at a time and watch the tree, the map and the leaf count. A minimum of four people per leaf stops the single-person rectangles the unrestricted tree grew, and the tree stops at three leaves and a training accuracy of 0.714 with the root question unmoved. Depth is only one of these controls, and an extra level of depth can at most double the number of leaves, which is not the same as doubling the questions available, since most nodes stop splitting before they reach the cap.
25. Post-pruning
The alternative is to grow a large tree first and cut it back. Measure whether each branch improves things enough to be worth its complexity, and replace the ones that do not with a leaf. Cost-complexity pruning writes that down as one objective.
adjusted objective = training leaf error + α × number of leavesA larger α buys fewer leaves, and the sequence of trees as α grows is a sequence of ever simpler candidates to judge on held-out data. Pruning simplifies an already grown tree by removing branches that do not earn their complexity, where the controls above stop them being grown at all.
In a model
Every tree on this page grows under the controls of section 24 and none of them prunes, so the pruning sequence is described here rather than drawn. It sits on the roadmap beside the ensembles, which are the other answer to the same problem.
Part 10. When Trees Overfit
26. Training and validation performance by depth
Forty-four people whose classes overlap the way real measurements do, thirteen of them held back by a seeded shuffle, and a tree grown on the other thirty-one at every depth cap from one to six.
…
| depth cap | leaves | training accuracy | held-out accuracy |
|---|---|---|---|
| 1 | 2 | 0.935 | 0.846 |
| 2 | 4 | 0.935 | 0.846 |
| 3 | 6 | 1.000 | 0.692 |
| 4 to 6 | 6 | 1.000 | 0.692 |
Training accuracy never falls as the cap rises. Held-out accuracy falls from 0.846 to 0.692 at the cap where the training score reaches 1.000.
A deeper tree describes the training data more closely while becoming less reliable on people it never saw. The two extra leaves that took training accuracy from 0.935 to perfect cost two of the thirteen held-out people, because they fenced off rectangles around individual training observations that the held-out observations do not respect.
27. Isolating individual observations
Go back to the growth widget in section 24 with the controls at their defaults and step to the last two splits. The seventh acts on six people at depth four, earns a gain of 0.044, and leaves one person alone in a rectangle, and the eighth acts on five people at depth five, earns 0.013, and leaves both of its sides still mixed. Five questions were spent to fence off one observation, and the cap is the only reason more were not.
Whether that was worth spending depends on what the observation is, and the tree cannot know. It might be a measurement error, a mislabelled person, a genuine rare case, a sign that a feature is missing, or a sign that the real pattern is complicated. A tree spends questions on an observation either way. What the held-out score in section 26 measures is whether the fence around it turned out to be a repeatable pattern.
28. Bias, variance, and tree depth
| shallow tree | deep tree | |
|---|---|---|
| regions | few and simple | many and specific |
| can represent | coarse structure only | detailed interactions |
| misses | real interactions, higher bias | less on the training set, lower bias |
| changes with the sample | usually little, lower variance | usually a lot, higher variance |
Depth trades structural simplicity for sensitivity to the particular sample. Section 29 shows the sensitivity directly, and it is what the forests page is built to average away.
Check yourself
Questions on Parts 7 to 10
Question 1 of 5
On the crossed crowd the search finds the two-question tree that gets all eight people right.
Part 11. Tree Instability
29. Tree instability
Change one label in the muddled crowd and grow the tree again.
…
A single relabelled person can move the root question, and everything beneath the root is decided inside the halves it made, so the branches reorganise and large regions of the map change their prediction. The second mode grows a tree on each of five random four-fifths of the crowd and overlays their root cuts, which land in different places for a reason that is mostly which people happened to be left out. A single decision tree is unstable because an early split changes every decision below it.
30. Why forests average many trees
That instability is the motivation for the next page, and the motivation is more precise than a crowd of memorisers somehow generalising. Individual deep trees have high variance. Train many of them on deliberately varied samples and feature subsets, so their errors are not the same errors, and average their predictions or take their vote. Averaging reduces variance when the trees are diverse enough, and random forests are engineered to make them diverse. How the varying is done is bagging and random forests, and belongs there.
Part 12. What Trees Handle Well
31. Why trees usually do not need scaling
Measure the heights in metres instead of centimetres and the question becomes height less than 1.515. The same people are on each side of it, because dividing every value by a hundred keeps their order, and a threshold question reads nothing but the order.
The tallest child is 147 cm and the shortest adult 156 cm. Halfway is 151.5 cm, or 1.515 m, and every person is on the same side of it in either unit.
So an ordinary tree generally does not need standardisation, and any monotonic transformation of a feature leaves the tree it grows unchanged apart from the numbers printed at the thresholds. Preprocessing can still be needed for other reasons, missing values and encodings among them, and the gradient-descent models on earlier pages needed scaling for reasons that do not apply here at all.
32. Conditional feature interactions
In the walking widget of section 3, weight is never consulted for anyone under 153.5 cm and decides everything for anyone over it. That is a conditional interaction, weight mattering only within one height range, and a tree expresses it without being told to, because a later question is only asked inside the region an earlier answer selected. A linear model would need the interaction built into its columns by hand.
33. Categories and missing values
Everything on this page is a numeric threshold. Categorical features and missing values are handled differently by different implementations, and there is no single behaviour to describe. Categories may be one-hot encoded, ordered, or partitioned natively. Missing values may be imputed, sent a learned default direction, routed by a surrogate question, or given a branch of their own. How a tree handles them is a decision an implementation has to make explicitly, and the trees here take finite numeric columns only.
Part 13. Limitations of Tree Interpretability
34. The limits of interpretability
A one-question tree can be read in a glance. The nine-leaf tree on the muddled crowd can be read with effort. A tree with twenty levels and thousands of leaves is still technically a list of rules, and nobody reads it in full. Depth, leaf count, average path length and the number of rules all grow, and past a point the structure is explicit without being understandable. A tree is readable when it is small, and being a tree does not keep it small.
35. Prediction rules versus causal explanations
If the tree asks about weight after height, that means the question improved prediction on the training data inside that branch. It does not show that weight causes the outcome, that 55 kilograms is a natural line in the world, that changing someone’s weight would change their class, that the training data was unbiased, or that the rule is fit for a decision with consequences. A path explains how the model produced its prediction. It does not explain why the real outcome happened.
Part 14. Deriving the Split Score
36. Deriving Gini gain
Part 5 scored a split as the parent’s impurity less the weighted impurity of its two children, and the weights were argued for rather than derived. The whole score comes from one thought experiment, draw two labels from a node and ask whether they differ, applied once to the parent and once to the node a random observation lands in after the question.
›From two draws to the gain
| Expression | What changed |
|---|---|
| P(both draws are class k) = pₖ² | two independent draws from the node's class shares |
| P(agree) = Σₖ pₖ² | summed over the classes |
| G = 1 − Σₖ pₖ² | disagree is the complement |
| G_root = 1 − (5/11)² − (6/11)² = 60/121 | the worked crowd |
| P(reach left child) = n_left / n | a random observation lands left with this probability |
| G_after = (n_left/n)·G_left + (n_right/n)·G_right | the expected impurity of the node reached after the split |
| gain = G_parent − G_after | expected mixture removed by asking |
The clean crowd, twice
height < 151.5: gain = 0.496 − (5/11)·0 − (6/11)·0 = 0.496
height < 146: gain = 0.496 − (4/11)·0 − (7/11)·0.245 ≈ 0.496 − 0.156 = 0.340The first is the winner of section 17’s leaderboard and the second is the middling question of section 13, and the two numbers are the ones the leaderboard drew for them.
Positive gain means the split reduces the expected impurity. Zero means it leaves it unchanged, which is section 20’s crossed crowd at every candidate. Larger means a cleaner immediate division, and impurity gain is nothing more than the expected label mixture a question removes.
Part 15. Extending the Mechanism
37. Multiclass trees
For several classes, count each class in the node and turn its count into a share. The Gini calculation includes every class.
G = 1 − Σₖ pₖ²The rest of the procedure stays the same: compare candidate splits, choose one, and predict the majority class at a leaf. A tie between classes needs a stated policy, just as a tie between splits does.
38. Regression trees
A tree can predict a number instead of a class. At a regression leaf the prediction is the mean target of the training observations that reached it, and a split is scored by how much it reduces the squared error, or equivalently the variance, of the targets on the two sides.
| classification leaf | regression leaf | |
|---|---|---|
| what it holds | class counts | numeric targets |
| what it predicts | the majority class | the mean target |
| how a split is scored | Gini impurity removed | squared error removed |
The structure is not tied to classification. The leaf summary and the split score change with the task, and the gradient boosting page fits trees of exactly this kind to residuals.
Part 16. Implementation and Failure Contracts
39. Implementation and failure contracts
A complete tree implementation has to define what it does with the cases the tidy description passes over.
| case | what has to be decided |
|---|---|
| empty training data | refuse, in words |
| missing or non-finite feature values | refuse or route by rule |
| a node holding one class | it is a leaf; no split can gain |
| a node with no valid split | it is a leaf at its majority |
| several splits with equal gain | the tie policy of section 18 |
| a constant feature | it offers no candidates |
| a leaf minimum larger than the data | the root is the only leaf |
| a threshold met exactly | less-than or less-than-or-equal, chosen once and kept |
| a category never seen in training | an implementation choice, section 33 |
| shares from a tiny leaf | reported as estimates, section 21 |
The comparison convention is the one people forget. An observation exactly equal to a threshold has to go the same way every time, in growth and in prediction, and here strictly-less-than goes to the left and everything else to the right, through the one routing rule both the walk in section 3 and the bulk prediction call. It also refuses the empty and non-finite cases by name rather than growing a tree on nothing.
Each row is a place where two implementations that agree on the tidy description can grow different trees, or grow one silently on nothing, so the trees here decide every row once. An empty column, a non-finite value and a target holding one class are each refused by name before any growth, since a tree has nothing to separate in any of them. A constant feature offers no candidate thresholds, so the search never asks about it. On the clean crowd with every height held at one value the tree grows on weight alone, rooting at weight below 47 and reaching four leaves at a training accuracy of 0.909, and reports that height contributed nothing. A leaf minimum larger than the crowd leaves the root as the only leaf, predicting the majority, which on the eleven people is adult at an accuracy of 0.545. And a person standing exactly on a threshold goes right, in growth and in prediction alike, because the question asks for strictly less.
Check yourself
Questions on Parts 11 to 16
Question 1 of 5
Measuring the heights in metres rather than centimetres changes which people a threshold question separates.
Try it with Botie
Build with Botie
Write and run Python