Module 1 · Technical track (optional): machine learning fundamentals

First model: predict with measurement and comparison

Lesson 3 of 15 9 min lesson Updated on October 30, 2026
Progress in the course20%

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.

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.

MetricWhat it measuresCareful
R²How much of the variation the model explains. 0 is guessing the averageNever alone
MAEAverage error, in the unit of the targetDirect reading: it is off, on average, by this much
RMSEError that punishes big errors moreAlways 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

  1. Calculate the baseline with DummyRegressor and write down its average error on the test set.
  2. Train a linear regression and a small tree. Write down the MAE of each one in the unit of the target.
  3. Write one sentence for each model: how much does it improve on the baseline?
Deliverable: a file called "My first comparison", with the baseline and the metrics of each model in the unit of the target.

Summary

Sources

Read on October 30, 2026.

  1. scikit-learn. r2_score: R² can be negative; a constant model at the average has an R² of 0.
  2. scikit-learn 1.0. load_boston: deprecation of the Boston dataset.
  3. scikit-learn. DummyRegressor: simple baseline.
  4. scikit-learn. Model persistence: safety when loading models.

Lesson quiz

Three situations to apply what you learned.

Lesson 3 of 15

Before moving on to the next lesson

Put what you saw into practice. The boxes stay checked in this browser, so you can come back later.

Have a question about this lesson? The IV Help team answers personally on WhatsApp.

Ask on WhatsApp