Sub topic 11
Related metrics: median absolute error and MAPE
This chapter assumes you can compute MAE, RMSE, and R squared, and that you know why RMSE reacts more strongly to large errors than MAE does. It adds two related metrics. Median absolute error resists outliers. MAPE reports error as a percentage of the true value.
Median absolute error
MAE averages the absolute residuals. Median absolute error takes the middle value of the absolute residuals instead. It answers the question: "What is the typical miss for a single prediction?"
The median ignores how large the biggest errors are. A single prediction that is off by 1,000 moves MAE a lot. It barely moves the median.
import numpy as np
from sklearn.metrics import mean_absolute_error, median_absolute_error
y_true = np.array([10.0, 12.0, 11.0, 13.0, 12.0])
y_pred = np.array([10.5, 11.5, 11.0, 13.5, 32.0]) # last prediction is far off
print("MAE :", mean_absolute_error(y_true, y_pred))
print("MedAE :", median_absolute_error(y_true, y_pred))The absolute errors are 0.5, 0.5, 0.0, 0.5, and 20.0. MAE is 4.3 and median absolute error is 0.5. The two numbers tell different stories. MedAE says most predictions are within half a unit. MAE says the average miss is large because of one bad prediction.
The cost of using the median is that it hides those big errors. If a rare large miss is expensive in your application, a metric that ignores it can give a false sense of safety.
MAPE
Mean absolute percentage error divides each absolute error by the true value, then averages the results:
MAPE = mean(|ytrue - ypred| / |y_true|)
scikit-learn returns a fraction, so 0.10 means 10%. Multiply by 100 to display a percentage.
import numpy as np
from sklearn.metrics import mean_absolute_percentage_error
y_true = np.array([100.0, 200.0, 400.0])
y_pred = np.array([110.0, 180.0, 400.0])
mape = mean_absolute_percentage_error(y_true, y_pred)
print(f"MAPE = {mape:.4f} ({mape * 100:.1f}%)")The relative errors are 0.10, 0.10, and 0.00. Their mean is 0.0667, or 6.7%.
MAPE has no units, so you can compare it across targets measured in different scales, such as sales in dollars and sales in units. Many stakeholders also find "off by 7%" easier to read than "off by 12.3 dollars".
Where MAPE breaks
MAPE has several known problems.
- Zero or near-zero true values. Dividing by zero is undefined. scikit-learn avoids a crash by substituting a tiny epsilon, which produces an enormous number. Values close to zero also inflate the metric.
- Asymmetry. Over-predicting has no upper bound on the percentage error, but under-predicting cannot exceed 100% for positive targets. A model that is trained to minimize MAPE tends to under-predict.
- Small targets dominate. An error of 1 on a true value of 2 counts as 50%. The same error on a true value of 200 counts as 0.5%.
The next example shows the near-zero problem.
import numpy as np
from sklearn.metrics import mean_absolute_percentage_error
y_true = np.array([0.1, 50.0, 60.0, 70.0])
y_pred = np.array([1.1, 51.0, 61.0, 71.0]) # every prediction is off by exactly 1.0
print(mean_absolute_error := np.mean(np.abs(y_true - y_pred))) # 1.0
print(mean_absolute_percentage_error(y_true, y_pred)) # about 2.5Every prediction misses by exactly 1.0, so MAE is 1.0. The first target is 0.1, so its relative error is 10, or 1000%. That single point pushes MAPE to about 2.5, which reads as 250%.
Choosing between them
| Metric | Units | Outlier effect | Use when |
|---|---|---|---|
| MAE | Target units | Linear | You want the average miss in plain units |
| RMSE | Target units | Strong | Large misses cost much more than small ones |
| Median absolute error | Target units | Very little | You want the typical miss and expect outliers |
| MAPE | Percent | Weights small targets heavily | Targets are strictly positive and well away from zero |
Exercise
A demand model predicts daily units sold for products. Some products sell 0 units on many days. A colleague proposes MAPE as the main metric. What goes wrong, and which metric would you report instead?
Show answer
When the true value is 0, the percentage error is undefined, and scikit-learn replaces the zero with a tiny epsilon, giving an enormous value. Days with very small sales also produce huge percentages. MAPE would be dominated by these low-volume points. Report MAE (in units sold), optionally with median absolute error to show the typical miss, and RMSE if large misses are especially costly.
Exercise
A model has MAE 4.3 and median absolute error 0.5 on the same test set. What does the gap tell you, and what would you check next?
Show answer
Most predictions are very close, but a few have large errors that raise the mean. Check the residuals for outliers: sort the absolute errors and inspect the largest ones. Decide whether they are data problems or real cases the model handles poorly, and whether those cases are costly enough that RMSE should also be reported.