What machine learning is, and the workflow
Supervised, unsupervised and reinforcement learning, empirical risk, and why a test set must stay hidden.
Animated explanation
The idea
A traditional program turns rules and data into answers. Machine learning turns data and answers into rules: instead of writing "if the email mentions a lottery, it's spam", we show the computer thousands of emails labelled spam or not and let it find the rule.
Every method in this course has the same three ingredients: a model family (lines, trees, networks), a loss that says how wrong a model is, and an optimiser that searches the family for the model with low loss. What changes between supervised, unsupervised and reinforcement learning is what the data looks like and what "wrong" means.
Generalisation is the whole game
We never care about the training examples themselves; we already know their answers. We care about new inputs. So the true goal is low expected loss on the distribution the data comes from, while we can only measure the average loss on our sample (the empirical risk).
The gap between the two is the generalisation gap. Flexible models can drive training loss to zero and still be useless. That is why every honest evaluation uses data the model never saw during training.
Train, validation, test
- Training set: fits the model's parameters (weights).
- Validation set (or cross-validation): chooses among models and hyperparameters (degree, λ, depth, learning rate).
- Test set: touched once, at the end, to report performance.
Typical splits are 60/20/20 or 80/20 with cross-validation. Split before any exploration that could influence choices, stratify by class for classification, and split by time or by group (patient, user) when examples are not independent.
Leakage: the silent killer
Data leakage is information from the test set (or from the future) sneaking into training. Classic cases: scaling features using the mean of the whole dataset, selecting features using all labels, or random splits of time series. Leaky models look brilliant offline and fail in production. Fit every preprocessing step on the training split only, then apply it to validation and test.
The algorithm, step by step
- 1Frame the task: inputs, target, metric, and a simple baseline to beat.
- 2Shuffle (or split by time/group) and hold out a test set.
- 3Fit preprocessing (scaling, encoding) on the training split only.
- 4Train candidate models; choose using validation error or cross-validation.
- 5Refit the chosen model on train + validation; report the test score once.
The maths
Expected risk
Empirical risk minimisation
Standardisation (fitted on training data)
Strengths and weaknesses
Strengths
- One framework (model, loss, optimiser) covers the whole course
- Held-out evaluation gives an honest estimate of real performance
- Baselines show whether ML adds value at all
Weaknesses
- Needs representative data; the test set only reflects the data you have
- Easy to fool yourself with leakage or repeated test-set use
Reach for it when…
Check your understanding
Lambdian Lab
A leak-free split and a baseline
Write the plumbing every later lab relies on.
- train_test_split(X, y, test_size, seed): shuffle with np.random.default_rng(seed).permutation and return X_train, X_test, y_train, y_test. The test part has round(test_size · n) rows.
- Standardiser: fit(X) stores the training mean and standard deviation; transform(X) uses them.
- baseline_mse(y_train, y_test): the test MSE of always predicting the training mean. Every model must beat this.