In this lesson
A model needs a comparison.
Saying that a model has "an R² of 0.85" means almost nothing. The number only makes sense when you compare it with a simple guess and measure the error in the unit of what you want to predict: reais, days, kilos.
- Measure a baseline and compare the model with it
- Read the error metrics without fooling yourself
- Choose parameters without looking at the test set
What the model learns
The model receives two things. The table of inputs, called X: each row is a case, such as a property, and each column is a feature, such as the area. And the target, called y: the value you want to predict, such as the price. The fit method makes the model learn the relationship between the two. Then predict predicts new cases.
There are two kinds of number. The parameters, the model learns on its own. The hyperparameters, you choose beforehand, such as the depth of a tree. In a cake recipe, the proportions are learned and the oven temperature is your choice.
For this course, use the California Housing dataset, with prices of districts in California. The old Boston dataset left scikit-learn because of an ethical problem, explained on the scikit-learn page listed in the sources.
Baseline: the zero on the ruler
Before any model, measure the simplest guess: always predicting the average of the training prices. DummyRegressor does that.
from sklearn.dummy import DummyRegressor
base = DummyRegressor(strategy="mean").fit(X_treino, y_treino)
print(base.predict(X_teste)[:5]) # always the average
If your model does not beat that guess by a clear margin, it has learned nothing useful.
Metrics in the unit of the target
A number on its own says nothing. Translate the error into the unit of the problem.
| Metric | What it measures | Careful |
|---|---|---|
| R² | How much of the variation the model explains. 0 is guessing the average | Never alone |
| MAE | Average error, in the unit of the target | Direct reading: it is off, on average, by this much |
| RMSE | Error that punishes big errors more | Always equal to or greater than MAE |
What the pros know
R² can be negative, with no limit. A model that always predicts the average has an R² of zero. If it is further off than that, R² goes below zero. It is not a bug: it is the warning that the model is worse than the simplest guess. That is why people who work with models never show an R² alone. They show the error in the unit of the target and the baseline next to it, so the reader knows whether the number is good or only looks good.
Families of models
Linear regression draws a straight line and adds up the effect of each column. The tree asks questions in sequence, such as "is the area greater than this value?", until it reaches a price. It is easy to explain, but it tends to memorize the training data.
The forest trains many different trees and takes the average, and it is off by less than a single tree. Gradient boosting trains small trees in sequence, each one correcting the error of the one before. Choose by validation, not by fashion.
Validation without looking at the test set
Never trust a score taken from a single split. Cross-validation separates the training data into parts, trains on all but one and tests on the one left over, repeating until all have been used. The average of the scores shows whether the difference between two models is real or luck.
The choice of hyperparameters happens inside that validation. The test set stays locked away and is used a single time, with the model already chosen. If you choose by looking at the test set, its score stops being honest.
Careful
Pickle and joblib can execute code when loading. Only open a model from a trusted source. If you only need to make predictions, formats such as ONNX are safer.
Practice · 5 min
- Calculate the baseline with DummyRegressor and write down its average error on the test set.
- Train a linear regression and a small tree. Write down the MAE of each one in the unit of the target.
- Write one sentence for each model: how much does it improve on the baseline?
Summary
- The model learns the relationship between X and y. Hyperparameters are your choice.
- Measure the baseline before any model.
- Report the error in the unit of the target. R² can be negative.
- Choose in validation on the training data and open the test set a single time.
Sources
Read on October 30, 2026.
- scikit-learn. r2_score: R² can be negative; a constant model at the average has an R² of 0.
- scikit-learn 1.0. load_boston: deprecation of the Boston dataset.
- scikit-learn. DummyRegressor: simple baseline.
- scikit-learn. Model persistence: safety when loading models.
Lesson quiz
Three situations to apply what you learned.
Isaque Victor free courses