Work record: , 8 minute read

Happy little trees

When does a decision tree stop learning and start painting every accident?

We don’t make mistakes, just happy accidents.

Bob Ross

A decision tree takes that literally. Give it enough depth and it will paint a little region around every accident in the data, then present the result as a masterpiece.

In November 2023 my decision-tree script scored 100 % on the Iris dataset, printed the report and stopped. The README called decision trees “awesome and scary”. I was pleased with myself. Of everything in that sentence, scary aged best.

The 2023 terminal output: accuracy 1.0, every precision and recall 1.00
November 2023. Forty-five test flowers, forty-five right. A legitimate result on that split—not a measurement of how the tree behaves as its complexity changes.Open image to inspect at full size.

That result answered a small question: did this fitted tree classify those 45 held-out flowers? It did. It did not tell me how sensitive the result was to the split, how much complexity I needed, or what would happen when the labels were unreliable. Iris can be overfit too; a neat report does not make a dataset immune.

A canvas with known accidents

The later experiment changes the question rather than trying to invalidate the old score. make_moons generates 600 points, with coordinate noise of 0.20. Two curved classes overlap enough to make straight cuts imperfect. A separate random draw flips each binary label with probability 0.10, and then a fixed split leaves 360 training points and 240 test points. The generator, label-noise draw and split all use seed 11.

The two kinds of noise do different jobs. Coordinate noise moves a point around its underlying crescent. Label noise can give that point the opposite class even if its position is otherwise typical. The classifier sees only the resulting coordinates and corrupted training labels, not a flag identifying the accidents.

Replaying the code reveals why “10% noise” needs care:

splitpointslabels actually flipped
training36036
test24031
complete generated set60067

The probability is 10%; the realised fraction is not guaranteed to be exactly 10%. In this split, 31 of 240 test labels—about 12.9%—were changed.

An earlier version of this page said no honest model could beat 90%, and that anything above it must be memorisation. That was too strong. Under independent binary label flips with probability 0.10, a fixed predictor’s expected noisy-label accuracy is:

a = clean-label accuracy
E[noisy] = 0.10 + 0.80 × a

The predictor keeps a correct clean-label answer when the label is not flipped, with probability 0.90. It can also get a previously incorrect answer right when the label is flipped, with probability 0.10. Combining those cases gives the expression above.

Even a rule that perfectly recovered the clean labels would score 90% in expectation, not necessarily on every finite test sample. On this particular sample, comparing the generator’s clean test labels with the corrupted labels gives 209/240, or 87.08%. That is a diagnostic using information available inside the synthetic generator, not an achieved model score or a second learned classifier. It is not a new hard ceiling either: some model mistakes against the clean labels can accidentally agree with a flipped label.

The overlapping, perturbed coordinates also make perfectly recovering the clean labels an idealisation. The thin 90% line in the plot is therefore a conditional population reference—not a pass/fail threshold and not a way to accuse a high-scoring model of cheating.

Watching it memorise

Training accuracy reaches 1.0 as depth increases, while test accuracy varies between roughly 0.77 and 0.81 after depth 2
Twenty depth settings on the same fixed split. The 90% line is an ideal-signal expectation under independent label noise, not a ceiling on a finite test score.Open image to inspect at full size.
treeleavestraintest
depth 120.7940.742
depth 240.8580.787
depth 8490.9530.812
unlimited721.0000.783
pruned (ccp α = 0.0129, chosen by 5-fold CV)40.8580.787
  • Training fit and generalisation are different measurements. The unlimited tree classifies all 360 training points correctly, but only 188 of the 240 test points. Its 72 leaves describe a much more detailed partition than the four-leaf tree. The count alone cannot tell us which extra leaves isolate flipped labels rather than genuine local structure.
  • A small apparent win remains small. Depth 8 gets 195 test points right; the pruned tree gets 189 and the unlimited tree 188. Depth 8 wins on this split, but this experiment does not establish that the ordering survives another sample or random seed. One additional correct answer changes accuracy by about 0.42 percentage points.
  • Pruning makes the tradeoff inspectable. Training-only, five-fold cross-validation chooses ccp_alpha ≈ 0.0129, leaving four leaves. It uses far fewer branches than the unlimited tree and gets one more test answer right here. That is a measured complexity–accuracy tradeoff, not evidence that four leaves is universally optimal.
Decision regions at depth 1, pruned by cross-validation, depth 8 and unlimited
Left to right: depth 1, pruned by CV, depth 8, unlimited. The small regions show the added flexibility. Their appearance alone does not identify which training labels were corrupted.Open image to inspect at full size.

Why the picture becomes rectangles

At each internal node, this classifier chooses a feature and a threshold. In two dimensions that means a horizontal or vertical cut. Repeating those cuts can approximate a crescent with a staircase; enough cuts can also carve a small island around an inconvenient training point.

The default classification criterion is Gini impurity. For class proportions p(k) in a node, it is 1 − sum(p(k)²): zero for a pure node, 0.5 for an even mix of two classes. A candidate cut is judged by the sample-weighted impurities of its two children. The algorithm chooses the best local reduction, then continues recursively.

That is a greedy procedure, not a search over every possible whole tree. A locally helpful split can enable a complicated downstream partition. A training score of 1.0 says the final leaves are pure enough to reproduce these labels; it does not say the boundaries represent the process that generated new points.

Maximum depth is a blunt constraint: stop every branch after a prescribed number of decisions. min_samples_leaf would instead require evidence for every terminal region. Neither setting was separately tuned in this study; they are different ways to constrain the same tendency, not additional benchmark results.

What the palette knife actually removes

Cost-complexity pruning starts with a grown tree and asks whether a branch earns its keep. The objective trades off training impurity and the number of terminal regions:

cost(T) = R(T) + α × leaves(T)

In scikit-learn, R(T) is the total sample-weighted impurity of the leaves—not the test error. As alpha increases, an extra leaf needs a larger impurity reduction to justify its cost. The pruning path supplies candidate values at which branches can be removed.

The script excludes the final root-only candidate, scores the remaining candidates with five-fold cross-validation on the training set, chooses the first best mean score, and refits that choice on all 360 training points. The test labels do not choose its alpha.

There are still boundaries around that statement. The candidate path is built from the complete training set before the folds are scored, so these CV scores are a model-selection aid, not a nested, unbiased estimate of future accuracy. And the depth-sweep plot repeatedly exposes the same test set: choosing depth 8 because it looks best here would turn that set into development data. A final comparison after such a choice needs a fresh holdout.

The useful outcome is not “the simplest tree always wins”. It is that I can state the selection procedure, show what complexity buys on the same sample, and keep a six-point difference from becoming a grand claim about generalisation.

The napkin

A small tree also offers something a neural-network benchmark does not: I can follow every decision. python -m happytrees.export turns the fitted, CV-pruned tree into a C function. The generated inference function needs no Python runtime, NumPy or model loader; it is a candidate for a small embedded program, not evidence that I deployed this model on a robot.

/* 4 leaves, depth 2; generated by happytrees.export */
static inline int predict(const float *x) {
    if (x[1] <= 0.247126482f) {
        if (x[0] <= -0.487924725f) {
            return 0;
        } else {
            return 1;
        }
    } else {
        if (x[0] <= 1.53083217f) {
            return 0;
        } else {
            return 1;
        }
    }
}

The existing test compiles generated C with gcc and compares it with scikit-learn on all 600 generated points, for depth 2, depth 4 and unlimited trees. That checks the observed inputs through an actual compiler, rather than comparing two copies of a formula. It does not prove equivalence at every floating-point threshold, on missing values, or for every possible trained classifier; nor is it an Arduino timing, memory or hardware test.

An exporter that’s almost right is a bug with good manners.

The 2023 script still lives in the repo, scoring its 100 % on Iris, forever pleased with itself. I kept it there as a reminder of what a perfect score feels like from the inside.

What I would ask of it next

For a real sensor classifier, a random point split would be only the beginning. Consecutive frames or readings from one run are related. I would separate acquisition sessions or devices before fitting, keep an independent final test, and report mistakes by operating condition rather than only one aggregate score. Those are proposed requirements, not experiments already performed by this repository.

For the exporter, threshold-boundary inputs deserve special attention: Python’s model representation and a C float comparison need not round numbers identically. The 600-point agreement is useful evidence within its tested domain, not permission to skip that check before deployment.

The lesson I take back to the 2023 terminal is smaller and more useful than distrusting every perfect result. Keep the result. Then ask what it measured, what information chose the model, and whether a much smaller object would answer the same question.

Follow the experiment