Lecture 11 · room 204
HEMA · Università Cattolica del Sacro Cuore, Rome
Tuesday 3 November 2026
Lecture 10 · Learning without a label · Mon 2 Nov
| Time | Min | |
|---|---|---|
| 09:00 | Last time · questions | 10’ |
| 09:10 | The algorithm: Lloyd’s four lines, by hand | 60’ |
| 10:10 | Two silent failures: scale and shape | 20’ |
| 10:30 | Break | 15’ |
| 10:45 | Choosing k: the elbow, and the elbow you get | 35’ |
| 11:20 | From clusters to segments | 25’ |
| 11:45 | In R · Set 10 | 15’ |
One offer for everyone, or one for each customer? Neither.
By the end of today you can build one, and you know what is wrong with the one you inherit.
Given n points and a number k that you choose, find k groups C_1,\dots,C_k that minimise the within-cluster sum of squares:
W(C) \;=\; \sum_{j=1}^{k} \;\sum_{i \in C_j} \lVert x_i - \bar{x}_j \rVert^2
Note
The true minimum over all partitions is out of reach: n = 100, k = 3 is more partitions than atoms in the universe. k-means finds a good one, fast.
Each step can only lower W, so it always stops. Usually within a dozen rounds.
Attention
It stops at a minimum, not the minimum. Different starting points give different answers. Always run it several times and keep the best: nstart = 25, never nstart = 1.
Deliberately bad starting centroids, so the first update is large. Watch W: it falls at every half-step, which is why the algorithm is guaranteed to stop — it cannot go back up.
Visits per month and average basket, already scaled
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| visits | 1 | 2 | 1 | 8 | 9 | 8 |
| basket | 1 | 1 | 2 | 8 | 8 | 9 |
k = 2. Start with centroids at A and D.
W = 2.67 + 2.67 = 5.3. Any other split of these six gives a larger W — try \{A,B,C,D\} vs \{E,F\} and see.
Every squared distance, both centroid updates, and W at the end. Six points is small enough to check the algorithm’s arithmetic against your own.
Both happen in every real dataset
Scale. Income in euros and age in years: income dominates the distance entirely, age is noise. k-means sees only geometry. Standardise first — scale() — unless you have a reason not to.
Twenty-eight years against six thousand euro, both squared: 784 against 36 million. Age is not weighted lightly, it is absent. The clusters you get back are income bands wearing a segmentation’s clothes.
Shape. k-means draws straight boundaries between centroids. It finds round, similar-sized blobs. Give it two crescents, or one dense core inside a diffuse halo, and it will cut them wrongly and report a fine W.
Two crescents any eye separates instantly. k-means cuts straight down the middle, splits both, and reports a W that looks perfectly reasonable in the output.
Note
Neither failure produces an error message. The output looks like any other segmentation. This is why you look at the plot before you trust the numbers.
Textbook curves have a corner. Most real ones bend smoothly, and the elbow you find is the elbow you went looking for. Bring a second criterion — silhouette, stability, or a business constraint on how many segments a team can act on.
Two honest tools:
library(tidyverse); library(factoextra)
X <- scale(USArrests) # always: same units for every column
fviz_nbclust(X, kmeans, method = "wss") # elbow
fviz_nbclust(X, kmeans, method = "silhouette")
set.seed(1)
km <- kmeans(X, centers = 4, nstart = 25) # 25 random starts, keep the best
fviz_cluster(km, data = X) # the picture you show a colleagueNote
set.seed() makes the random starts reproducible. Without it, your colleague reruns your script and gets different segment numbers — the same segments, relabelled — and a meeting is lost to confusion.
The algorithm gives you 1, 2, 3, 4. Nobody in marketing can act on 3.
Profile each cluster: the mean of every variable, per cluster, back in the original units.
| Segment | Visits / month | Basket (€) | Tenure (yrs) | Share |
|---|---|---|---|---|
| 1 | 0.8 | 62 | 1.1 | 41% |
| 2 | 6.4 | 28 | 4.8 | 22% |
| 3 | 2.1 | 140 | 3.9 | 19% |
| 4 | 4.0 | 55 | 0.4 | 18% |
Then name them from the numbers — occasional big-basket, frequent small-basket loyalists, … — and only then hand them over.
Attention
The segmentation is only as good as the variables you fed it. Cluster on demographics and you get demographic segments, which rarely predict behaviour. Cluster on behaviour — recency, frequency, monetary value — and the segments are things a company can move.
The choice of k is a decision, and it is yours.
Friday is clustering end to end, on a real customer table. Come having run today’s R block on USArrests, and with an answer to:
kmeans with nstart = 1 three times. Do the clusters change?k-means and customer segmentation · about 40 minutes · before Mon 9 Nov
Not graded. The test is a mirror, not an exam.
?kmeans, and the factoextra vignette for the plotsStatistics & Big Data · HEMA, Università Cattolica del Sacro Cuore