AI from scratch · Chapter 13 · Stage 3: classic ML and judging models

The models that still win on tables

Most machine learning in companies isn't images or chat: it's tables. Rows of customers, payments, trips, sensor readings; columns of numbers and categories. On that kind of data, the winners are usually not neural networks but two older ideas: logistic regression (a line) and gradient-boosted decision trees (many small questions, each fixing the last ones' mistakes). An ML engineer reaches for these first.

1. A line, and where it fails

Each dot is a subway trip: x is how far it went, y how busy the hour was, both scaled to −1…1. Red trips ran late, blue ones didn't. Logistic regression (chapter 3's single neuron) draws one straight line between them.

The real rule here bends (long trips at rush hour run late, and so do short ones at the very busiest times), so a line gets maybe 70–80% right. You could hand-craft features (x², x·y…) to bend it, and people do. A tree finds the bends itself.

A line can cut the plane only once, and only straight; rules in real tables often bend.

2. A decision tree: twenty questions

Pick the decision tree above. It asks one yes/no question about one column ("is x < 0.31?"), then another question on each side, and so on: the plane gets cut into boxes, and each box predicts its majority. Raise the depth and watch chapter 11 happen again: deep trees carve a box around every noisy dot (train accuracy → 100%) and validation accuracy drops.

How does it pick a question? It tries every column and every cut point, and keeps the one that makes the two sides purest. Purity is measured by Gini impurity: the chance two random dots from the box have different colors.

\[ G = 1 - \sum_k p_k^2 \qquad \text{split score} = \frac{n_L}{n} G_L + \frac{n_R}{n} G_R \ \ (\text{lower is better}) \]

A box holds 6 late trips and 4 on-time ones. What is its Gini impurity?

\(1 - (0.6^2 + 0.4^2) = 1 - (0.36 + 0.16) = 0.48\). A pure box has 0; a 50/50 box has 0.5, the worst for two classes.

Trees are why tables are friendly to them: no need to scale columns (a cut at "< 31 minutes" works at any scale), categories become cuts, missing values can get their own branch, and the model explains itself: you can print the questions.

A tree asks the one yes/no question that sorts the dots best, then asks again inside each side.

Why Gini, and why a split never makes it worse on training data
  1. Pick two dots from a box at random (the same dot may come up twice). Both are class \(k\) with chance \(p_k^2\), so they match with chance \(\sum_k p_k^2\). That's why \(G = 1 - \sum_k p_k^2\) is "the chance they differ".
  2. A split sends a share \(n_L / n\) of the dots left and the rest right. The parent's share of late trips is the weighted average of the two children's shares.
  3. \(p^2\) is convex (calculus 5): the average of the squares is at least the square of the average. So the children's weighted \(\sum_k p_k^2\) is at least the parent's.
  4. So the weighted Gini after any split is at most the parent's: it can stay the same, never rise. The 6 late, 4 on-time box (\(G = 0.48\)) split into 5/1 and 1/3 gives \(0.6 \times 0.278 + 0.4 \times 0.375 \approx 0.32\).
  5. That's why a tree left alone keeps splitting until every box is pure and training accuracy is 100%: on training data another question always helps or does nothing. Only a depth limit or validation data stops it.

3. Gradient boosting: small trees that fix each other

One deep tree overfits; one shallow tree underfits. Boosting adds many shallow trees, each fit to what the previous ones still got wrong (the residuals), and adds only a fraction of each (the learning rate). Here it predicts a number (a noisy curve, as in chapter 11) with trees of depth 2.

With one tree, the prediction is three or four flat steps. Each new tree is fit to the gap between the steps and the dots, and the prediction creeps toward the curve. A small learning rate creeps slowly but generalizes better (many small corrections average out the noise); a learning rate of 1 lurches and overfits within a few dozen trees. In practice: a small rate, many trees, and stop where validation error is lowest (early stopping, the yellow curve's minimum).

The math: why it's called gradient boosting

With squared error \(L = \tfrac12 (y - F(x))^2\), the residual \(y - F(x)\) is exactly the negative gradient of the loss with respect to the prediction. So each round is chapter 2's gradient descent, except the step is a tree fit to the gradient:

\[ r_i = -\frac{\partial L}{\partial F(x_i)} = y_i - F_{m-1}(x_i), \qquad h_m = \text{tree fit to } (x_i, r_i), \qquad F_m = F_{m-1} + \eta\, h_m \]

For classification, swap in the log-loss from chapter 5 and the same recipe works. XGBoost, LightGBM and CatBoost are this algorithm with fast split finding, regularized leaves and many engineering tricks.

Validation error is lowest at 140 trees and rises after. What do you ship?

The number of trees is a knob chosen on validation data, like the polynomial degree. Libraries do it for you: give them a validation set and an early-stopping patience.

Boosting is gradient descent where each step is a small tree fitted to what's still wrong.

Where it breaks: what trees can't do

4. When to use what

DataStart withWhy
A table of mixed columns, thousands to millions of rowsGradient-boosted treesStrong out of the box, handles scale, categories and missing values, fast, explainable enough. Studies comparing deep nets and trees on many tabular datasets keep finding trees ahead or tied.
Few rows, need to explain every weight (credit, medicine)Logistic regressionEach coefficient is a readable effect; hard to overfit; regulators understand it.
Images, audio, text, videoNeural networks (stages 2 and 5)The structure (pixels near pixels, words in order) is what convolutions and attention exploit; trees can't.
A table plus text or imagesEmbeddings from a network as extra columns for boosted treesStage 6 shows how to turn text into vectors.

On tables, start with boosted trees; use a line when every weight must be explained, and networks when the data has structure like pixels or words.

Whatever you pick, chapter 11 and 12 still rule: a baseline first, splits that respect time, and the metric that matches the cost of a mistake.

All chapters: AI from scratch.

Last updated