Sub topic 6
Measuring error: MSE, RMSE, MAE and R-squared
This chapter assumes you can fit a LinearRegression, split data with train_test_split, and read NumPy arrays. A fitted model needs a number that says how wrong its predictions are. Each metric below summarizes the gap between actual values y and predictions ŷ.
The four metrics
The error for one row is y - ŷ. The metrics combine these errors in different ways.
| Metric | Formula | Units | Notes |
|---|---|---|---|
| MSE | mean of (y − ŷ)² | target squared | Penalizes large errors heavily |
| RMSE | square root of MSE | same as target | Easy to read next to the target |
| MAE | mean of abs(y − ŷ) | same as target | Less sensitive to outliers |
| R² | 1 − SS_res / SS_tot | unitless | Compares the model to predicting the mean |
For R², SSres is the sum of squared errors of your model. SStot is the sum of squared errors of a model that always predicts the mean of y. An R² of 1 is a perfect fit. An R² of 0 matches the mean-only baseline. On held-out data, R² can be negative, which means the model does worse than that baseline.
Computing them in scikit-learn
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
rng = np.random.default_rng(0)
X = rng.uniform(0, 10, size=(200, 1))
y = 3.0 * X[:, 0] + 2.0 + rng.normal(0, 1.0, size=200)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0
)
model = LinearRegression()
model.fit(X_train, y_train)
pred = model.predict(X_test)
print("coef:", model.coef_, "intercept:", model.intercept_)
print("RMSE:", np.sqrt(mean_squared_error(y_test, pred)))
print("MAE:", mean_absolute_error(y_test, pred))
print("R2:", r2_score(y_test, pred))The noise in the data has a standard deviation of 1.0. So the RMSE should land near 1, and the MAE should be a bit lower, near 0.8. R² should be close to 1 because the line explains almost all the variance.
The argument order is metric(y_true, y_pred). R² is the one metric where swapping the arguments changes the result, so keep the order.
Choosing a metric
- Use RMSE when large misses are costly, because squaring weights them more.
- Use MAE when you want the typical miss in plain units, or when a few outliers would dominate RMSE.
- Use R² to compare models on the same target. It says nothing about error size in real units.
- RMSE is always at least as large as MAE. A big gap between them points to a few large errors.
A common mistake
A high R² does not prove the model is right. A curved relationship can still give a decent R² with a straight line, while the residuals show a clear pattern. Pair the metrics with a residual check, which a later chapter covers.
Exercise
A model has a test RMSE of 4.0 and a test MAE of 1.0. What does the large gap suggest, and which metric would you report to a stakeholder who cares most about rare big misses?
Show answer
The gap suggests a few large errors are inflating RMSE, since squaring weights them heavily while MAE treats all errors equally. For a stakeholder who cares about rare big misses, report RMSE, ideally alongside MAE so they see both the typical and the worst-case behavior.