Machine Learning · Chapter 28 of 40
Regression Metrics
For regression: MAE (mean absolute error), MSE (mean squared error), RMSE (√MSE), R² (coefficient of determination).
MAE is robust to outliers; MSE/RMSE penalise big errors more.
Example 1 (python)
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import math
print(mean_absolute_error(y_test, y_pred))
print(math.sqrt(mean_squared_error(y_test, y_pred)))
print(r2_score(y_test, y_pred))
Output
3.2
4.5
0.83
Three common metrics.
Example 2 (python)
# R^2 = 1 - SS_res/SS_tot. 1.0 = perfect, 0 = as good as mean.
R² intuition.
Key points
- MAE: average absolute error.
- RMSE: penalises big errors more.
- R²: fraction of variance explained.
- Choose based on error cost.
💡 Note: Report metrics in the target's original units (rupees, kg) — much easier for stakeholders to interpret.
Understanding Regression Metrics
Regression metrics summarize differences between numerical predictions and observed targets. For observation i, define the residual as e_i = y_i − ŷ_i, where y_i is the observed value and ŷ_i is the prediction. Positive residuals indicate underprediction. Averaging signed residuals can hide mistakes because positive and negative values cancel; absolute values and squares prevent that cancellation. Here, assume one target, equally weighted observations, and finite values.
Mean absolute error is MAE = Σ|e_i| / n. It measures average error magnitude in the target’s units. An MAE of 3 kilograms means the absolute errors average 3 kilograms, not that every prediction is within 3 kilograms. Its penalty grows linearly: doubling one error doubles that observation’s contribution. MAE is less sensitive to extreme residuals than squared-error metrics, but it is not immune to them.
Mean squared error is MSE = Σe_i² / n. Doubling an error quadruples its contribution, so large mistakes receive disproportionately high weight. MSE has squared target units. Root mean squared error, RMSE = √MSE, restores the original units without removing this sensitivity. On the same observations with identical weights, MSE and RMSE rank models identically because square root is increasing. RMSE is at least MAE; equality occurs when all absolute residuals match.
These penalties also influence learning algorithms. Squared loss has a derivative proportional to the residual, giving larger errors stronger influence during optimization. Absolute loss has constant slope magnitude away from zero and a corner at zero. For an unrestricted constant prediction, squared loss is minimized by the target mean, whereas absolute loss is minimized by a median. Changing the evaluation metric does not automatically change an estimator’s training objective.
R² = 1 − Σe_i² / Σ(y_i − ȳ)² compares squared error with variation around the evaluated targets’ mean. With nonconstant targets, 1 means perfect predictions, 0 matches that mean-based reference, and negative values are worse. R² is dimensionless, not percentage accuracy or evidence of causation. Different target variability can produce different R² values for the same RMSE. Constant targets make the formula undefined; software may substitute finite values, so inspect this case explicitly.
Practical walkthrough
Begin by defining the prediction task and error cost. If each additional unit of error carries roughly equal cost, MAE is a natural primary metric. If occasional large misses are especially undesirable, consider RMSE. Both penalize overprediction and underprediction symmetrically; neither directly represents a situation where underestimating demand costs more than overestimating it.
Next, separate training, validation, and final test data before learning transformations. Fit imputers, scalers, feature selection, and the estimator using training data only. During cross-validation, fit those steps separately within each training fold, preferably using a pipeline. Use validation results to choose settings, then evaluate the selected procedure on untouched test data. For forecasting, preserve time order; for repeated observations of people or devices, consider group-aware splitting.
Create predictions and verify that each prediction matches the correct target row. Check lengths, missing values, and units. If targets were transformed, invert predictions before reporting original-unit MAE or RMSE. MSE remains in squared units. Compare candidates on identical evaluation observations, and include a baseline whose constant prediction was learned from training targets. The evaluation-set mean inside R² is a mathematical reference, not a deployable baseline learned without test information.
Finally, report the primary metric alongside a complementary metric and the evaluation size. Inspect residuals by target range and relevant groups: one average can hide systematic underprediction. Scikit-learn’s direct metric functions return ordinary errors, while scoring names such as neg_mean_absolute_error return negated errors so larger scores are better. Convert these back before presenting them as MAE. Small test sets warrant cautious conclusions.
Worked example
from math import sqrt
# Fixed illustrative predictions, not fitted or tuned on these targets.
y_true = [2.0, 4.0, 6.0, 8.0]
predictions = {
'A': [2.0, 4.0, 6.0, 2.0],
'B': [0.0, 2.0, 4.0, 6.0],
}
mean_y = sum(y_true) / len(y_true)
sst = sum((y - mean_y) ** 2 for y in y_true)
for name, y_pred in predictions.items():
assert len(y_true) == len(y_pred)
errors = [y - p for y, p in zip(y_true, y_pred)]
mae = sum(abs(e) for e in errors) / len(errors)
sse = sum(e ** 2 for e in errors)
mse = sse / len(errors)
rmse = sqrt(mse)
r2 = 1 - sse / sst # Targets here are nonconstant.
print(f'{name}: MAE={mae:.2f}, MSE={mse:.2f}, '
f'RMSE={rmse:.2f}, R2={r2:.2f}')
Expected output
A: MAE=1.50, MSE=9.00, RMSE=3.00, R2=-0.80
B: MAE=2.00, MSE=4.00, RMSE=2.00, R2=0.20
Model A has residuals 0, 0, 0, and 6; model B has four residuals of 2. A therefore wins on MAE: its absolute errors total 6 rather than 8. Squaring reverses the preference: A’s squared errors total 36, while B’s total 16. The target mean is 5, and squared deviations around it total 20, giving R² values of 1 − 36/20 and 1 − 16/20. A’s negative R² is valid despite three perfect predictions. This example isolates metric arithmetic using Python’s standard library; four fixed observations cannot establish either model’s expected performance on future data.
Common mistakes and how to avoid them
Treating MAE as unaffected by outliers.
Describe it as less sensitive than squared error. Investigate extreme residuals for data problems or important rare cases rather than deleting them merely to improve a score.
Calling R² = 0.80 “80% accurate.”
Say that squared error is 20% of the evaluation-mean reference error, assuming nonconstant targets. Add original-unit error metrics to communicate prediction magnitude.
Selecting models repeatedly against the final test set.
Make model and metric decisions with validation data or cross-validation. Reserve final test results for evaluation, with all learned preprocessing excluded from test fitting.
Comparing errors measured on different target scales.
Evaluate candidates on the same observations and scale. A log-scale RMSE and an original-unit RMSE measure different quantities and cannot be compared directly.
Review questions
Why can MAE and RMSE prefer different models?
MAE weights error magnitude linearly, whereas RMSE aggregates squared errors before taking a square root. A model with many tiny errors and one large miss can win on MAE but lose on RMSE.
If kilograms become grams, what happens to the metrics?
Multiplying targets and predictions by 1,000 multiplies MAE and RMSE by 1,000 and MSE by 1,000,000. R² stays unchanged because its numerator and denominator scale equally.
Does negative R² indicate a calculation failure?
Not necessarily. For nonconstant evaluated targets, it means prediction squared error exceeds the error from always predicting their mean. Check the predictions and baseline, but do not clip the result to zero.
Why distinguish training loss from evaluation metric?
Training loss determines what the fitting algorithm optimizes. An evaluation metric judges the resulting predictions. Reporting MAE for a squared-loss estimator does not make that estimator optimize absolute error.