Skip to content
Road to Intelligence

Part I · Foundations

Chapter 3

Machine Learning

Learning a function from examples — and the art of not fooling yourself.

2 h 30 min core path15 concepts3 interactivesCore path · 5 optional concepts foldedDeep · all 15 concepts shown in full

In one sentenceMachine learning fits a function to data so that it generalizes to examples it has never seen, and most of the craft is about measuring whether it really does.

The idea

What does it mean to learn?

Chapter 1 ended with a turn: instead of writing rules, collect examples and let the machine find the pattern. This chapter makes that precise. Machine learning means choosing a family of functions with adjustable parameters, then using data to pick the parameters that make the function fit — in a way that keeps working on examples it hasn't seen.

Everything depends on where the training signal comes from. With labelled examples, you learn a mapping from inputs to answers (supervised). Without labels, you look for structure (unsupervised). And with only raw data, you can invent labels from the data itself — hide a word and predict it — which is self-supervised learning, the way every LLM is pretrained.

For supervised learning the data is a table you'll recognize from data engineering: rows are examples, columns are features, one column is the label. If the label is a number it's regression; if it's a category, classification.

How it works

Your first models: lines and curves

Every model in this site — up to GPT — follows the same three-part recipe you met in Chapter 2: a model with parameters, a loss that scores it, and an optimizer that lowers the loss. The simplest instance is linear regression: predict a number as a weighted sum of the features, and choose the weights that minimize the squared error.

Swap the output for a probability — pass the weighted sum through a sigmoid — and swap squared error for cross-entropy, and you have logistic regression, the basic classifier. Train one below and watch the boundary settle; then try the XOR data and watch it fail.

Try it · toy model

Logistic Regression Lab

Train a linear classifier with gradient descent and watch its decision boundary settle — then see it fail on XOR.

Implement8 min

The central problem

The central problem: generalization

Here is the most important idea in machine learning. A model can always fit its training data better by becoming more flexible — but fitting the training data isn't the goal. Predicting new data is. Move the slider and watch the two errors part ways.

Try it · toy model

Overfitting Lab

Fit curves of increasing complexity to 12 noisy points. Training error keeps falling; error on new data falls, then soars. Then add regularization.

Know well8 min

At degree 11, the curve passes exactly through all twelve training points — zero training error — and is wildly wrong everywhere else. That is overfitting: memorizing noise instead of learning the pattern. At degree 0 the model is too simple to capture anything — underfitting. The job is to find the middle, and the only honest way to find it is to measure on data the model never trained on.

When you can't simplify the model, you can penalize complexity instead. Turn on regularization in the lab at degree 11 and the test error drops back near the best simple model.

Failure modes

How ML goes wrong

Two failures account for a large share of models that look great in evaluation and disappoint in production — and both are, at heart, data pipeline problems.

Leakage: the training data contains information that won't exist at prediction time — a feature computed from the future, a preprocessing step fitted on the test set, the same customer in both splits. The model learns the leak, not the task.

Distribution shift: the world the model is deployed into differs from the one it was trained on. A test-set score is a promise only about data like the test set.

Measurement

Measuring a classifier

Accuracy is the obvious metric and often the wrong one. When one class is rare — fraud, disease, spam — a model that always says "no" scores high accuracy while being useless. The fix is to count which kind of mistakes the model makes, and to choose where on the precision–recall trade-off you want to be.

Try it · toy model

Precision, Recall and the Threshold

A fraud detector on imbalanced data: move the threshold and watch precision, recall, the confusion matrix and the ROC curve respond.

Know well8 min

The toolbox

The classical toolbox

You don't need to memorize algorithms — but you should recognize the main families and what each is good for. Each is a different answer to the same question: what shape of function should the model be allowed to learn?

The limit

From hand-crafted features to learned ones

Every model in this chapter learns only the last step: from features to answer. Someone still has to design the features — and for images, audio and text that was years of expert work per domain, the knowledge-acquisition bottleneck from Chapter 1, one level down. The XOR example in the logistic regression lab is this problem in miniature: a linear model fails until someone invents the right feature.

The next step is to learn the features too. That is what neural networks do.

Why it matters

Why it matters

Everything later is this chapter at scale. An LLM is a model with parameters, trained by gradient descent on a loss (cross-entropy) over data, and evaluated on held-out benchmarks. Overfitting, leakage (called contamination for LLMs), distribution shift and misleading metrics are exactly the failure modes you'll meet in Chapter 16.

For your day job, much of this is directly usable. On tabular data — the kind data engineers own — well-featured linear models and tree ensembles remain strong, fast and explainable. Knowing when that's enough is as valuable as knowing how a Transformer works.

Concepts in this chapter

Mark each one as you go. Must-know concepts are the core path.

What do I actually need to remember?

  • ML = a model with parameters + a loss + an optimizer, fitted to data.
  • Supervised learns from labels; unsupervised finds structure; self-supervised makes labels from the data (how LLMs are pretrained).
  • Linear regression: weighted sum + squared error. Logistic regression: weighted sum → sigmoid + cross-entropy.
  • The goal is generalization: performance on data the model has never seen.
  • More flexibility always lowers training error; test error falls then rises (underfit → overfit).
  • Train to fit, validate to choose, test once to report.
  • Regularization (e.g. L2 / weight decay) trades a little fit for better generalization.
  • Leakage and distribution shift make offline scores lie — both are usually data-pipeline problems.
  • On imbalanced data, accuracy misleads; use precision, recall and the right baseline.
  • Classical ML learns only the last step; features were hand-crafted — the bottleneck deep learning removes.

You do not need to memorize everything else. This list is the revision sheet.

Key papers

Essential

A few useful things to know about machine learning

Pedro Domingos · 2012 · Communications of the ACM

A short, practical essay on the lessons ML practitioners learn the hard way: generalization is what counts, data beats cleverness, and intuition fails in high dimensions.

Problem
The folk knowledge that separates successful ML projects from failed ones was rarely written down.
What was new
Twelve compact lessons — overfitting, the curse of dimensionality, feature engineering, more data vs smarter algorithms, and more.

How to read it: The best single reading for Chapter 3. Read it after the chapter; much of it will click.

~30 min readdoi:10.1145/2347736.2347755✓ verified 2026-09-26
Important

Leakage in data mining

Shachar Kaufman, Saharon Rosset et al. · 2012 · ACM Transactions on Knowledge Discovery from Data

Named and systematized data leakage — information in training data that won't exist at prediction time — one of the most common ways ML results turn out to be fake.

Problem
Models that looked excellent in evaluation failed in deployment because their training data contained hints about the answer.
What was new
A formal definition of leakage, a catalogue of real examples, and methods to detect and avoid it.
~45 min readdoi:10.1145/2382577.2382579✓ verified 2026-09-26
Important

Support-vector networks

Corinna Cortes, Vladimir Vapnik · 1995 · Machine Learning

Introduced the soft-margin support vector machine, the dominant classifier of the late 1990s and 2000s.

Problem
How do you pick, among all boundaries that separate the data, the one most likely to generalize — and handle data that isn't perfectly separable?
What was new
Maximize the margin to the nearest points, allow some violations with a penalty, and use kernels to separate data in high-dimensional feature spaces.
~1 h readdoi:10.1007/BF00994018✓ verified 2026-09-26
Important

Random Forests

Leo Breiman · 2001 · Machine Learning

Random forests — many decision trees trained on random subsets of data and features, then averaged — remain one of the strongest methods for tabular data.

Problem
Single decision trees overfit easily and are unstable: small data changes produce very different trees.
What was new
Average many decorrelated trees, each grown on a bootstrap sample with a random subset of features at each split.
~50 min readdoi:10.1023/A:1010933404324✓ verified 2026-09-26
Optional

Regression Shrinkage and Selection Via the Lasso

Robert Tibshirani · 1996 · Journal of the Royal Statistical Society, Series B

Introduced the lasso (L1 regularization), which shrinks weights and sets many exactly to zero — regularization and feature selection at once.

Problem
Least-squares regression overfits with many features and produces hard-to-interpret models.
What was new
Penalize the sum of absolute weight values; the geometry of this penalty drives many weights to exactly zero.
~50 min readdoi:10.1111/j.2517-6161.1996.tb02080.x✓ verified 2026-09-26
Optional

On lines and planes of closest fit to systems of points in space

Karl Pearson · 1901 · Philosophical Magazine

The origin of principal component analysis: find the directions along which data varies most.

Problem
How can many correlated measurements be summarized by a few underlying directions?
What was new
Fit the line or plane that minimizes perpendicular distances to the points — what we now call the first principal components.
~30 min readdoi:10.1080/14786440109462720✓ verified 2026-09-26
Optional

Least squares quantization in PCM

S. Lloyd · 1982 · IEEE Transactions on Information Theory

The paper behind 'Lloyd's algorithm', the standard iterative procedure for k-means clustering (circulated at Bell Labs in 1957, published 1982).

Problem
How should a continuous signal be quantized into a few levels with the least squared error?
What was new
Alternate between assigning points to their nearest centre and moving each centre to the mean of its points.
~40 min readdoi:10.1109/TIT.1982.1056489✓ verified 2026-09-26

Watch

27 min

StatQuest with Josh Starmer

Linear Regression, Clearly Explained!!!

Least squares, R², and what a fitted line actually tells you — from the ground up.

Covers: Fitting a line, residuals, R², p-values.

Should know
9 min

StatQuest with Josh Starmer

StatQuest: Logistic Regression

A short, clear picture of how logistic regression turns a line into probabilities.

Covers: The S-shaped curve, classification with probabilities, maximum likelihood.

Must know
20 min

StatQuest with Josh Starmer

Regularization Part 1: Ridge (L2) Regression

Regularization made concrete: accept a little bias to reduce a lot of variance.

Covers: Ridge penalty, λ, bias–variance, fitting with few data points.

Should know
16 min

StatQuest with Josh Starmer

ROC and AUC, Clearly Explained!

How to evaluate a classifier across every possible threshold.

Covers: Confusion matrices, true/false positive rates, ROC curves, AUC.

Should know

What came next?

Chapter 4

Neural Networks →

Hand-designed features limit what a model can learn. Can a model learn the features too?