Maths for ML / Read an equation
Mean squared error
How far off is the model, on average? Take every error, square it, and average the squares.
The parts
Point at any part of the equation, or pick it from the list.
the loss · output · shape one number, never negative
One score for the whole dataset. Training tries to make it small.
divide by n · constant · shape one number (n is how many examples there are)
Turns a total into an average, so the loss doesn't grow just because you have more data.
sum over examples · index · shape i counts 1, 2, …, n
A for-loop over the dataset: work out the bracket for each example i and add them up.
the true value · data · shape one number per example
The answer from the data for example i.
the prediction · output · shape one number per example
What the model guessed for example i.
the square · operation · shape one number in, one out
Removes the sign, so misses above and below both count, and punishes big misses much more than small ones: a miss of 3 costs 9, a miss of 1 costs 1.
Read it with the seven questions
- What comes out? One number, L, the average squared miss. It can't be negative.
- List the parts. Data: every yᵢ. Predictions: every ŷᵢ, which come from the model's parameters. Constant: n. Index: i, running over the examples.
- Check the shapes. Each bracket is one number; squared, still one number; summed over n examples and divided by n, one number.
- Say what each function does. The square removes signs and stretches big errors; the sum collapses n numbers into one; 1/n averages.
- Ask what makes it bigger or smaller. Errors grow the loss quadratically. One miss of 10 adds 100 to the sum, as much as a hundred misses of 1.
- Push the inputs to extremes. Perfect predictions give L = 0. A single wild outlier can dominate the whole loss, which is why some models use absolute error instead.
- Write it as code. L = np.mean((y - y_hat) ** 2).
Change it and watch
The line is fixed at ŷ = x + 1. Drag a point (or focus it and use the arrow keys) and watch its square grow: the area of each square is that point's squared error, and L is their average.
Now write it in NumPy
import numpy as np
y = np.array([3.0, 5.0, 4.0, 7.0]) # true values
y_hat = np.array([2.5, 5.5, 4.0, 5.0]) # predictions
L = np.mean((y - y_hat) ** 2) # (0.25 + 0.25 + 0 + 4) / 4
print(L) # 1.125