Module lessons (4/4)
The machine learning workflow
The hook: it is a loop, not a line
Beginners picture machine learning as "pick an algorithm, press train". In practice the algorithm is a small part. Most of the work is framing the problem, getting good data, and checking honestly whether the result is any good, usually several times over.
Project stage reference
| Stage | Core question | Main risk if skipped |
|---|---|---|
| 1. Problem & data | What decision changes and which metric matches it? | Solves the wrong problem cleanly |
| 2. Hold-out split | Did we lock away test rows before inspecting patterns? | Test leakage and fake optimism |
| 3. Train & baseline | Does the model beat a trivial majority rule? | Shipping complexity for zero gain |
| 4. Evaluate & monitor | Does live unseen performance hold up over time? | Silent accuracy decay under drift |
Walk through the workflow
Why the test set is held out
If you grade a student on the exact questions they practised, a high score proves little: they may have memorised the answers. The same is true for a model. The score that matters is the one on new data, so we keep a part of the data hidden until the end.
First, hold out the last 20% of rows deterministically before looking at metrics:
import numpy as np
n = 10
split_idx = int(0.8 * n)
train_idx = np.arange(0, split_idx)
test_idx = np.arange(split_idx, n)
print(len(train_idx), len(test_idx)) # 8 2Your first metric: accuracy and baseline comparison
For classification, the simplest metric is accuracy: the fraction of predictions that are correct.
accuracy = correct predictions / total predictions
Second, with numpy, comparing two arrays gives booleans, and their mean is accuracy:
y_true = np.array([1, 0, 1, 1])
y_pred = np.array([1, 0, 0, 1])
print(float((y_true == y_pred).mean())) # 0.75Third, compute the majority-class baseline before celebrating any score:
y_imbalanced = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
majority = int(y_imbalanced.mean() >= 0.5)
baseline_acc = float((y_imbalanced == majority).mean())
print("majority baseline accuracy:", baseline_acc) # 0.9Fourth, measure the generalisation gap between training and held-out test accuracy:
train_acc = 0.98
test_acc = 0.74
print("overfitting gap:", round(train_acc - test_acc, 2)) # 0.24Practice
Warmup — Implement accuracy
Write a function accuracy(y_true, y_pred) that returns the fraction of positions where the two arrays are equal. Then store the accuracy of the example arrays in score.
Show hint
Compare with ==, then take the mean of the resulting booleans.
Solution available after 3 attempts
Practice — Split data before evaluation
Split X and y into train (first 80 rows) and test (remaining rows) slices: store X_train, X_test, y_train, y_test.
Show hint
Slice both X and y using [:80] and [80:].
Solution available after 3 attempts
Challenge — Beat the majority-class baseline
Given imbalanced labels y_test and model predictions y_pred, compute baseline_acc (predicting 0 everywhere) and model_acc, then store beats_baseline as True if model_acc > baseline_acc.
Show hint
Compare both zero predictions and y_pred against y_test using float((... == y_test).mean()).
Solution available after 3 attempts
Check your understanding
Why do we keep a test set that the model never sees during training?
Put the following in the usual order: train the model, split the data, evaluate on the test set, define the problem.
A model is 98% accurate on a dataset where 98% of the cases are in one class. What should you conclude?