CodeBlanks Logo Icon CodeBlanks

Error measurement

Regression error is measured with metrics like MSE and MAE.

MAE and MSE

To measure a regression model, predictions are compared with the true values:

  • MAE (mean absolute error): mean(|y - ŷ|), in the data's unit.
  • MSE (mean squared error): mean((y - ŷ)²), penalises large errors more.
  • RMSE: √MSE, back in the data's unit.
from sklearn.metrics import mean_absolute_error, mean_squared_error
y = [3, 5, 7]
p = [2, 5, 9]
print(mean_absolute_error(y, p))   # 1.0
print(mean_squared_error(y, p))    # 1.666...

The R² score

R² says how much of the variability in y the model explains: 1 is perfect, 0 is as good as "always predict the mean", negative is worse than that.

from sklearn.metrics import r2_score
print(r2_score([3, 5, 7], [3, 5, 7]))   # 1.0
print(r2_score([3, 5, 7], [5, 5, 5]))   # 0.0
print(m.score(X_test, y_test))          # for regression score = R²

R² is unit-free; MAE/RMSE answer "how many TL is it off on average?". Both should be reported together.

Practice Measure the model

Train the model and get predictions for X. Round MAE, MSE and R² with round(..., 3) and print them on separate lines.

Starter code

model.py
import numpy as np
from sklearn.linear_model import LinearRegression

X = np.array([[1], [2], [3], [4], [5]])
y = np.array([3.1, 4.9, 7.2, 8.8, 11.0])
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

# buraya yaz

Sample solution

model.py
import numpy as np
from sklearn.linear_model import LinearRegression

X = np.array([[1], [2], [3], [4], [5]])
y = np.array([3.1, 4.9, 7.2, 8.8, 11.0])
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

m = LinearRegression().fit(X, y)
p = m.predict(X)
print(round(mean_absolute_error(y, p), 3))
print(round(mean_squared_error(y, p), 3))
print(round(r2_score(y, p), 3))

Expected output

0.12
0.018
0.998

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If the truth is <code>[10, 20]</code> and the prediction <code>[12, 16]</code>, what is the MAE?

  • 3.0 Correct answer
  • 10.0
  • 2.0
  • 6.0

Explanation: |10-12| = 2, |20-16| = 4; the mean is (2 + 4) / 2 = 3.

2 Compute the mean absolute error.

Question
hata = mean______error(y_true, y_pred)
Answer
hata = mean_absolute_error(y_true, y_pred)

Explanation: mean_absolute_error gives MAE, mean_squared_error gives MSE.

3 Match the metrics.

Term Meaning
MAE Mean of absolute errors
MSE Mean of squared errors
RMSE Square root of MSE
R² Share of explained variance

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation