Chapter 13 of 42 · ~17 min read & lab+60 XP
Chapter 13 · Machine Learning

Regression Metrics Explained: MSE, RMSE, MAE, MAPE & R-Squared (R²)

How to score continuous predictions honestly — when to pick RMSE vs. MAE vs. MAPE, why R² can be negative, and why Adjusted R² beats plain R²

What Is It?

Regression metrics (MSE, RMSE, MAE, MAPE, R², Adjusted R²) quantify how close a model's continuous numeric predictions ŷ are to true values y.

Why Is It Required?

Different business problems punish errors differently — RMSE heavily penalizes rare big misses, MAE treats every dollar of error equally, and plain R² falsely rises whenever you add useless random columns.

Everyday Analogy: Grading a delivery-time predictor: MAE tells you the average minutes late, RMSE flags catastrophic 2-hour delays, and R² tells you how much better you are than blindly guessing the average.
Why ML Engineers & Data Scientists Actually Need This

Picking the wrong regression metric optimizes the wrong business behavior — predicting the median (MAE) vs. predicting the mean (MSE) changes pricing and inventory decisions.

ML / AI Engineer

You must match your training loss (MSE vs. L1 vs. Huber) to your evaluation KPI (RMSE vs. MAE).

Data Scientist

Stakeholders understand '$14,000 average error (MAE)' or '7.5% error (MAPE)' immediately — whereas 'MSE = 196,000,000 dollars²' means nothing to a CEO.

Interview & Debug Edge

Guaranteed interview question: 'Can R² ever be negative? And why do we use Adjusted R² when comparing models with different feature counts?'

Step 0 · Test Your Gut Before Any Math!

You are predicting daily product demand, and many products have 0 sales on slow days. Which metric will crash with a division-by-zero explosion?

What You Will Master in This Chapter
  • Compute MSE, RMSE, MAE, MAPE, R², and Adjusted R² from scratch.
  • Choose RMSE when large outliers are catastrophic vs. MAE when every unit of error has equal cost.
  • Explain why R² can actually be NEGATIVE on a test set (worse than predicting the constant mean!).
  • Explain why MAPE explodes when true targets y approach zero.
Plain-English Symbol Decoder · Click Any Symbol Before We Begin
1 / 3
RMSE=1N∑(yi−y^i)2\text{RMSE} = \sqrt{\frac{1}{N}\sum (y_i - \hat{y}_i)^2}Say aloud:“Root Mean Squared Error”
Plain-English Meaning
Same units as y; heavily penalizes big single misses because errors are squared first.
Everyday Example
One $100 error hurts as much as one hundred $10 errors!
Story-First Intuition, Visual Diagrams & Interactive Walkthroughs

Every regression metric asks a slightly different question: MAE asks 'How far off am I on a typical row?', RMSE asks 'How bad are my worst misses?', MAPE asks 'How far off am I in percent?', and R2R^2 asks 'How much better am I than just predicting the average every time?'

1. Side-by-Side Metric Decision Guide

MetricUnitsOutlier SensitivityOptimal Constant GuessWhen to Pick
MSEy2y^2Very HighMean yˉ\bar{y}Training loss (smooth gradient)
RMSESame as yyHighMean yˉ\bar{y}Big misses are disproportionately costly
MAESame as yyLow (Robust)MedianEvery dollar/minute of error costs the same
MAPE%Medium (explodes at y=0y=0)—Strictly positive targets (y≫0y \gg 0) for exec reports
R2R^2 / Adj R2R^2UnitlessHigh—Scale-free comparison across datasets
Hands-On Mini-Lab · Drag 1 Mansion Outlier & Watch MSE Get Hijacked!

Four normal homes cost [$200k, $220k, $240k, $260k]. Drag the 5th home's price up to $2,000k and watch which loss function stays calm!

MSE Optimal (= Mean)
$284k
Pulled wildly upward!
MAE Optimal (= Median)
$240k
100% immune to outlier!
Huber Loss (Smooth)
$253k
Smooth + outlier-resistant!

2. Plain R² vs. Adjusted R²

Plain R2R^2 never decreases when you add more training features — even pure random numbers! Adjusted R2R^2 fixes this by penalizing every extra feature pp: Radj2=1−(1−R2)N−1N−p−1R^2_{\text{adj}} = 1 - (1 - R^2)\frac{N - 1}{N - p - 1} If a new column doesn't improve fit enough to earn its keep, Adjusted R2R^2 drops immediately.

“Wait, I'm Confused...” · Teacher Office Hours Q&A

The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.

Yes! On a validation/test set (or a bad model without an intercept), if your model's squared error is larger than simply predicting the constant mean yˉ\bar{y}, then R2=1−SSE/SST<0R^2 = 1 - \text{SSE}/\text{SST} < 0.

Interactive Visual Lab
Lab Challenge: Watch how squared error (MSE/RMSE) reacts to outliers compared to absolute error (MAE).
Interactive Animation • Linear Regression & Gradient Descent

Watch Gradient Descent Tilt & Slide the Line Down the MSE Bowl

Left: live fitted line & crimson residual errors. Right: real-time MSE descent curve!

MSE = 50.020
Fitted Line ŷ = wx + b & ResidualsMSE Loss vs. Gradient Descent Step
0%
Step 0/25
Live parameters: w = 0.20, b = 0.40 — notice how large vertical residual springs pull the line rapidly in early steps, then settle gently at the OLS optimum (w ≈ 1.88, b ≈ 0.99)!
Intuition Takeaway: Always inspect RMSE and MAE together (a big gap signals outlier errors), use Adjusted R² or out-of-sample R² instead of training R², and avoid MAPE whenever targets can equal zero.
Production NumPy & PyTorch Code Patterns
import numpy as np

# Compute MSE, RMSE, MAE, MAPE, R² and Adjusted R² from scratch
y_true = np.array([100.0, 200.0, 300.0, 400.0, 500.0])
y_pred = np.array([110.0, 190.0, 285.0, 420.0, 560.0])
N, p = len(y_true), 2

err = y_true - y_pred
mse = float(np.mean(err ** 2))
rmse = float(np.sqrt(mse))
mae = float(np.mean(np.abs(err)))
mape = float(np.mean(np.abs(err / y_true)) * 100.0)
r2 = float(1.0 - np.sum(err ** 2) / np.sum((y_true - y_true.mean()) ** 2))
adj_r2 = 1.0 - (1.0 - r2) * (N - 1) / (N - p - 1)

print(f"RMSE={rmse:.2f} | MAE={mae:.2f} | MAPE={mape:.1f}% | R2={r2:.4f} | Adj_R2={adj_r2:.4f}")
Common ML Interview Traps & Numerical Pitfalls
Trap #1: Reporting MAPE on retail demand data that has 0-sale days.
Best Practice: Use WAPE (sum(|y - ŷ|) / sum(|y|)) or MAE/RMSE when true targets can be zero.
Chapter Mastery Check (+60 XP)
Score: 0 / 1
1On a test set, your model has MAE = 10 and RMSE = 48. What does the huge gap between RMSE and MAE immediately tell you?

Apply What You Learned — Solve Matching Coding Problems