Clustering
Finding groups in data without knowing them in advance: k-means, HDBSCAN, and when a cluster is more than an arithmetic operation.
The idea
You have ten thousand customer records and no segmentation. Clustering lays them on a table so that similar things sit together, and draws circles around them. Which circles are meaningful is then decided by a person.
What it is good for
- Customer segments as a starting point for a discussion, not as a result.
- Bundling similar error reports before anyone reads them.
- Organising product ranges, grouping sites, surveying text corpora.
The methods compared
| Method | Group shape | Noise allowed | Count required |
|---|---|---|---|
| k-means | spherical, similar size | no | yes |
| Gaussian mixture | elliptical | no | yes |
| Agglomerative | arbitrary, hierarchical | no | yes or a cut |
| DBSCAN | arbitrary, uniform density | yes | no |
| HDBSCAN | arbitrary, varying density | yes | no |
import numpy as np
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score, adjusted_rand_score
from sklearn.preprocessing import StandardScaler
rng = np.random.default_rng(0)
X = np.vstack([rng.normal(m, s, (200, 2))
for m, s in ([(0,0), .5], [(5,5), .8], [(0,6), .4])])
X = StandardScaler().fit_transform(X)
# Stability check: two runs on two seeds, then compare.
a = KMeans(3, n_init=10, random_state=1).fit_predict(X)
b = KMeans(3, n_init=10, random_state=2).fit_predict(X)
print("silhouette", round(silhouette_score(X, a), 3))
print("stability ", round(adjusted_rand_score(a, b), 3)) # close to 1 is goodThe k-means objective
This quantity falls monotonically with k and is therefore worthless as a
selection criterion on its own. The elbow criterion looks for the point where the
gain bends: a heuristic, not a test.
Why k-means thinks spherically
Assignment minimises Euclidean distance to a centroid. Cluster boundaries are
therefore perpendicular bisectors and the cells form a Voronoi diagram: convex
polygons. Elongated, curved or nested groups cannot be captured in principle,
regardless of k.
Cost
| Method | Time | Memory |
|---|---|---|
| k-means | O(n · k · d · i) | O(n · d) |
| Agglomerative | O(n³) naive, O(n² log n) good | O(n²) |
| DBSCAN with an index | O(n log n) | O(n) |
| HDBSCAN | O(n log n) typical | O(n) |
At n = 10⁶, agglomerative clustering with a 4-terabyte distance matrix is out
of the question. In practice you reduce dimensionality first, see
Dimensionality reduction, and then
cluster.
The legal point
Clusters over personal data are new personal attributes. They fall under purpose limitation and are subject to access requests. Where a cluster feeds a decision about a person, it carries a duty to justify, and "the procedure grouped them that way" is not a justification.
Related courses and sources
scikit-learn user guide
Not a manual but a textbook with code. Every method comes with a note on when it does not fit, which textbooks rarely state so plainly.
For anyone using classical methods; each one comes with a note on when it does not fit.