Unsupervised learning
Finding structure in data without anyone saying which structure. What it is good for, where it is overrated, and how to judge a result at all.
The idea
You tip a box of screws onto a table and sort them without anyone naming the categories. By length? By head shape? By thread? Every sorting is correct, and which one is useful depends on what you plan to do next.
Unsupervised learning works exactly like that: it finds an order, but the meaning is yours to supply.
What it is good for
- Discovering customer groups before anyone has defined segments.
- Flagging unusual transactions, such as conspicuous bookings.
- Condensing very many features into a few so a chart becomes readable.
- Grouping similar documents before anyone reads them.
Methods by task
| Task | Method | When it fits |
|---|---|---|
| Find groups | k-means, HDBSCAN | Spherical groups / arbitrary shapes with noise |
| Estimate density | Gaussian mixtures | Soft membership, probabilities needed |
| Find outliers | Isolation Forest, One-Class SVM | Few, unknown anomalies |
| Reduce dimensions | PCA, UMAP | Preprocessing / display |
| Find topics | NMF, BERTopic | Surveying a text corpus |
import numpy as np
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import StandardScaler
rng = np.random.default_rng(0)
X = np.vstack([rng.normal(m, 0.6, (120, 2)) for m in ([0,0], [4,4], [0,5])])
X = StandardScaler().fit_transform(X) # without scaling, the feature with
# the largest unit dominates
for k in range(2, 7):
labels = KMeans(k, n_init=10, random_state=0).fit_predict(X)
print(k, round(silhouette_score(X, labels), 3))- Always scale, otherwise the unit of measurement decides the result.
- Try several values of
kand have a domain expert name the outcome. - Test stability: rerun with a different seed and compare.
- Check group sizes. A cluster with three cases is usually an artefact.
Silhouette
Values above 0.5 count as clear structure, below 0.25 as barely present. The coefficient does favour spherical clusters and systematically underrates HDBSCAN results that contain noise points.
The legal point
Clustering personal data creates a new category that did not exist before. That category can effectively encode a special category under Art. 9 GDPR without naming it: neighbourhood plus purchasing behaviour plus time of day frequently resolves to origin or health. For purpose limitation this means the purpose of the original collection rarely covers the grouping. See GDPR basics and Bias in data.
An anomaly is not an error
An Isolation Forest flags the rare, not the wrong. In invoice checking, the largest invoice of the year is an anomaly and perfectly correct. Which is why every anomaly detection ends in a human review, and the metric is not the hit rate but the share of reviewed alerts that triggered an action.