The line of the five stores
Ordito, the clothing chain in the book, wants to open a new store in a city centre. How much will it take? To answer, we have five stores already open, their floor area and their 2025 revenue. It is the smallest problem in the book, and it already contains the whole of machine learning.
A rule and its error
A simple rule is a straight line: predicted revenue = b + w · area. The number w says how much more is taken for each square metre, b is the starting point. Each pair (b, w) is a different model.
To choose between two models you have to measure how wrong they are. The error for a store is the difference between the true revenue and the predicted one. The MAE is the mean of the errors in absolute value; the RMSE is the square root of the mean of the squares, and gives more weight to large errors.
In the lab, move b and w and watch the errors store by store, in red on the chart and in the bars on the right.
Three experiments
The mean. With w = 0 the line is flat and predicts 898.8 for everyone, the mean of the revenues. It is off by 169.4 on average. It is the simplest possible model, and the benchmark that every model has to beat.
Two rules compared. The rule 300 + 2.2 · x is off by 44.88 on average, the rule 250 + 2.5 · x by 47. They are two similar lines, but the steeper one pays more on the stores in the middle, Genoa and Bologna. Switch on “mostra i quadrati degli errori” (show the squares of the errors): the RMSE is the mean of the areas of those squares, under a square root.
The store that doesn’t fit. Add Lecce: 263 square metres and only 487 in revenue. Any reasonable line overestimates it by about 400. The model knows only what the data tell it, and something is missing from the data: for example, the income of the area.
The same experiment in Python
import numpy as np
x = np.array([165, 185, 268, 327, 413]) # floor area in m²
y = np.array([668, 749, 856, 944, 1277]) # 2025 revenue, thousands of euros
for nome, b, w in [("mean", y.mean(), 0), ("300 + 2.2x", 300, 2.2), ("250 + 2.5x", 250, 2.5)]:
errori = y - (b + w * x) # true minus predicted
print(f"{nome:12s} MAE {np.abs(errori).mean():6.2f} RMSE {np.sqrt((errori ** 2).mean()):6.2f}")
print("Lecce:", 300 + 2.2 * 263, "against a true 487")
In the book
This lab accompanies chapter 1, “What is machine learning”. The chapter starts from these five stores to define model, error, training and test, and presents the Ordito questions that the rest of the book answers: regression, classification, clustering, anomalies, time series.
The “Inside Ordito” series
- Chapter 1 The line of the five stores
- Chapter 2 A simulated store
- Chapter 3 Where the leak hides
- Chapter 4 The valley of the five stores
- Chapter 5 The threshold
- Chapter 6 Why was it returned?
- Chapter 7 Build a tree
- Chapter 8 Boosting step by step
- Chapter 9 The margin
- Workshop A Price is place
- Chapter 10 Shapley by hand
- Chapter 11 Rotate the cloud
- Chapter 12 Groups and dense areas
- Chapter 13 Isolating a point
- Chapter 14 Naive forecasts
- Workshop B A year in production
- Chapter 15 The ink stain
- Chapter 16 Backpropagation