cat /en/rotate-the-cloud/
MDN-119:22
SERIES Inside Ordito · Chapter 11

Rotate the cloud

· Artificial Intelligence · 4 min read

The director of Ordito’s sales network has a sheet with 41 points of sale and twenty-three columns. He wants to understand which stores resemble each other, but twenty-three dimensions cannot be drawn. Principal component analysis, PCA, finds a few directions that summarise the columns while losing as little as possible.

The most spread-out direction

Let us start with two columns: the share of takings made with discounted garments and the average receipt, both standardised. They are closely linked: where a lot is sold at a discount the receipt is lower, and the correlation is −0.888. The stores lie almost on a sloping line.

Projecting each store onto a direction means summarising it with a single number. PCA chooses the direction in which the projections are most spread out, that is, have the largest variance. That direction is the first principal component, and mathematically it is the eigenvector of the covariance matrix with the largest eigenvalue.

In the lab, turn the line and watch the projections and their variance, on the right, as the angle changes.

DEMO Rotate the cloud · about this demo · full screen ↗

Three experiments

The most spread-out direction. The variance rises and falls with the angle. The maximum is at 135°, on the diagonal where the discounted share falls and the receipt rises. It gathers 1.888 of the 2 units of variance, 94%.

Eigenvalues and eigenvectors. With two standardised columns the eigenvalues are 1 + |r| and 1 − |r|, and the eigenvectors the two diagonals. The curve goes exactly from 0.112 to 1.888: the theory, drawn.

Twenty-three columns. Switch to the complete map. The first component sets the five outlets apart, the second the website. At the bottom four stores stand apart: they are the ones opened between 2023 and 2025, with few loyal customers. PCA knew nothing about opening dates, and found them by itself.

The same experiment in Python

import numpy as np
import pandas as pd

URL = "https://raw.githubusercontent.com/mid079/machine-learning-teoria-pratica/main/dati/"
righe = pd.read_parquet(URL + "righe_scontrino.parquet", columns=["id_scontrino", "quantita", "prezzo_unitario", "sconto_pct"])
scontrini = pd.read_parquet(URL + "scontrini.parquet", columns=["id_scontrino", "id_negozio", "totale"])
righe = righe.merge(scontrini[["id_scontrino", "id_negozio"]], on="id_scontrino")
righe["importo"] = righe["quantita"] * righe["prezzo_unitario"]

profilo = pd.DataFrame({
    "quota_scontata": righe[righe["sconto_pct"] > 0].groupby("id_negozio")["importo"].sum() / righe.groupby("id_negozio")["importo"].sum(),
    "scontrino_medio": scontrini.groupby("id_negozio")["totale"].mean()})
Z = (profilo - profilo.mean()) / profilo.std(ddof=0)          # standardise
S = Z.T @ Z / len(Z)                                            # correlation matrix
autovalori, autovettori = np.linalg.eigh(S)
print(S.round(3).to_numpy(), autovalori.round(3), autovettori[:, 1].round(3))

In the book

This lab accompanies chapter 11, “Dimensionality reduction”: PCA from scratch with eigenvectors and with scikit-learn, reading the components, how many components to keep, PCA before KNN, LDA when the classes are known and t-SNE for looking at the garments on a map.

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