Sub topic 2
MAE: the average size of the error
This chapter assumes you can read basic NumPy code, know what a mean is, and know that a regression model predicts a number. It also assumes you have seen residuals (prediction minus true value) from the previous chapter. Here you will compute Mean Absolute Error (MAE) and learn how to read it.
What MAE measures
Mean Absolute Error is the average distance between each prediction and its true value, ignoring direction. For n examples:
MAE = (1/n) * sum(|y_pred_i - y_true_i|)
The steps are:
- Compute each residual.
- Take its absolute value, which discards the sign.
- Average the results.
Why the absolute value matters
Residuals can be positive (the model overshot) or negative (it undershot). If you averaged them directly, overshoots and undershoots would cancel. A model that is wrong by 100 in both directions would look perfect.
import numpy as np
errors = np.array([-2.0, 2.0, -1.0, 1.0])
print(np.mean(errors)) # 0.0, the signs cancel
print(np.mean(np.abs(errors))) # 1.5, the typical size of a missThe signed average has its own use. It is called mean error, or bias, and it tells you whether the model leans high or low. It says nothing about how large the misses are. MAE answers the size question.
A worked example
Use the four-point example from the previous chapter.
| True | Predicted | Residual | Absolute error |
|---|---|---|---|
| 3.0 | 2.5 | -0.5 | 0.5 |
| 5.0 | 5.0 | 0.0 | 0.0 |
| 2.5 | 4.0 | 1.5 | 1.5 |
| 7.0 | 8.0 | 1.0 | 1.0 |
The absolute errors sum to 3.0. Dividing by 4 gives an MAE of 0.75. The same calculation in NumPy:
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
mae = np.mean(np.abs(residuals))
print(mae) # 0.75Swapping the order of subtraction (y_true - y_pred) flips every residual's sign. The absolute values and the MAE stay the same.
Reading the number
MAE is in the same units as the target. If you predict house prices in dollars and get an MAE of 12,000, your predictions miss by about $12,000 on average. If you predict temperature in degrees Celsius, the MAE is in degrees Celsius. This is the main reason people like MAE: you can explain it to a non-technical stakeholder without any translation.
Three properties help you interpret it:
- Lower is better. An MAE of 0 means every prediction was exact.
- Errors count in proportion to their size. A miss of 4 counts exactly twice as much as a miss of 2. The next chapters contrast this with squared-error metrics.
- It has no fixed scale. An MAE of 0.75 is excellent if the target ranges over thousands and poor if it ranges from 0 to 1. Always judge MAE against the spread of the target or against a baseline.
Common mistakes
- Reporting MAE on training data. A model can look excellent on data it has already seen. Compute MAE on held-out data, which a later chapter covers.
- Comparing MAE across different targets. An MAE of 5 for price in dollars and an MAE of 5 for age in years are not comparable.
- Forgetting that MAE is "typical", not "worst". It does not bound the largest error. Check the maximum absolute error separately if worst cases matter.
- Mixing up the argument order in libraries. For MAE the order does not change the result. For some other metrics it does, so get in the habit of writing
(y_true, y_pred).
Where MAE fits
MAE is the simplest of the three metrics in this tutorial. It is easy to explain and treats every unit of error equally. The next chapter squares the errors instead, which changes how large misses are weighted.
Exercise
A model predicts ypred = [12, 18, 33] for ytrue = [10, 20, 30]. Compute the MAE by hand, then say what it means in plain language if the target is delivery time in minutes.
Show answer
The residuals are 2, -2, and 3. The absolute errors are 2, 2, and 3, which sum to 7. Dividing by 3 gives an MAE of 7/3, about 2.33. On average, the predicted delivery time is off by about 2.33 minutes, in either direction.
Exercise
Why does averaging the raw residuals fail as a measure of prediction quality, and what does that average tell you instead?
Show answer
Positive and negative residuals cancel, so a model with large misses in both directions can average close to 0. The signed average is the mean error, or bias. It shows whether predictions lean too high or too low, not how large the errors are.