Skip to main content
Module 1 · Lesson 4 of 44/56 in the course~22 min
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

StageCore questionMain risk if skipped
1. Problem & dataWhat decision changes and which metric matches it?Solves the wrong problem cleanly
2. Hold-out splitDid we lock away test rows before inspecting patterns?Test leakage and fake optimism
3. Train & baselineDoes the model beat a trivial majority rule?Shipping complexity for zero gain
4. Evaluate & monitorDoes 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:

Python
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 2

Your 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:

Python
y_true = np.array([1, 0, 1, 1])
y_pred = np.array([1, 0, 0, 1])
print(float((y_true == y_pred).mean()))   # 0.75

Third, compute the majority-class baseline before celebrating any score:

Python
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.9

Fourth, measure the generalisation gap between training and held-out test accuracy:

Python
train_acc = 0.98
test_acc = 0.74
print("overfitting gap:", round(train_acc - test_acc, 2))  # 0.24

Practice

Practice Stage

Warmup — Implement accuracy

Exercise#machine-learning.m1.l4.e1
Attempts: 0Loading…

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.

Loading editor…
Show hint

Compare with ==, then take the mean of the resulting booleans.

Solution available after 3 attempts

Practice Stage

Practice — Split data before evaluation

Exercise#machine-learning.m1.l4.e2
Attempts: 0Loading…

Split X and y into train (first 80 rows) and test (remaining rows) slices: store X_train, X_test, y_train, y_test.

Loading editor…
Show hint

Slice both X and y using [:80] and [80:].

Solution available after 3 attempts

Practice Stage

Challenge — Beat the majority-class baseline

Exercise#machine-learning.m1.l4.e3
Attempts: 0Loading…

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.

Loading editor…
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

Quiz#machine-learning.m1.l4.e4

Why do we keep a test set that the model never sees during training?

Answer options
Quiz#machine-learning.m1.l4.e5

Put the following in the usual order: train the model, split the data, evaluate on the test set, define the problem.

Answer options
Quiz#machine-learning.m1.l4.e6

A model is 98% accurate on a dataset where 98% of the cases are in one class. What should you conclude?

Answer options