Clustering groups unlabeled points so that members of a group are more alike than members of different groups. There is no ground truth to check against, so the algorithm’s assumptions about what a cluster looks like decide the result. I think of the three standard methods as three answers to that question: a compact blob around a center (k-means), a Gaussian with its own shape (GMM), or a dense region separated by sparse gaps (DBSCAN).

Clustering puts similar dots into the same group without anyone telling it the groups. Rendered for this page.

Mental Model

  • k-means minimizes within-cluster squared distance to a centroid. It assumes roughly round, similar-sized clusters and needs in advance.
  • A Gaussian mixture model is a soft, probabilistic generalization: each cluster has its own mean, covariance and weight, and each point gets a membership probability.
  • DBSCAN defines clusters by density, finds arbitrary shapes, labels outliers as noise, and does not need but does need a distance scale.
  • Initialization and scaling matter more than people expect: k-means finds a local optimum, and distances change if features have different units.
  • Choosing the number of clusters is a modeling decision supported by heuristics, not something the data decides alone.

1. k-means and Lloyd’s Algorithm

Given points and , k-means seeks assignments and centroids minimizing

Lloyd’s algorithm alternates two steps. Assign each point to its nearest centroid. Then move each centroid to the mean of its assigned points, which is the exact minimizer of for fixed assignments. Both steps can only lower (or leave it unchanged) and there are finitely many assignments, so it terminates, but only at a local minimum. Finding the global minimum is NP-hard. Each iteration costs .

Consequences of the objective: clusters are Voronoi cells with linear boundaries, outliers drag centroids because squared distance punishes them, and features on larger scales dominate, so standardize first. k-means cannot separate two concentric rings or two interleaved crescents.

2. k-means++ Seeding

Random initial centroids can land two in one blob and none in another, and Lloyd will not fix that. k-means++ (Arthur and Vassilvitskii, 2007) picks the first centroid uniformly at random, then each following centroid with probability proportional to , the squared distance to the nearest already chosen centroid. Points far from existing centers are likely picks, so the seeds spread out. The expected cost is within of optimal, and in practice it also converges in fewer iterations. Standard practice is k-means++ plus several restarts, keeping the lowest .

3. Choosing

decreases as grows (at it is zero), so minimizing it directly is useless. Two common heuristics:

  • Elbow: plot against and look for the bend where extra clusters stop paying off. Often ambiguous.
  • Silhouette: for point , let be its mean distance to its own cluster and the smallest mean distance to another cluster. Then . The average over points is high when clusters are tight and well separated, and you pick the that maximizes it.

The code below implements k-means++, Lloyd and the silhouette on three Gaussian blobs (centers , , , standard deviation 0.6, 100 points each) and tries with five restarts each.

import numpy as np
 
def kmeans(X, k, iters=100, seed=0):
    rng = np.random.default_rng(seed)
    C = [X[rng.integers(len(X))]]                      # k-means++ seeding
    for _ in range(k - 1):
        d2 = np.min([((X - c) ** 2).sum(1) for c in C], axis=0)
        C.append(X[rng.choice(len(X), p=d2 / d2.sum())])
    C = np.array(C)
    for _ in range(iters):                             # Lloyd's algorithm
        lab = ((X[:, None] - C[None]) ** 2).sum(2).argmin(1)
        newC = np.array([X[lab == j].mean(0) if (lab == j).any() else C[j] for j in range(k)])
        if np.allclose(newC, C): break
        C = newC
    return lab, C, ((X - C[lab]) ** 2).sum()
 
def silhouette(X, lab):
    D = np.sqrt(((X[:, None] - X[None]) ** 2).sum(2))
    s = []
    for i in range(len(X)):
        same = (lab == lab[i]); same[i] = False
        a = D[i, same].mean()
        b = min(D[i, lab == j].mean() for j in set(lab) if j != lab[i])
        s.append((b - a) / max(a, b))
    return np.mean(s)
 
rng = np.random.default_rng(42)
X = np.vstack([rng.normal(m, 0.6, (100, 2)) for m in [(0, 0), (4, 0), (2, 3.5)]])
for k in range(1, 7):
    lab, C, J = min((kmeans(X, k, seed=s) for s in range(5)), key=lambda r: r[2])
    print(k, round(J, 1), round(silhouette(X, lab), 3) if k > 1 else "-")

Running it gives:

Silhouette
11840.8n/a
21009.50.497
3202.80.726
4170.50.599
5145.70.457
6125.20.345

drops from 1009 to 203 going from 2 to 3 clusters, then only by 32 more going to 4: a clear elbow at . The silhouette peaks at as well. Real data seldom agrees this cleanly.

4. Gaussian Mixtures and EM

A GMM models the density as with mixing weights summing to 1. The log-likelihood has a sum inside the log, so there is no closed-form maximizer. Expectation-Maximization alternates:

The responsibilities are soft assignments. EM never decreases the likelihood and converges to a local maximum, so initialization matters (k-means output is a typical starting point). Relative to k-means, a GMM captures elliptical clusters through , gives uncertainty for points between clusters, and works as a density model. If you fix , equal weights, and let , the E-step becomes a hard assignment and EM reduces to Lloyd’s algorithm. Watch for collapse: a component centered on one point with shrinking variance sends the likelihood to infinity, so add a small variance floor. Unlike , the likelihood penalized by BIC can compare different .

5. DBSCAN

DBSCAN needs a radius and a count (minPts). A point is a core point if at least points (itself included) lie within . A cluster is a maximal set of core points reachable from each other through chains of core points, plus the border points within of a core point. Everything else is noise (label ).

It finds arbitrary shapes such as rings and crescents, labels outliers explicitly, and the number of clusters falls out. The weak points are the two parameters and a single global density: clusters of very different density cannot both be right for one . A usual recipe sets or higher, then plots the sorted distance to each point’s -th nearest neighbor and sets at the knee of that curve. HDBSCAN removes the single- limitation by working across a range of scales. Distances also degrade in high dimensions, so DBSCAN is best run after dimensionality reduction (Dimensionality Reduction).

6. Hierarchical Clustering

Agglomerative clustering starts with every point as its own cluster and repeatedly merges the closest pair, producing a dendrogram you can cut at any height. The linkage rule defines “closest”: single (minimum pairwise distance, prone to chaining), complete (maximum), average, or Ward (merge that adds the least within-cluster variance, which behaves like k-means). It needs no up front and yields a whole hierarchy, but costs memory in the plain version, so it suits thousands of points, not millions.

Common mistake

Running k-means on raw features with mixed units (income in euros, age in years) and calling the result meaningful. Distances are dominated by the largest-scale feature. Standardize, or use a metric that fits the data.