Chapter 8: Clustering
Discover hidden structure in unlabeled data through unsupervised learning. Master K-Means for partitioning, hierarchical methods for building cluster trees, density-based DBSCAN for arbitrary shapes, and learn to evaluate cluster quality without ground truth.
- Topics
- 3
- Demos
- 4
- Min read
- 26
Chapter Overview
Clustering is the quintessential unsupervised learning task: finding natural groupings in data without any labels to guide us. Unlike supervised learning where we have ground truth, clustering must discover structure purely from the data itself.
The applications are everywhere: segmenting customers by behavior, grouping similar documents, detecting anomalies that don't fit any cluster, compressing images by grouping similar colors, and exploring high-dimensional data by identifying natural categories.
Different clustering algorithms make different assumptions about what constitutes a "cluster." K-Means assumes clusters are spherical and roughly equal-sized. Hierarchical methods build trees that can be cut at any level. Density-based methods like DBSCAN find clusters of arbitrary shape and automatically identify outliers.
Choosing the right algorithm and parameters requires understanding these assumptions and evaluating results. Unlike classification, we can't simply measure accuracy. Instead, we use metrics like silhouette score that measure how well-separated clusters are, or domain knowledge to assess whether discovered groups are meaningful.
This chapter covers:
- K-Means: Fast partitioning algorithm that iteratively assigns points to nearest centroids
- Hierarchical: Bottom-up or top-down methods that produce dendrograms showing cluster relationships
- DBSCAN: Density-based algorithm that finds clusters of any shape and identifies noise points
- Evaluation: Metrics like silhouette score and methods like the elbow plot to assess clustering quality
Chapter Roadmap
Click any topic to jump in
K-Means Clustering
Partition data into K spherical clusters by iterating between point assignment and centroid updates.
Hierarchical structure and arbitrary-shape density clusters
Hierarchical Clustering
Build a dendrogram by progressively merging clusters — cut at any level for different granularities.
DBSCAN
Find clusters of arbitrary shape using density, and automatically identify noise points as outliers.
Topics
3 topics in this chapter
The previous chapter, Support Vector Machines, closed the book on supervised learning for now: every algorithm so far learned from labeled examples. Often there are no labels at all. A retailer has millions of purchase histories but no list of customer types; a biologist has gene expression profiles but no known cell categories. Can an algorithm discover groups on its own, using nothing but the similarity between points? This chapter, Clustering, studies that question with three families of methods that define a cluster in very different ways.
K-Means, the oldest and most widely used, defines a cluster by its center: every point belongs to the nearest of K centroids. This topic starts with the algorithm itself, Lloyd's alternation between assigning points and moving centers. We then define inertia, the within-cluster sum of squares that K-Means minimizes, and use it with the silhouette score to choose K. The topic closes with the method's limitations, including its sensitivity to initialization and its preference for round, equally sized clusters.
Definition
K-Means clustering partitions points into clusters by choosing centroids and an assignment of each point that together minimize the total within-cluster sum of squared distances from each point to its own centroid, . Lloyd's algorithm finds a local minimum by alternating nearest-centroid assignment and centroid averaging.
In this topic
- 1Algorithm
- 2Inertia (WCSS)
- 3Choosing K
- 4Limitations
Algorithm
Lloyd's algorithm alternates two steps. Assignment: give each point to its nearest centroid, measured by Euclidean distance. Update: move each centroid to the mean of the points assigned to it. Each step can only lower or keep the objective, the total squared distance from points to their centroids, so the algorithm converges, usually in a few dozen iterations, each costing for points in dimensions. It converges to a local minimum, not necessarily the global one, so the starting centroids matter. Random initialization can place two centroids in one true cluster; k-means++ spreads the starting centroids out and is scikit-learn's default.
K-Means alternates between two steps that each reduce the objective . The assignment step finds the optimal cluster for each point given fixed centroids (minimum distance). The update step finds the optimal centroid for each cluster given fixed assignments (the mean). Since each step decreases or maintains , and is bounded below by 0, the algorithm must converge. However, it converges to a local minimum — the result depends on initialization, which is why K-Means++ matters.
K=2, centroids μ₁=(0,0), μ₂=(5,5). Point x=(1,2). Which cluster does it belong to?
Inertia (WCSS)
Inertia, also called the within-cluster sum of squares (WCSS), adds up the squared distance from every point to its assigned centroid: the formula above, where is the centroid of point 's cluster. It is exactly the objective K-Means minimizes, and lower means tighter clusters. Two cautions. First, inertia always falls as K grows, reaching zero when every point is its own cluster, so it cannot pick K by itself. Second, squaring makes it sensitive to outliers: one far point can pull a centroid toward it. Inertia also depends on feature scale, so standardize features before clustering, as with SVMs.
Inertia is the total within-cluster sum of squares. It decomposes as , where TSS is the total scatter and BSS is the between-cluster scatter. Minimizing inertia is equivalent to maximizing BSS — pushing clusters apart. The inertia always decreases with (adding a cluster center can only reduce distances), reaching 0 when . This monotonic decrease is why the elbow method looks for diminishing returns rather than an absolute minimum.
Cluster 1: points {(0,0), (1,1)}, centroid (0.5, 0.5). Calculate inertia for this cluster.
Choosing K
Because inertia always decreases with K, choosing K needs a different criterion. The elbow method plots inertia against K and picks the point where further clusters stop paying off. It is subjective, and many datasets show no clear elbow. The silhouette score is more principled: for each point, is the mean distance to points in its own cluster and the mean distance to points in the nearest other cluster, and ranges from to . Average it over points and pick the K with the highest value. The gap statistic compares inertia with that of random data. Domain knowledge often matters most.
The elbow method plots inertia versus and identifies the point of maximum curvature. Mathematically, this is where the second derivative is maximized — the transition from steep decline to gradual decline. The silhouette score provides a complementary view: is the mean intra-cluster distance (cohesion) and is the mean nearest-cluster distance (separation). Values near indicate well-clustered points; near means boundary points; near means likely misassigned.
Inertia values: K=1→1000, K=2→400, K=3→350, K=4→340, K=5→335. Where's the elbow?
Limitations
K-Means makes strong implicit assumptions. Assigning each point to its nearest centroid splits space into convex regions, so clusters are effectively assumed to be round, similar in size, and similar in spread. Elongated, ring-shaped, or crescent clusters get cut apart, and a dense small cluster next to a sparse large one is mishandled. K must be chosen in advance. Results depend on initialization, which is why scikit-learn runs several starts and keeps the best. Every point must belong to some cluster, so outliers distort centroids. Gaussian mixture models relax the shape assumption, and DBSCAN, later in this chapter, handles arbitrary shapes and noise.
K-Means assumes clusters are isotropic Gaussians (spherical, equal variance). The Voronoi cell assignment creates convex, polygonal decision boundaries — it cannot capture non-convex cluster shapes like crescents or rings. The objective treats all directions equally, so elongated clusters get split along their major axis. For non-spherical clusters, consider Gaussian Mixture Models (which model covariance) or density-based methods like DBSCAN.
Data has two crescent-moon shaped clusters. Will K-Means work with K=2?
Theory Exercise
Problem:
K-Means is run twice on the same data with different random initializations and produces two noticeably different clusterings with different inertia values. Why does this happen, and how would you make the result more reliable?
Hints:
- What kind of minimum does K-Means converge to?
- How does the starting position of centroids affect the outcome?
- What does scikit-learn's n_init parameter do, and what is K-Means++?
Coding Exercise
Problem:
Generate three blob clusters, then use the elbow method (inertia vs K) and the silhouette score to confirm the correct number of clusters. Print the inertia and silhouette for K = 2..6.
Hints:
- make_blobs(n_samples=..., centers=3) gives a labeled-looking dataset, but cluster on X only
- Loop K from 2 to 6, fit KMeans(n_clusters=K, n_init=10, random_state=0), record km.inertia_
- silhouette_score(X, km.labels_) measures separation — the best K maximizes it
Related Problems on PixelBank
The previous topic, K-Means Clustering, forced us to fix the number of clusters before seeing the result, and gave a single flat partition. Real groupings are often nested. Species group into genera and genera into families; customers split into broad segments that divide into finer niches. A biologist or analyst wants to see every level of grouping at once, and to decide how many clusters to keep after looking at the structure, not before. Can an algorithm build that whole hierarchy in one pass?
Hierarchical clustering does exactly this. The common, agglomerative version starts with every point as its own cluster and repeatedly merges the two closest clusters until one remains, recording each merge. This topic walks through that algorithm, then the linkage methods that define distance between clusters (single, complete, average, and Ward) and how each choice changes the shapes found. We then read the dendrogram, the tree diagram of the merges, and cut it to obtain any number of clusters. The topic closes with the method's advantages and its cost, which grows at least quadratically with the number of points.
Definition
Agglomerative hierarchical clustering starts with each of the points as its own cluster and performs merges, each time joining the two clusters that are closest under a chosen linkage criterion. The result is a binary tree, the dendrogram, whose merge heights record the distances at which clusters joined; cutting it yields a flat clustering.
In this topic
- 1Agglomerative Clustering
- 2Linkage Methods
- 3Dendrogram
- 4Advantages
Agglomerative Clustering
Agglomerative clustering is bottom-up. Start with singleton clusters and a matrix of pairwise distances. Repeatedly find the closest pair of clusters, merge them, and update the distances from the new cluster to every other cluster using the linkage rule, until a single cluster remains. The sequence of merges and their distances forms the dendrogram. The algorithm is deterministic, with no random initialization, and needs only pairwise distances, not coordinates, for most linkages. The cost is the problem: storing the distance matrix takes memory and standard algorithms take to time, so it suits thousands of points, not millions. Divisive clustering works top-down instead.
Agglomerative clustering starts with singleton clusters and performs merges. At each step, it merges the two closest clusters according to a linkage criterion, producing a binary tree (dendrogram). The time complexity is for naive implementation or with a priority queue. Unlike K-Means, it does not require specifying upfront — the full hierarchy encodes all possible values from 1 to , and any specific clustering is obtained by cutting the dendrogram at the appropriate height.
4 points: A, B, C, D. Distances: d(A,B)=2, d(A,C)=5, d(B,C)=3, d(C,D)=1. What merges first?
Linkage Methods
The linkage defines the distance between two clusters. Single linkage uses the closest pair of points across them; it can follow long, curved clusters but suffers from chaining, where a thin line of noise points joins two distinct groups. Complete linkage uses the farthest pair, favoring compact clusters of similar diameter, but is sensitive to outliers. Average linkage uses the mean of all cross-cluster distances, a compromise. Ward's method merges the pair that least increases total within-cluster variance, the same inertia K-Means minimizes, and tends to produce balanced, round clusters; it requires Euclidean distances. Ward is scikit-learn's default and works well when clusters are compact.
Single linkage uses — it can find elongated, chain-like clusters but is sensitive to noise bridges between clusters (chaining effect). Complete linkage uses — it produces compact, spherical clusters but is sensitive to outliers. Ward's method minimizes the total within-cluster variance increase — it is equivalent to K-Means in the sense that it optimizes the same inertia objective, but in a greedy, hierarchical fashion.
Cluster X={A,B}, Cluster Y={C}. d(A,C)=3, d(B,C)=7. What's distance using single vs complete linkage?
Dendrogram
A dendrogram draws the merge history as a tree. Leaves at the bottom are individual points; each horizontal bar joins two clusters at a height equal to their linkage distance when they merged. Cutting the tree with a horizontal line at height yields a flat clustering whose number of clusters equals the number of vertical branches the line crosses. Low cuts give many small clusters, high cuts few large ones. A long vertical stretch with no merges signals a natural clustering, since those clusters stay separate over a wide range of distances. The cophenetic correlation measures how faithfully the merge heights preserve the original pairwise distances.
The dendrogram is a tree where each internal node represents a merge event at distance (height) . A horizontal cut at height produces a clustering with clusters, where is the number of branches crossing the cut line. Large vertical gaps in the dendrogram indicate natural cluster separations — the optimal cut is typically at the largest gap. The cophenetic distance between two points is the height at which they first merge; the cophenetic correlation between pairwise distances and cophenetic distances measures how well the dendrogram preserves the original distance structure.
Dendrogram has horizontal cut options at heights 5, 15, and 30. Height 5 gives 4 clusters, height 15 gives 2. What does height 30 give?
Advantages
Hierarchical clustering's main strength is that it returns every level of grouping at once. You need not choose K in advance, since one dendrogram serves all values of K, and you can choose after inspecting the structure. The tree itself is interpretable and suits domains that really are nested, such as taxonomies or document topics. Average, single, and complete linkage work with any distance or dissimilarity, such as edit distance for strings, so no coordinates are required, although Ward needs Euclidean data. Results are deterministic. The costs are quadratic memory, merges that can never be undone, and sensitivity to the linkage choice.
Hierarchical clustering produces a complete hierarchy of nested clusterings, a richer output than any flat clustering method. The dendrogram reveals multi-scale structure: cutting at different heights yields different granularities. Single, complete, and average linkage work with any dissimilarity (not just Euclidean), making them applicable to domains with custom measures like edit distance for strings or Jaccard distance for sets; Ward's criterion is defined through centroids, so it assumes squared Euclidean distance. The deterministic nature (no random initialization) means results are reproducible without multiple restarts, up to ties between equal distances.
You're clustering species by genetic similarity. Why might hierarchical clustering be ideal?
Theory Exercise
Problem:
You run agglomerative clustering on the same dataset with single linkage and with complete linkage and get very different dendrograms — single linkage produces one giant straggly cluster while complete linkage gives compact balls. Explain why, and when each linkage is appropriate.
Hints:
- How does single linkage define the distance between two clusters?
- What is the 'chaining effect'?
- Which linkage favors compact clusters and which favors elongated ones?
Coding Exercise
Problem:
Run agglomerative clustering with Ward linkage on a blob dataset and reproduce the merge structure as a dendrogram using scipy's linkage and dendrogram functions. Cut the tree into 3 clusters and report the cluster sizes.
Hints:
- scipy.cluster.hierarchy.linkage(X, method='ward') returns the merge matrix Z
- fcluster(Z, t=3, criterion='maxclust') cuts the tree into a fixed number of clusters
- np.bincount on the resulting labels gives the size of each cluster
The previous topic, Hierarchical Clustering, built a full tree of nested groups, and K-Means before it found round clusters around centers. Both share two weaknesses. Every point must belong to some cluster, so outliers distort the result, and both struggle with clusters of arbitrary shape, such as crescents, rings, or winding streets on a map. Can we define a cluster not by a center or a merge order, but simply as a region where points are packed densely, separated from other such regions by sparse space?
DBSCAN, short for Density-Based Spatial Clustering of Applications with Noise, introduced by Ester and colleagues in 1996, does exactly that. It needs no number of clusters, finds arbitrary shapes, and labels isolated points as noise. This topic builds it from its three point types: core points in dense neighborhoods, border points on a cluster's edge, and noise points that belong nowhere. We then study its two parameters, the radius eps and the count min_samples, how to choose them with a k-distance plot, and why a single density threshold fails when clusters differ in density, the gap HDBSCAN fills.
Definition
DBSCAN groups points by density. A point is a core point if at least minPts points, counting itself, lie within radius . Clusters are formed by connecting core points within of each other and attaching border points within of a core point; all remaining points are labeled noise.
In this topic
- 1Core Points
- 2Border Points
- 3Noise Points
- 4Parameters
Core Points
A core point has at least minPts points within distance of it, a neighborhood dense enough to anchor a cluster. Clusters grow from core points: any two core points within of each other belong to the same cluster, and chains of such links let a cluster snake through space in any shape. That is why DBSCAN can follow crescents and rings that K-Means cuts apart. A counting convention matters: the original paper and scikit-learn's min_samples count the point itself. With minPts = 5, a point needs 4 other points within . Distances are computed with a spatial index, so typical runs scale far better than the quadratic hierarchical methods.
A core point has at least points within its -neighborhood: , where includes itself. Core points anchor cluster expansion: they sit in dense regions and pull nearby points into their cluster. The implied density threshold is about points per unit area in 2D (or in dimensions, where is the volume of a -ball). Points that are density-reachable from a core point, through a chain of core points each within of the next, belong to the same cluster.
ε=1, minPts=3. Point A has 4 neighbors within distance 1. Point B has 2 neighbors. Which are core points?
Border Points
A border point has fewer than minPts points within , so it is not dense enough to be core, but it lies within of at least one core point. It joins that core point's cluster and sits on the cluster's edge. Border points never extend a cluster: expansion only continues from core points, which keeps a cluster from leaking across sparse regions. A border point within of core points from two different clusters is assigned to whichever cluster reaches it first, so its label can depend on the order of the data. That ambiguity aside, DBSCAN's result is deterministic, with no random initialization as in K-Means.
Border points are within of at least one core point but have fewer than neighbors themselves: but core point with . They sit on the periphery of dense regions. Border points can be density-reachable from multiple core points in different clusters — DBSCAN assigns them to the first cluster discovered during traversal, making border point assignment non-deterministic (order-dependent). This is the only source of non-determinism in DBSCAN.
Point B (2 neighbors) is within ε of core point A. What is B's classification?
Noise Points
A noise point is neither core nor border: fewer than minPts points lie within of it, and it is not within of any core point. DBSCAN labels such points instead of forcing them into a cluster, which makes it a natural outlier detector and keeps outliers from dragging cluster shapes the way they drag K-Means centroids. The amount of noise is controlled by the parameters: a small or large minPts raises the density bar, so more points become noise. A large noise fraction is a diagnostic: either the parameters are too strict or the data contain regions of very different density.
Noise points are neither core nor border: they are not within of any core point. DBSCAN labels them as , effectively treating them as outliers. The fraction of noise points depends on the density threshold set by . This built-in outlier detection is a major advantage over K-Means and hierarchical clustering, which force every point into a cluster. The noise fraction can serve as a diagnostic: too many noise points suggests is too small or is too large.
Point C is isolated—no points within ε. What happens to C?
Parameters
ε (eps): neighborhood radius. minPts: minimum points for core.
DBSCAN has two parameters. (eps) is the neighborhood radius and minPts (min_samples) the count required for a core point; together they set a density threshold. Smaller means more noise and more, smaller clusters; larger merges clusters, eventually into one. A common heuristic sets minPts to at least the dimension plus one, often twice the dimension. To choose , compute each point's distance to its k-th nearest neighbor, with k = minPts - 1, sort these distances, and look for the elbow where they rise sharply. The main limitation is the single global threshold: clusters of very different densities cannot all be captured, which HDBSCAN addresses.
The parameters jointly define the density threshold. The k-distance plot (each point's distance to its -th nearest neighbor, sorted) helps choose : the elbow marks the boundary between dense cluster points and sparse noise. Use when minPts counts the point itself, as in scikit-learn. A common heuristic sets , often , so the density estimate is robust. DBSCAN's main limitation is a single global : it cannot handle clusters with very different densities. HDBSCAN addresses this by considering all values at once and extracting the most persistent clusters.
ε=0.5 gives 10 clusters, 50 noise points. ε=2.0 gives 2 clusters, 5 noise points. What trade-off are we seeing?
Theory Exercise
Problem:
Your dataset has varying densities: one tight cluster and one sparse cluster. Which clustering algorithm would work best and why?
Hints:
- Consider how each algorithm handles density
- What does DBSCAN assume about density?
- Think about parameter sensitivity
Coding Exercise
Problem:
Generate two interleaving half-moons and cluster them with DBSCAN, showing that it recovers the non-convex shapes that K-Means cannot. Print the number of clusters found and how many points were labeled as noise.
Hints:
- make_moons(n_samples=..., noise=0.06) creates two crescent shapes
- DBSCAN(eps=0.2, min_samples=5).fit_predict(X) returns labels, with -1 meaning noise
- Count clusters as the number of unique labels excluding -1; noise count is (labels == -1).sum()