Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 3: Classical Machine LearningCore Concepts

The supervised learning setup

P3.A145 min●○○ BeginnerReviewed Dec 2, 2026

Why this matters for machine learning

Almost every practical ML system, from a spam filter to a house-price estimator to the next-word step of a language model, is the same recipe: examples with answers, a model with adjustable numbers, a loss that scores mistakes, and a search for numbers that make the loss small on data the model has not seen.

  • Fitting a line by minimizing the average squared error is the simplest instance of the whole setup, and the first algorithm that follows it. (P3.A2: Linear regression)
  • Holding out data to estimate how the model behaves on new examples is how every model in this course is judged. (P3.A4: Train, validation and test sets)
  • A language model is trained with the same loop: for each context the label is the next token, and the loss is cross-entropy. (P10.B2: The pretraining objective and loss curves, coming soon)
  • A deployed model is only useful while new data looks like the training data, which is why monitoring is part of the setup. (P12.5: Monitoring and drift, coming soon)

Before this: How a machine "learns", Kinds of learning, Scalars, vectors and spaces, Matrices as data and as transformations, Expectation and variance, Joint, marginal and conditional probability, Sampling and Monte Carlo, What is optimization?

After this lesson you can

  • Name the parts of a supervised problem (example, features, label, data set, model, parameters, loss) and write the shapes of the matrix X and the vector y
  • Tell regression from classification by the kind of label, and say why a classifier is trained with a smooth loss instead of the error rate
  • Compute the loss on one example and the average loss (empirical risk) on a small data set, and say how it differs from the true risk
  • Explain why a low training loss is not the goal, what the generalization gap is, and what the no-free-lunch idea does and does not say
  • Put the stages of a pipeline in order, and say what can go wrong at each

A spam filter, a house-price estimator, a tool that reads handwritten digits and the next-word step of a chatbot look like different things. Underneath they follow one template: collect examples where the right answer is known, build a model that has adjustable numbers, score its mistakes with a loss, and search for numbers that make the loss small. Then comes the part that matters: the model must work on examples it has never seen.

This lesson sets that template out with names and small numbers, so that every later lesson in Part 3 (and the neural networks after it) has a place to hang its ideas. You met the pieces in P0.2 (the learning loop) and P0.3 (kinds of learning). Here they are made exact.

One example: an input and an answer

Take one house. We describe it by a few measured numbers, such as the area, the number of bedrooms and the age. Those numbers are the features, and the list of them is the feature vector x\mathbf{x} (a vector with dd entries, P1.B1). The thing we want to predict, the price, is the label yy. Some books say target or response, and an input with its label is an example (or sample, or instance).

Here are five houses. The prices (in thousands) are invented.

area (m2\text{m}^2)bedroomsage (years)price (k)
120315310
85240190
15045400
60130120
95310240

The first example is x1=(120,3,15)\mathbf{x}_1=(120,3,15) with label y1=310y_1=310. Here d=3d=3. A subscript ii names the example, and in the feature vector xijx_{ij} is feature jj of example ii.

Features must be numbers, because a model does arithmetic on them. Text, colours and categories are first turned into numbers (P3.A11 covers how; P2.5 did it for the Titanic passengers).

Two kinds of label

The kind of label sorts the problem, as P0.3 said:

  • Regression: the label is a real number, and being close counts. A price, a temperature, a delivery time.
  • Classification: the label is one of a fixed set of categories, and a wrong category is simply wrong. For two categories (binary) the labels are often written 0 and 1. For KK categories (multi-class) they are written 0,1,…,K−10,1,\dots,K-1. These numbers are only names: class 2 is not “twice” class 1, so a model must not do arithmetic on them as if it were a quantity.

A label can also be a vector (predict price and rent together), or a long sequence such as a sentence. A language model predicts one token at a time, so each step is a classification over the vocabulary (P10.B2).

The data set: a table, written two ways

A data set of NN labelled examples is

D={(x1,y1),(x2,y2),…,(xN,yN)}.D=\{(\mathbf{x}_1,y_1),(\mathbf{x}_2,y_2),\dots,(\mathbf{x}_N,y_N)\}.

In code it is stored as two arrays. Stack the feature vectors as rows of a feature matrix X\mathbf{X} and the labels in a vector y\mathbf{y} (the row-per-example convention of P1.B5):

X=(12031585240150456013095310)\mathbf{X}=\begin{pmatrix}120&3&15\\85&2&40\\150&4&5\\60&1&30\\95&3&10\end{pmatrix} y=(310190400120240)\mathbf{y}=\begin{pmatrix}310\\190\\400\\120\\240\end{pmatrix}

Here X\mathbf{X} has shape (5,3)(5,3), which is N×dN\times d, and y\mathbf{y} has shape (5,)(5,). The same shapes appear in P2.5. There, the Titanic table of 1309 passengers ended as an X\mathbf{X} of shape (1309,7)(1309,7) and a y\mathbf{y} of shape (1309,)(1309,) with yi=1y_i=1 for a survivor (500 of the 1309) and yi=0y_i=0 otherwise. That is a classification problem with N=1309N=1309 and d=7d=7.

The assumption behind the table: i.i.d.

We assume the rows and the future are the same kind of thing: every pair (xi,yi)(\mathbf{x}_i,y_i) is drawn independently from one fixed but unknown distribution p(x,y)p(\mathbf{x},y). This is independent and identically distributed, or i.i.d. (P1.D5). Two details matter.

  • The distribution is over pairs. Two identical houses can sell for different prices, so yy is drawn from p(y∣x)p(y\mid\mathbf{x}), and part of its variation is noise that no model can predict from these features.
  • The assumption is a modelling choice, often only approximately true. It fails when examples depend on each other (many rows from one patient, days in a time series), with duplicates, and when the world changes after training. Then a test score can look better than the real performance (P3.A4, P12.5).

The model and the hypothesis class

A model is a function fθf_{\boldsymbol\theta} that turns features into a prediction y^=fθ(x)\hat y=f_{\boldsymbol\theta}(\mathbf{x}). The vector θ\boldsymbol\theta holds its parameters, the adjustable numbers. For a line on one feature, fθ(x)=wx+bf_{\boldsymbol\theta}(x)=wx+b and θ=(w,b)\boldsymbol\theta=(w,b). For the house data a linear model is y^=w1x1+w2x2+w3x3+b\hat y=w_1x_1+w_2x_2+w_3x_3+b, so θ\boldsymbol\theta has d+1=4d+1=4 numbers.

The hypothesis class H\mathcal{H} is the set of all functions you can get by changing the parameters: H={fθ:θ∈Θ}\mathcal{H}=\{f_{\boldsymbol\theta}:\boldsymbol\theta\in\Theta\}. For “a line” it is every line. Choosing the class is the first big decision, and it limits what can be learned: if the true relation is a curve, the best line is still a line. Training can only pick the best member of H\mathcal{H}, not something outside it.

Parameters are learned from data. Hyperparameters are the settings you choose before training, such as the learning rate (P0.2), the degree of a polynomial or the number of layers; they are not learned by gradient descent on the training loss, and they are chosen with validation data (P3.A4). For a classifier, fθf_{\boldsymbol\theta} usually outputs a score or a probability, and a rule turns it into a label (P3.A7).

The loss for one example

To train, we need a number that says how bad one prediction is. The loss L(y^,y)L(\hat y,y) is zero for a perfect prediction and larger for worse ones. Common choices:

  • Squared error for regression, L=(y^−y)2L=(\hat y-y)^2. It never cancels between over- and under-predictions and punishes big misses more. Absolute error ∣y^−y∣|\hat y-y| punishes them less.
  • 0-1 loss for classification: L=0L=0 if the predicted class is right and 11 if it is wrong. Its average is the error rate.
  • Cross-entropy, which scores the probability the model gave to the correct class (P3.A7).

The 0-1 loss is what we would like to be small, but it is a poor thing to train with. If the model outputs a smooth score, a tiny change in the parameters either changes no prediction at all (so the loss does not move and the slope is zero) or flips one prediction (a sudden jump). Gradient descent (P1.F3) cannot follow that. Smooth losses such as cross-entropy are used for training, and the error rate is used for reporting (P3.A9). So a loss is a tool for the search, and a metric is the number you finally care about; they can differ.

The average loss, or the empirical risk

The loss scores one example. To score a model on the whole data set, average it:

L(θ)=1N∑i=1NL(fθ(xi), yi).\mathcal{L}(\boldsymbol\theta)=\frac1N\sum_{i=1}^{N}L\big(f_{\boldsymbol\theta}(\mathbf{x}_i),\,y_i\big).

This is called the empirical risk or the training loss (“empirical” means “measured on the data we have”). Here is a worked example from P0.2: the data (1,2),(2,3),(3,7)(1,2),(2,3),(3,7) and the line y^=wx+b\hat y=wx+b with w=1w=1, b=0b=0.

xxyyy^\hat yerror y^−y\hat y-ysquared
121−1-11
232−1-11
373−4-416

The empirical risk is (1+1+16)/3=6(1+1+16)/3=6. The best line for these points is w=2.5w=2.5, b=−1b=-1, with empirical risk 0.50.5 (P3.A2 shows how to find it). The loss on a single example is a number for that example; the empirical risk is one number for the model.

The true risk, and why it cannot be computed

What we want is the average loss on future examples from the same distribution, an expectation (P1.D4):

R(θ)=E(x,y)∼p[L(fθ(x),y)],R(\boldsymbol\theta)=\mathbb{E}_{(\mathbf{x},y)\sim p}\big[L\big(f_{\boldsymbol\theta}(\mathbf{x}),y\big)\big],

the true risk or expected risk. We do not know pp, so we cannot compute it. We can estimate it by averaging the loss over a sample, which is what Monte Carlo does (P1.D9).

There is one catch, and it is the heart of this lesson. A Monte Carlo average is an unbiased estimate only when the sample is independent of the thing measured. The training examples were used to choose θ\boldsymbol\theta, so the model was tuned to them, and the empirical risk is usually smaller than the true risk, sometimes much smaller. A fair estimate needs examples the model has not seen: a test set (P3.A4).

Training, and inference

Training (or fitting) means choosing the parameters that minimize the empirical risk:

θ^=arg⁡min⁡θ L(θ).\hat{\boldsymbol\theta}=\arg\min_{\boldsymbol\theta}\ \mathcal{L}(\boldsymbol\theta).

This is an optimization problem in the sense of P1.F1: the parameters are the unknowns, the empirical risk is the objective, and θ^\hat{\boldsymbol\theta} is the argmin. The method is called empirical risk minimization (ERM). For a line with squared error there is a formula for θ^\hat{\boldsymbol\theta}; for most models we search step by step with gradient descent (P1.F3, P0.2). A regularized version adds a penalty on the parameters and is the subject of P3.A6.

Inference (or prediction) is what happens after training. The parameters stay fixed, a new input x\mathbf{x} arrives with no label, and the model returns y^=fθ^(x)\hat y=f_{\hat{\boldsymbol\theta}}(\mathbf{x}). With the line above, x=4\mathbf{x}=4 gives 2.5×4−1=92.5\times4-1=9. Training is usually the slow, occasional step; inference is the step that runs every time the model is used.

Generalization: the training loss is not the goal

A model generalizes if it does well on new examples, not only on the ones it was trained on. The difference

generalization gap=R(θ^)−L(θ^)\text{generalization gap}=R(\hat{\boldsymbol\theta})-\mathcal{L}(\hat{\boldsymbol\theta})

(true risk minus training loss) tells how much the training loss flatters the model. Training only sees the training loss, never RR.

A model that memorizes shows why. Consider a “lookup” model that predicts, for a new input, the label of the nearest training example. On the training set it is perfect: every training input is its own nearest neighbour, so its training loss is exactly 00. On new data it copies the noise in the neighbour’s label. In a simulation where y=2x+1y=2x+1 plus noise of variance 11, with N=10N=10 training points, averaged over 200 repeats (code below):

  • the line has training loss 0.780.78 and true risk 1.271.27;
  • the lookup model has training loss 0.000.00 and true risk 2.462.46.

The model that is best on the training set is worse in the world. Noise variance 11 is the best any model could do, so a true risk close to 11 is as good as it gets. Two failures are named from this: underfitting (the hypothesis class is too simple, so even the training loss is high) and overfitting (the model fits quirks of the sample, so training loss is low and true risk high). P3.A3 shows them on one data set, P3.A5 explains the trade-off, P3.A4 gives the way to measure it, and P3.A6 gives one way to control it.

Go deeper A number for the optimism of the training loss

For a line (2 parameters) fitted by least squares to NN points with independent noise of variance σ2\sigma^2, when the true relation really is a line, the average training loss is σ2(1−2/N)\sigma^2(1-2/N), while the average loss on new labels at the same inputs is σ2(1+2/N)\sigma^2(1+2/N). With σ2=1\sigma^2=1 and N=10N=10 these are 0.80.8 and 1.21.2. A simulation of 20000 repeats gives 0.800.80 and 1.201.20. In general, with pp parameters the factors are 1∓p/N1\mp p/N: the more parameters per example, the bigger the optimism. These are exact only for this fixed-input, correctly specified linear case; the lesson from them (more freedom, more flattery) carries over in general.

Go deeper Why a gap can be bounded: a simple guarantee

Suppose the loss is between 0 and 1, the class H\mathcal{H} is a finite list of ∣H∣|\mathcal{H}| functions chosen before seeing the data, and the NN examples are i.i.d. Then, with probability at least 1−δ1-\delta, every hh in the list has ∣R(h)−L(h)∣≤ln⁡(2∣H∣/δ)/(2N)|R(h)-\mathcal{L}(h)|\le\sqrt{\ln(2|\mathcal{H}|/\delta)/(2N)} (Hoeffding’s inequality plus a union bound). For ∣H∣=1000|\mathcal{H}|=1000, δ=0.05\delta=0.05 and N=10000N=10000 the right side is 0.0230.023. The bound grows with the logarithm of the number of candidates and shrinks like 1/N1/\sqrt N: more candidates, or fewer examples, allow a larger gap. Real model classes are infinite and the bound is usually loose, so treat it as an explanation of the direction, not a prediction of the size.

No free lunch: why a model needs assumptions

Can we build one learner that is best for every problem? The no-free-lunch result says no, in a precise sense. Take a small example: inputs are the 8 patterns of three bits, you have seen the labels of 4 of them, and you must predict the other 4. Suppose each of the 24=162^4=16 ways to label the unseen inputs is equally likely. Whatever rule you choose for the unseen inputs, “always 0”, “always 1”, or anything clever, its average accuracy over those 16 labellings is exactly 50%50\%. The data you saw say nothing about the rest unless you assume that unseen inputs are related to seen ones.

Wolpert (1996) proved a general version for classification-style (0-1) error: averaged uniformly over all possible target functions, with error measured on inputs outside the training set, every learning algorithm has the same expected error. The careful reading is:

  • It does not say all algorithms are equally good on real problems. Real labels are not uniformly random: nearby inputs tend to have similar labels, patterns repeat, effects are usually simple. An algorithm that bets on such structure does better on real problems.
  • It does say that every learner carries a bet. The hypothesis class, a preference for smooth or simple functions, a distance measure, all are assumptions about the world, called the inductive bias. A bet that fits the problem helps; one that does not hurts. That is why there are many algorithms in Part 3, and why you compare them on your data (P3.A4) instead of trusting a favourite.

The pipeline around the model

A model is one step of a longer process. The usual order has seven stages, and the loop back is part of the work, because the evaluation or the world may send you back.

The supervised learning pipeline

Pick a stage to see what happens in it.

1Collectgather (x, y) pairs2Splithide some data3Featuresturn rows into vectors4Trainminimize training loss5Evaluateestimate the true risk6Deploypredict on new inputs7Monitorwatch for changeiteratenew data 1Collect2Split3Features4Train5Evaluate6Deploy7Monitoriterate: better features or modelnew data, retrain

1. Collect

Gather examples: inputs x together with their labels y, from records, sensors, surveys or human labellers. Decide first what x and y are and which loss and metric fit the goal.

Output
A raw table of examples.
House prices (regression)
Sale records: size, bedrooms and age of each house, and the price it sold for.
Titanic survival (classification)
A passenger list with a column that says who survived.
Common trap
Examples that do not look like the future inputs (the model will be used where the data was not collected), or noisy labels.
Where this course goes deeper
P12.2 (labels) and P12.1 (the whole project)
The supervised learning pipeline: collect, split, features, train, evaluate, deploy, monitor. Dashed pink arrows are the loops: evaluation sends you back to Features (and then Train), and monitoring brings new data into the next round. On a phone the stages stack vertically. Tap a stage to read what happens in it, its output, an example for house prices and for Titanic survival, a common trap, and where the course goes deeper.

Notice the order: the split comes before the features. A step such as “fill a missing age with the median” learns a number from data; if it is computed using the test rows, information from the test set has leaked into training (P3.A11). So the held-out data are put aside first, and every fitted step is learned on training rows only (P2.5 showed this for the median age).

Intuition

Think of a student with a pile of past exam papers and their answers. The student can learn the method or memorize the answers. Their score on the past papers is the training loss. The real exam is a fresh draw from the same examiner: that is the true risk. A student who memorized scores perfectly on the pile and badly on the exam, and the only honest way to tell is a practice paper the student has not seen (the test set). The analogy breaks in one place: a model has no “understanding” to fall back on. What it learns is whatever fits the examples within its hypothesis class.

Math

Data D={(xi,yi)}i=1ND=\{(\mathbf{x}_i,y_i)\}_{i=1}^{N}, (xi,yi)∼i.i.d.p(\mathbf{x}_i,y_i)\overset{\text{i.i.d.}}{\sim}p, X∈RN×d\mathbf{X}\in\mathbb{R}^{N\times d}, y∈RN\mathbf{y}\in\mathbb{R}^{N} for regression (class labels for classification).

Model y^=fθ(x)\hat y=f_{\boldsymbol\theta}(\mathbf{x}) with fθ∈Hf_{\boldsymbol\theta}\in\mathcal{H}; loss L(y^,y)L(\hat y,y).

Empirical risk L(θ)=1N∑iL(fθ(xi),yi)\mathcal{L}(\boldsymbol\theta)=\frac1N\sum_iL(f_{\boldsymbol\theta}(\mathbf{x}_i),y_i); true risk R(θ)=Ep[L(fθ(x),y)]R(\boldsymbol\theta)=\mathbb{E}_{p}[L(f_{\boldsymbol\theta}(\mathbf{x}),y)].

Training θ^=arg⁡min⁡θL(θ)\hat{\boldsymbol\theta}=\arg\min_{\boldsymbol\theta}\mathcal{L}(\boldsymbol\theta); gap R(θ^)−L(θ^)R(\hat{\boldsymbol\theta})-\mathcal{L}(\hat{\boldsymbol\theta}).

For a fixed θ\boldsymbol\theta chosen without looking at the sample, E[L(θ)]=R(θ)\mathbb{E}[\mathcal{L}(\boldsymbol\theta)]=R(\boldsymbol\theta). For θ^\hat{\boldsymbol\theta} chosen on the sample this equality fails, and L(θ^)\mathcal{L}(\hat{\boldsymbol\theta}) is typically below R(θ^)R(\hat{\boldsymbol\theta}).

Code

The same recipe in scikit-learn. This block is not runnable here, because scikit-learn is not available in the browser; the output shown is from a real run on the machine used for this lesson (scikit-learn 1.8). The Try it cells below use only NumPy.

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

rng = np.random.default_rng(0)
X = rng.uniform(0, 5, size=(100, 1))          # N=100, d=1
y = 2 * X[:, 0] + 1 + rng.normal(0, 1, 100)   # noisy labels

X_tr, X_te, y_tr, y_te = train_test_split(
    X, y, test_size=0.2, random_state=0)      # split
model = LinearRegression().fit(X_tr, y_tr)    # train
print(X_tr.shape, X_te.shape)
print("w =", model.coef_.round(3),
      " b =", round(float(model.intercept_), 3))
print("training loss:",
      round(mean_squared_error(y_tr, model.predict(X_tr)), 3))
print("test loss    :",
      round(mean_squared_error(y_te, model.predict(X_te)), 3))
print("prediction at x = 4:", model.predict([[4.0]]).round(3))
(80, 1) (20, 1)
w = [1.967]  b = 1.071
training loss: 1.021
test loss    : 0.636
prediction at x = 4: [8.937]

Here the test loss happens to be lower than the training loss. That is possible: 20 test points are a small sample with its own noise. One test number is an estimate with error (P3.A4).

Try it

Cell 1: the worked example in code. The model, the empirical risk and the best line for the three points. It prints 6.06.0 for w=1,b=0w=1,b=0, then the best line w=2.50w=2.50, b=−1.00b=-1.00 with loss 0.500.50, a constant-only model with loss 4.674.67, and the prediction 9.09.0 at x=4x=4. A richer hypothesis class gives a lower training loss.

The setup in NumPy

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Cell 2: training loss against true risk. Here we know the world, so we can draw a huge fresh sample (20000 points) to stand in for the true risk (Monte Carlo, P1.D9). Change n to 40 or the noise to 2, and watch the gap.

Training loss versus true risk

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

It prints about 0.780.78 and 1.271.27 for the line, and 0.000.00 and 2.462.46 for the lookup model.

Cell 3: no free lunch, by counting. All 16 ways to label the four unseen inputs, against four different rules.

No free lunch on 4 unseen inputs

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

All four print 0.500.50. The 50% holds only because every labelling was allowed. A learner that does better than that on a real problem is relying on an assumption about how seen and unseen inputs are related.

Check your understanding

  1. A data set has N=200N=200 emails, each described by d=50d=50 numeric features, with a label of 1 for spam and 0 otherwise. What are the shapes of X\mathbf{X} and y\mathbf{y}?

  2. After training, the loss on the training set is 0.00.0 and the loss on a held-out test set is 4.24.2. What is the best reading?

  3. Why is the error rate (the average 0-1 loss) a poor thing to minimize directly with gradient descent, for a model that outputs a smooth score?

  4. A hospital model is trained on 5 photographs from each of 100 patients. The 500 photographs are split into train and test sets at random, photo by photo. Which problem does this create?

  5. Which statement about the no-free-lunch idea is correct?

Exercises

  1. For the points (0,1),(1,5),(2,5)(0,1),(1,5),(2,5) and the model y^=2x+1\hat y=2x+1, find the mean squared error and the mean absolute error.
    Show answerThe predictions are 1,3,51,3,5 and the errors 0,−2,00,-2,0. Mean squared error is 4/3≈1.334/3\approx1.33 and mean absolute error is 2/3≈0.672/3\approx0.67. One miss of size 2 counts four times in the squared error but only twice in the absolute error.
  2. A classifier predicts (1,0,0,1,1,0)(1,0,0,1,1,0) for six emails whose labels are (1,0,1,1,0,0)(1,0,1,1,0,0). What is the 0-1 loss for each email and the empirical risk?
    Show answerThe losses are 0,0,1,0,1,00,0,1,0,1,0, and the average is 2/6≈0.332/6\approx0.33: an error rate of one third.
  3. How many numbers are in the Titanic feature matrix X\mathbf{X} of (1309,7)(1309,7), and how many parameters does a linear model with a bias have on it?
    Show answer1309×7=91631309\times7=9163 numbers. A linear model has one weight per feature plus a bias: d+1=8d+1=8 parameters, far fewer than 9163.
  4. For the data (1,2),(2,3),(3,7)(1,2),(2,3),(3,7), the hypothesis class “constant functions” y^=b\hat y=b has the best member b=4b=4. What is its empirical risk, and why is it above the 0.50.5 of the best line?
    Show answerThe errors are −2,−1,3-2,-1,3, and the average of the squares is (4+1+9)/3=14/3≈4.67(4+1+9)/3=14/3\approx4.67. Constants are a subset of lines (a line with w=0w=0), so the best line cannot be worse. Here it is much better because yy clearly rises with xx.
  5. A bank has 10000 past loans with their features and whether each was repaid. It wants to predict repayment for new applicants. Name x\mathbf{x}, yy, the kind of problem, and a suitable training loss.
    Show answerx\mathbf{x}: the applicant’s features (income, amount, history, and so on); yy: repaid or not (0 or 1). It is binary classification, trained with cross-entropy (P3.A7). Reporting would use a metric that fits the costs (P3.A9).
  6. A house-price model was trained on 2022 sales and is accurate on a 2022 test set, but is badly off in 2026. Which assumption failed, and which pipeline stage should have caught it?
    Show answerThe data were no longer identically distributed (prices and buyers changed). Monitoring should have caught it, by tracking the error as true prices arrived, and the answer is to collect new data and retrain (P12.5).
  7. Four inputs have labels you have not seen, and every one of the 16 label patterns is equally likely. A rule predicts (1,1,0,0)(1,1,0,0). What is its expected accuracy?
    Show answerEach input is right with probability 1/21/2 whatever the rule says, so the expected accuracy is 0.50.5, the same as for any other rule.

Next: P3.A2 fits a line in full (the closed form and gradient descent), P3.A3 shows underfitting and overfitting on a polynomial, P3.A4 sets up train, validation and test sets properly, P3.A9 covers the metrics for evaluation, and P3.A11 returns to features and leakage.

Key takeaways

  • A supervised problem is a set of examples. Each example is a pair of an input (a feature vector of d numbers) and a label (a number for regression, a category for classification). N examples give a matrix X of shape N by d and a label vector y of length N.
  • A model is a function with adjustable parameters. The hypothesis class is the set of functions you can get by changing the parameters, for example all lines. The loss scores one prediction against its label; the average of the loss over the data is the empirical risk.
  • What we care about is the true risk: the average loss over all future examples from the same distribution. It cannot be computed, because the distribution is unknown. The empirical risk is an estimate of it, but only for examples the model was not fitted to.
  • Training means choosing the parameters with the smallest empirical risk. Inference means using the fitted model on a new input. Zero training loss is easy to reach by memorizing and says little about new data; the gap between the two is the generalization gap.
  • The data are assumed to be i.i.d.: independent draws from one unknown distribution. Time order, repeated measurements of the same person and a changing world all break this, and then test numbers can be too good.
  • No-free-lunch: if every possible labelling of unseen inputs is equally likely, no learning algorithm beats another on those inputs. Learning works in practice because real problems are not like that, and every model carries assumptions (an inductive bias).
  • A pipeline has stages: collect, split, features, train, evaluate, deploy and monitor. Each has its own typical mistake, and the loops back (iterate, retrain on new data) are part of the job.
Found a mistake? Tell us