cat /en/the-valley-of-the-five-stores/
MDN-120:20
SERIES Inside Ordito · Chapter 4

The valley of the five stores

· Artificial Intelligence · 5 min read

Five stores of Ordito, the clothing chain in the book, and one question: how much more does a store take for each square metre? The answer is a straight line, revenue = b + w · area. In this lab we find it the way a computer finds it: going down into a valley in small steps.

A valley of errors

Each pair of numbers (b, w) is a different line, and each line is off by something on the five stores. If we measure the error with the mean of the squared deviations, each pair (b, w) corresponds to a height. All together they form a valley: the bottom is the best line.

Gradient descent does not see the valley from above. Like a hiker in the fog, it only looks at which way the ground slopes down most steeply, takes a step and repeats: (b, w) ← (b, w) − η · gradient. The number η is the length of the step.

In the lab, on the left there is the valley seen from above, with the contour lines and the path of the weights. On the right, the five stores with the current line and, in red, its errors.

DEMO The valley of the five stores · about this demo · full screen ↗

Three experiments

The right step. With the floor area standardised and η = 0.1 the descent reaches the bottom in a few dozen steps: b = 898.8 and w = 204.6. Converted back to square metres they are the line 291.9 + 2.2345 · x, the same one that chapter 4 finds with the exact formula.

The step that is too long. With η = 0.9 the point bounces from one slope to the other, but each bounce is lower and in the end it gets there. Beyond η = 1 each bounce is higher than the previous one, and the error explodes.

The narrow valley. Now the floor area stays in square metres. The valley becomes a long and extremely narrow gully. With η = 0.00001 the point goes straight down to the floor and then advances by millimetres: after ten thousand steps it is still at b = 5.9 instead of 291.9. With η = 0.00002 it leaves the valley in a few dozen steps.

There is only one lesson. The right step depends on the shape of the valley, and the shape depends on the scale of the variables. Standardising makes the valley round, and a single step works well in all directions.

The same experiment in Python

It is the function from chapter 4, cut down to the bone.

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

def discesa(x, passo, iterazioni):
    A = np.column_stack([np.ones(len(x)), x])  # column of 1s for the intercept b
    w = np.zeros(2)
    for _ in range(iterazioni):
        gradiente = 2 / len(y) * A.T @ (A @ w - y)
        w = w - passo * gradiente                # one step downhill
    return w

z = (x - x.mean()) / x.std()                   # standardised floor area
print(discesa(z, 0.1, 200))       # at the bottom of the valley
print(discesa(x, 1e-5, 10_000))   # still very far away
print(discesa(x, 2e-5, 50))       # huge numbers: the descent diverges

In the book

This lab accompanies chapter 4, “Linear regression”. The chapter derives the exact formula of the line, writes gradient descent in NumPy, introduces the stochastic version and arrives at Ridge, Lasso and ElasticNet on the stores’ weekly sales.

COVER COMING SOON
mltp · 2026

Machine Learning. Teoria, matematica e pratica degli algoritmi

Michele di Nuzzo · in Italian

Every algorithm in two steps: first derive it, with the mathematics it needs and an example worked by hand, then write it from scratch in NumPy. Only at the end use it with scikit-learn.

[the book’s page] [shop links coming soon]

The “Inside Ordito” series