Interactive Python · Machine learning

Regression Metrics, the game

MAE, MSE, RMSE, max error, R² and adjusted R²: drag a bad prediction around and watch every metric react. Then compute them all in a few lines of Python.

~12 minBeginner friendlyPython · scikit-learn5 missions275 XP
Mission 1+25 XP✓ Complete

Measuring errors

Every error metric starts from the same residuals (actual − predicted), then summarizes them differently:

MAE = (1/n) Σ |yᵢ − ŷᵢ|Mean absolute error: the average size of a miss, in the target’s units
MSE = (1/n) Σ (yᵢ − ŷᵢ)²Mean squared error: squares punish big misses much harder
RMSE = √MSERoot mean squared error: back in the target’s units

Max error is simply the single worst miss. For all four, lower is better and 0 is perfect.

🧠Quick check
+25 XP

Actual values are 3, 5, 2 and predictions are 2, 5, 4. What is the MAE?

Mission 2+80 XP✓ Complete

Outlier lab

Ten house prices (₹ lakh) and a model’s predictions. Drag the prediction for house 10 away from its true price of 70 and watch which metrics explode. Then change the number of features to see adjusted R² react.

These are example numbers chosen to show how the metrics behave.

🧪Outlier lab
+80 XP
–MAE
–MSE
–RMSE
–Max error
–R²
–Adjusted R²
actual pricepredictionerror
  • ✓Create a miss of 30 or more and compare MAE with RMSE+30 XP
  • ✓Make house 10’s prediction perfect+20 XP
  • ✓Raise the feature count until adjusted R² falls well below R²+30 XP
Mission 3+40 XP✓ Complete

R² and adjusted R²

R² = 1 − SSE / SSTSSE = Σ(y − ŷ)², SST = Σ(y − ȳ)²

R² is the share of the target’s variance the model explains: 1 is perfect, 0 is no better than predicting the mean, and it can even go negative on test data.

Adjusted R² = 1 − (1 − R²)(n − 1) / (n − k − 1)n = rows, k = features

Adjusted R² only rises if a new feature improves the model more than chance would.

⚡Pick the metric
1 / 4
correct on the first try
Mission 4+25 XP✓ Complete

Code it in Python

The article’s workflow on the California housing data (the text says “Boston”, but the code loads California housing).

Step 1Train a model

import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error, max_error, r2_score

X, y = fetch_california_housing(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
y_pred = LinearRegression().fit(X_train, y_train).predict(X_test)

Step 2Compute every metric

mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
max_err = max_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

n, p = X_test.shape
adj_r2 = 1 - (1 - r2) * (n - 1) / (n - p - 1)

print(f"MAE {mae:.2f} | MSE {mse:.2f} | RMSE {rmse:.2f} | Max {max_err:.2f} | R² {r2:.2f} | Adj R² {adj_r2:.2f}")
🧠Quick check
+25 XP

scikit-learn has no built-in adjusted R². What do you need to compute it?

Final boss+105 XP✓ Defeated

Defeat the Metrics Boss 🐲

Seven questions, three hearts.

Question 1 / 7
❤️❤️❤️

💥 The boss winsReview the missions above, then try again.

You’re a Metrics Master!

Share on LinkedIn
What’s next

Keep levelling up

You learned what MAE, MSE, RMSE, max error, R² and adjusted R² measure, how outliers affect each one, and how to compute them all with scikit-learn.

Lv 1 Rookie0 / 275 XP