AI Compass
Compass

Clustering

Finding groups in data without knowing them in advance: k-means, HDBSCAN, and when a cluster is more than an arithmetic operation.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

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

MethodGroup shapeNoise allowedCount required
k-meansspherical, similar sizenoyes
Gaussian mixtureellipticalnoyes
Agglomerativearbitrary, hierarchicalnoyes or a cut
DBSCANarbitrary, uniform densityyesno
HDBSCANarbitrary, varying densityyesno
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 good

The k-means objective

Within-cluster sum of squares

WCSS = Σⱼ₌₁..ₖ Σ_{x∈Cⱼ} ‖x − μⱼ‖²

What gets minimised is the sum of squared distances from every point to its cluster centroid.

Cⱼ
the point set of the j-th cluster
μⱼ
the centroid of the j-th cluster
k
the number of clusters

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

MethodTimeMemory
k-meansO(n · k · d · i)O(n · d)
AgglomerativeO(n³) naive, O(n² log n) goodO(n²)
DBSCAN with an indexO(n log n)O(n)
HDBSCANO(n log n) typicalO(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.

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

ArticleFreeEN

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.

scikit-learnGo to offer
Was this page helpful?
Clustering