Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Machine Learning Foundations — Alpha School

Lesson 6: Measuring Error: Loss Functions and R-Squared

A model is only as good as its errors are small — but small compared to what? Saying a house-price model is off by 20,000 in whatever currency you use sounds bad, but it means something very different for a small flat than for a mansion. This lesson is about measuring error properly, so that you can compare models fairly, spot when a model is barely better than guessing, and explain to someone else exactly how good your model is.

The simplest error measure is the mean absolute error, or MAE: take the size of each mistake, ignoring whether it was too high or too low, and average them. If a model's predictions are off by 5, 10 and 3, the MAE is 6. It is easy to explain — on average, the model misses by this much — and it treats every error equally. The mean squared error, or MSE, instead squares each error before averaging, which punishes a few big mistakes much more than many small ones. Its square root, the RMSE, brings the number back into the original units, so it can be compared directly with the thing being predicted. Which one you choose depends on what you care about: if one huge mistake is a disaster, use MSE or RMSE; if you want a plain average miss, use MAE.

Better than guessing? R-squared

Error measures alone still leave a question open: is the model actually useful? A model that predicts house prices with an average error of 20,000 might be excellent if prices vary wildly, or terrible if all houses cost almost the same. R-squared answers that question by comparing the model with the simplest possible guess: predicting the average price for every house. R-squared is the fraction of the variation in the real values that the model explains. A value of 0.85 means the model explains 85% of the ups and downs in the data; a value near 0 means it is barely better than guessing the average; a negative value means it is worse than guessing the average.

R-squared is useful, but it has a trap. Adding more features to a model can never make R-squared go down on the training data, even if the new features are useless — the model simply finds some tiny way to use them. That means a model with twenty features can look better than a model with two, while actually being worse on new data. This is one reason professionals check error on held-out test data, not just on the data the model was trained on, and why simpler models are often preferred when their test scores are close.

Gradient descent, at last

Choosing between error measures

Which error measure should you actually use? The answer depends on what a mistake costs in your problem. Imagine a worked example: a delivery company predicts arrival times. If it is off by five minutes, customers barely notice; if it is off by two hours, a customer misses a delivery entirely. The company should use RMSE, because squaring punishes that two-hour disaster far more than a handful of five-minute misses, and the model it chooses will be the one that avoids big errors. A different problem — a charity estimating the cost of food parcels — might prefer MAE, because every pound of error matters equally and no single error is a catastrophe. R-squared then adds the second question: is the model actually better than guessing the average? A model can have a small MAE and still be useless if the thing being predicted barely varies at all. The professional habit is to report two numbers, not one: an error in real units, and R-squared against the baseline.

Earlier you met gradient descent as walking downhill in fog. Now you can see what the hill is: it is the loss function itself. For a line with a slope and an intercept, the loss — the total squared error — is a surface with a single lowest point, like a bowl. Gradient descent starts with a random slope and intercept, works out which small change would reduce the loss fastest, takes a step in that direction, and repeats. The size of each step is called the learning rate. Too small, and training crawls; too large, and the model leaps over the bottom of the bowl and may never settle. Getting the learning rate right is one of the first practical skills in machine learning, and it matters for the neural networks you will meet later just as much as it matters here.

Key Terms

Mean absolute error (MAE)
The average size of a model's errors, ignoring whether each was too high or too low.
Mean squared error (MSE)
The average of the squared errors; it punishes a few large mistakes much more than many small ones.
Root mean squared error (RMSE)
The square root of the MSE, which puts the error back into the original units of what is being predicted.
R-squared
The fraction of the variation in the real values that a model explains, compared with simply guessing the average every time.
Loss function
A single number measuring how wrong a model's guesses are, which training tries to make smaller.
Learning rate
How big a step gradient descent takes each time it adjusts the model's settings.
Baseline
A deliberately simple prediction, such as always guessing the average, used to check whether a model is actually useful.

Discussion Questions

  1. A weather model has an MAE of 2 degrees. Is that good? What extra information would you need before deciding?
  2. Why can adding useless features make R-squared look better while the model gets worse?
  3. If the learning rate is too large, training may never settle. Can you think of a real-life analogy for taking steps that are too big?

Practice Questions

  1. What is the MAE of errors of 4, 6 and 2?
  2. When would you prefer RMSE over MAE?
  3. What does an R-squared of 0.85 mean?
  4. What does a negative R-squared mean?
  5. What is the learning rate in gradient descent?
Show answers
  1. 4. Add the sizes (4 + 6 + 2 = 12) and divide by 3, which gives 4.
  2. When a few large errors matter much more than many small ones. Squaring punishes big mistakes heavily, so RMSE is the right choice when one huge miss is a serious problem.
  3. The model explains 85% of the variation in the real values. It is measured against the baseline of always guessing the average, so 0.85 means the model captures most of the ups and downs.
  4. The model is worse than simply guessing the average every time. If even the simplest baseline beats it, the model has learned nothing useful.
  5. How big a step the model takes when adjusting its settings. Too small and training is very slow; too large and it may leap over the best settings and never settle.
Keep reading to complete this lesson