Sub topic 1
Prediction errors: the raw material of every metric
Prerequisites
This chapter assumes you can:
- Read basic Python and NumPy array operations.
- Compute a mean and a square root.
- Describe what a regression model predicts: a numeric target such as a price or a temperature.
- Recognize the idea of training data and test data.
What a prediction error is
A regression model returns a number for each input. The true value for that input is also a number. The difference between the two is the prediction error, also called the residual.
This tutorial uses one convention throughout:
residual = y_pred - y_trueSome books define it as y_true - y_pred. The sign flips, but the metrics in this tutorial give the same results either way. They use absolute values or squares, which remove the sign.
The residual has three properties worth fixing in mind:
- Sign. A positive residual means the model predicted too high (overprediction). A negative residual means it predicted too low (underprediction).
- Size. The absolute value says how far off the model was for that one example.
- Units. The residual uses the same units as the target. If you predict house prices in dollars, residuals are in dollars. If you predict temperature in degrees Celsius, residuals are in degrees.
Every metric in this tutorial is a way to summarize a list of residuals as a single number.
A worked example
Take four examples. The true values are 3.0, 5.0, 2.5, and 7.0. The model predicts 2.5, 5.0, 4.0, and 8.0.
import numpy as np
y_true = np.array([3.0, 5.0, 2.5, 7.0])
y_pred = np.array([2.5, 5.0, 4.0, 8.0])
residuals = y_pred - y_true
print(residuals) # [-0.5 0. 1.5 1. ]| Example | y_true | y_pred | Residual (y_pred - y_true) | Reading |
|---|---|---|---|---|
| 1 | 3.0 | 2.5 | -0.5 | Too low by 0.5 |
| 2 | 5.0 | 5.0 | 0.0 | Exact |
| 3 | 2.5 | 4.0 | 1.5 | Too high by 1.5 |
| 4 | 7.0 | 8.0 | 1.0 | Too high by 1.0 |
Example 3 is the largest miss. Later chapters show how different metrics treat that single error.
Why you cannot just average the residuals
The obvious summary is the mean residual. Here it is:
import numpy as np
residuals = np.array([-0.5, 0.0, 1.5, 1.0])
print(residuals.mean()) # 0.5This number is called the bias of the predictions. It tells you whether the model leans high or low on average. It does not tell you how large the errors are, because positive and negative residuals cancel.
Consider a model that is wrong by +10 on one example and -10 on another. Its mean residual is 0, which looks perfect. Its predictions are poor.
import numpy as np
residuals = np.array([10.0, -10.0])
print(residuals.mean()) # 0.0To measure the size of the errors, you must remove the sign before averaging. There are two standard ways:
- Take the absolute value of each residual. This leads to Mean Absolute Error (MAE).
- Square each residual. This leads to Mean Squared Error (MSE) and Root Mean Squared Error (RMSE).
The following chapters cover both.
Common mistakes
- Mixing argument order. Scikit-learn metric functions take
y_truefirst, theny_pred. MAE and RMSE do not change if you swap them, but R squared does. - Ignoring units. A residual of 1.5 means little until you know the target's units. An error of 1.5 degrees Celsius and an error of 1.5 million dollars are very different.
- Judging from training data. Residuals computed on the data the model learned from are usually smaller than on new data. A later chapter covers held-out evaluation.
Exercise
A model predicts [10, 12, 9] for true values [11, 10, 9]. Using residual = ypred - ytrue, what are the residuals, and what is their mean? Why is the mean a poor measure of error size?
Show answer
The residuals are [-1, 2, 0]. Their mean is (-1 + 2 + 0) / 3, which is about 0.33. The mean is a poor measure of error size because negative and positive residuals cancel. Here the model misses two of the three examples, yet the mean is close to zero.
Exercise
Residuals from a model that predicts apartment rent in euros per month are [-50, 30, 20]. What are the units of these residuals, and what does the -50 mean?
Show answer
The units are euros per month, the same as the target. The -50 means the model predicted 50 euros per month less than the true rent, so it underpredicted for that apartment.