MACHINE LEARNING • LESSON 7

Evaluate a Regression Model

A model can make predictions, but predictions alone are not enough. We need to measure how close those predictions are to the actual values.

THE SIMPLEST DEFINITION

Model evaluation tells us how good or bad the predictions are.

We compare the model's predicted values with the actual values and use evaluation metrics to measure the error or overall performance.

01

Actual Value vs Predicted Value

Suppose our model predicts exam scores for students. We compare what the model predicted with what actually happened.

Actual Score Predicted Score
50 48
60 63
70 68
80 84

The predictions are not exactly equal to the actual values. That difference is the prediction error.

02

What Is Prediction Error?

Prediction error is the difference between the actual value and the predicted value.

Error = Actual Value − Predicted Value

For example:

Actual = 70 Predicted = 68 Error = 70 − 68 = 2

A smaller error generally means the prediction is closer to the actual value.

Smaller prediction errors generally mean better predictions.
03

Why Do We Need Evaluation Metrics?

Looking at one prediction is not enough. A model usually makes many predictions.

For example:

Actual Predicted
50 48
60 63
70 68
80 84
90 87

We need a single measurement that summarizes how well the model performed across all these predictions.

Evaluation metrics turn many prediction errors into useful numbers.
04

Mean Absolute Error — MAE

MAE stands for Mean Absolute Error.

It calculates the average size of the prediction errors, ignoring whether the errors are positive or negative.

MAE = Average of |Actual − Predicted|

Suppose our errors are:

2
3
2
4

First calculate the average:

MAE = (2 + 3 + 2 + 4) / 4 MAE = 2.75

This means the model's predictions are off by about 2.75 units on average.

MAE is easy to understand because it uses the same units as the target.
05

Mean Squared Error — MSE

MSE stands for Mean Squared Error.

Instead of simply taking the absolute error, MSE squares each error before calculating the average.

MSE = Average of (Actual − Predicted)²

Suppose the errors are:

2
3
2
4

Square them:

2² = 4
3² = 9
2² = 4
4² = 16
MSE = (4 + 9 + 4 + 16) / 4 MSE = 8.25

Because errors are squared, large errors receive much more weight.

MSE strongly penalizes large prediction errors.
06

R² Score

R² is called the R-squared score.

It gives us another way to understand how well the model explains the variation in the target values.

A simple way to think about it is:

R² tells us how much of the variation in the target is explained by the model, relative to a baseline that predicts the mean.

For example, an R² value of:

R² = 0.90 Strong explanatory performance

The model explains a large portion of the variation in this particular dataset.

R² = 0.30 Weaker explanatory performance

The model explains much less of the variation.

R² can also be negative when the model performs worse than the mean-prediction baseline.

07

MAE vs MSE vs R²

MAE Average absolute error

Lower is generally better. Easy to interpret.

MSE Average squared error

Lower is generally better. Large errors matter more.

Explained variation relative to mean baseline

Higher is generally better, but context matters.

08

Evaluate the Model With Python

Scikit-learn provides functions for calculating these metrics.

from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score

actual = [50, 60, 70, 80, 90]
predicted = [48, 63, 68, 84, 87]

mae = mean_absolute_error(actual, predicted)
mse = mean_squared_error(actual, predicted)
r2 = r2_score(actual, predicted)

print("MAE:", mae)
print("MSE:", mse)
print("R²:", r2)

The important idea is that we give the evaluation metric two things:

ACTUAL VALUES actual
+
PREDICTED VALUES predicted
METRIC Performance score
09

Evaluate a Trained Model

Usually, we don't manually create the predicted values. We get them from the trained model.

from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score

X = [[1], [2], [3], [4], [5]]
y = [45, 55, 65, 75, 85]

model = LinearRegression()

model.fit(X, y)

predicted = model.predict(X)

mae = mean_absolute_error(y, predicted)
mse = mean_squared_error(y, predicted)
r2 = r2_score(y, predicted)

print("MAE:", mae)
print("MSE:", mse)
print("R²:", r2)

Here the process is:

Training data
Train model
Make predictions
Compare actual vs predicted
Calculate metrics
10

A Very Important Mistake to Avoid

You should not judge a machine learning model only by evaluating it on the same data it used for training.

Why?

A model can perform very well on training data but perform poorly on new, unseen data.

That is why machine learning uses separate validation and test data.

TRAINING DATA Used to learn
TEST DATA Used to check generalization

We covered this idea earlier in Training, Validation, and Test Data.

REMEMBER THIS

Don't just ask, "Can the model predict?" Ask, "How accurate are those predictions?"

MAE measures the average absolute error, MSE gives more weight to large errors, and R² describes how much variation the model explains relative to a mean-prediction baseline. For evaluating real model performance, use unseen validation or test data rather than relying only on training data.

QUICK CHECK

Check Your Understanding

What does MAE measure? The average absolute size of prediction errors.
What does MSE do? It squares errors, making large errors more influential.
What does R² tell us? How much target variation the model explains relative to a mean-prediction baseline.
Which is generally better: lower MAE or higher MAE? Lower MAE.
LESSON 7 COMPLETE

Next: Lesson 8 — Classification

We have now completed the Linear Regression section: regression, the regression line, prediction, Python implementation, and model evaluation. Next we move from predicting numbers to predicting categories.