Agglomerative Clustering and DBSCAN
Agglomerative clustering is one family of these methods, and its strategy is unusual: instead of splitting one big group into smaller ones, it begins at the opposite extreme and builds larger groups up from the bottom.
Testing an Assumption About k-means
After learning k-means, it is natural to assume it works like the other decomposition methods you have seen. PCA and NMF both describe each data point as a combination of learned building blocks, so it seems reasonable that k-means does the same thing with its cluster centers. This section tests that assumption. The same test set of face images is reconstructed by all three methods, and the differences in what each method keeps are what the comparison is designed to reveal.
What do you think happens?
Suppose you fit PCA, NMF, and k-means with 100 components or 100 cluster centers, then ask each model to reconstruct a face from the test set. Which reconstructions are built from many components at once, and which is built from only one stored vector?
Reveal answer
PCA and NMF build each reconstruction from several components at once, while k-means returns a single stored cluster center.
The reconstruction code for each method reveals the difference: PCA and NMF combine learned components, while k-means performs a lookup of one center for each test face. The rest of this article traces why that is so.
Keep this prediction in mind. The sections that follow show that the answer is not a matter of opinion: it follows directly from the steps each method performs when it reconstructs a face.
Three Reconstructions Side by Side
The source fits all three models on the same training split, using 100 components for PCA and NMF and 100 cluster centers for k-means. The test faces are then reconstructed by each model. The training data shapes the three models, and the test data is only passed through them. Each model's reconstruction is therefore a statement about what that model learned from the training faces.
The comparison has two parts: the components each method extracts, and the reconstructions each method produces. For components, k-means yields cluster centers, which are typical faces of the groups it found. PCA and NMF yield components that are reused across many faces. For reconstructions, PCA and NMF build a face from several components at once, while k-means returns the single closest center.
The diagram shows the two logics side by side. The k-means column has a single chosen center at the end of the path, while the PCA and NMF column has several components feeding into one sum. The plotting code in the source places the same test faces next to each method's output, which is how these differences become visible on screen.
Following a Face Through the k-means Path
The correction to the misconception is best seen as a path of steps. A test face is first assigned to its nearest training-set cluster, which yields an integer index. That index selects one stored center. The selected center becomes the reconstruction. No blending happens at any stage.
- 1Start with the test face x, a vector of pixel values that was not used during training.
- 2Compare x with every cluster center learned on the training set and find the closest one.
- 3Record the index k* of that closest center. This index is an integer, not a blend of centers.
- 4Select the stored center c_k* at that index from the learned cluster centers.
- 5Return c_k* as the reconstruction of x.
In this notation, x is a test face, c_k is the k-th cluster center learned on the training set, and k* is the index of the closest center to x. The formula restates the description in the source: the reconstruction is the closest cluster center found on the training set. Because the output can only be one of the centers learned from the training data, k-means reconstructions look like typical faces from the training set, not like the particular test face.
Vector Quantization as a New Representation
The source points out a second, less obvious use of k-means. Clustering can also encode data, a technique called vector quantization. Here the goal is no longer a faithful reconstruction. Instead, each point is replaced by its cluster assignment, and that assignment becomes a feature. The source notes that many more clusters than input dimensions can be used this way, which PCA and NMF cannot do in the same sense.
The source's example is the two_moons dataset, which lives in only two dimensions. Reducing it to one dimension with PCA or NMF would destroy its structure. Using k-means with more cluster centers gives a more expressive representation, and the source fits 10 cluster centers to show it.
Read the diagram as a feature vector with one position per cluster. The point's assignment selects position 3, which receives the value 1. Every other position holds 0. This is the sea of 0s with a single 1 that the source describes. The representation says only which cluster the point belongs to.
The source adds that an even richer representation is possible by using the distances to each cluster center as features, which is obtained through the transform method of the fitted k-means model. Those distances keep more information than a single 1, because they record how far the point is from every center.
Why Random Starts Change the Result
A final point from the source explains a drawback that the comparison makes visible. The result of k-means depends on a random initialization, so the outcome depends on a random seed. A different seed can give different cluster centers, and therefore different reconstructions. The random_state=0 argument in the code fixes the seed, so the result is repeatable in that example.
scikit-learn reduces this dependence by running several initializations and keeping the best result. Rather than trusting one random starting point, the model tries multiple starts and selects the run that gives the better outcome. This reduces the risk of settling on a poor set of centers, but it does not remove the randomness from the method.
The source also lists the strengths that make k-means popular: it is relatively easy to understand and implement, it runs relatively quickly, and it scales easily to large datasets. scikit-learn includes MiniBatchKMeans for very large datasets. The same source lists the drawbacks: the reliance on random initialization, relatively restrictive assumptions about the shape of clusters, and the requirement to specify the number of clusters, which may not be known in a real-world application.
Starting From Singletons
Suppose you have a pile of unlabeled data points and no idea how many natural groups they form. Clustering methods answer this kind of question by grouping points that resemble each other. Agglomerative clustering is one family of these methods, and its strategy is unusual: instead of splitting one big group into smaller ones, it begins at the opposite extreme and builds larger groups up from the bottom.
Agglomerative clustering is a collection of algorithms that share one underlying principle. Every algorithm in the family starts in the same place: each data point is treated as a cluster containing only itself. From that starting arrangement, the algorithm repeatedly looks for the two clusters that are most similar to each other and merges them into one. After each merge there is one fewer cluster, and the process continues.
It helps to notice what the procedure never does. It does not start with one big cluster and cut it apart, and it does not need a guess about the final groups before it begins. The final grouping emerges from the sequence of merges themselves. This is why the word agglomerative, meaning gathering together, fits the method well.
Merging Pairs Until a Final Grouping
Each step of agglomerative clustering takes the two most similar current clusters and merges them. Because each merge reduces the cluster count by one, the sequence of merges is a record of how the groups were built. The example below uses five points labeled A through E. The merge order is illustrative, chosen only to show the mechanism.
Tracing merges for five points down to two clusters
Five points A, B, C, D, and E each start as their own cluster. Suppose the most similar pairs are merged in the order shown, and the algorithm stops when two clusters remain. What is the final grouping?
- 1Start
Five clusters exist: A, B, C, D, E. Each contains only itself.
- 2Merge 1
A and B are the most similar pair, so they merge into cluster AB. Four clusters remain.
- 3Merge 2
D and E are the next most similar pair, so they merge into cluster DE. Three clusters remain: AB, C, DE.
- 4Merge 3
C is most similar to DE among the current pairs, so C merges with DE into CDE. Two clusters remain: AB and CDE.
- 5Stop
The target number of clusters is 2, so the stopping criterion is met and no further merges happen.
The final grouping is AB and CDE. The merge history, not a preset guess, produced these groups.
Read the tree from the bottom up, which mirrors the build-up process. The leaves are the original singleton points. Each internal node records one merge, and the stopping point is the horizontal line that cuts the tree where two clusters remain. The example merges happen to be fixed here, but in the algorithm they are chosen by a similarity measure, which the linkage criterion defines.
The Stopping Criterion and the Cluster Count
Merging cannot go on forever, so the algorithm needs a rule for when to halt. That rule is called the stopping criterion. The source describes the general idea as merging the most similar pair of clusters until some stopping criterion is satisfied, which means the criterion is what ends the loop.
In scikit-learn, the implementation discussed here uses the number of clusters as its stopping criterion. You tell the algorithm how many clusters you want to end up with, and it keeps merging the most similar clusters until only that number is left. The number is therefore an input you supply, not something the algorithm discovers on its own.
Because each merge reduces the cluster count by one, reaching a target count requires a predictable number of merges. Starting from n singleton points and aiming for k clusters takes exactly n minus k merges. The stopping criterion therefore fixes both the endpoint and how much merging happens along the way.
What a Linkage Criterion Decides
Choosing the two most similar clusters requires a way to score similarity, and this is where linkage criteria come in. A linkage criterion defines how most similar is measured between two existing clusters. The source stresses that there are several linkage criteria, and that the exact meaning of most similar depends on which one you select.
An important detail is that a linkage criterion always compares two clusters that already exist. It is not a rule for comparing a single point to a single point in isolation at the moment of merging. Early in the process, clusters may still be single points, but the linkage rule still operates on pairs of current clusters. Each time a merge happens, the set of clusters changes, and the comparisons are made again on the new set.
scikit-learn lets you select the linkage criterion as a setting when you configure the model. The options differ in how they define similarity between clusters, so the same data can produce different merge sequences depending on the one you pick. The source does not spell out the formal definition of each option in this material, so when you choose one, check its documentation for the exact rule.
Because the comparison is recalculated after every merge, a choice that looked reasonable at the first step can still change which clusters are merged later. The merge sequence is a chain of decisions, and each decision uses the clusters as they exist at that moment.
Mistakes Beginners Make With Clustering Methods
Common mistake Assuming k-means reconstructs a face by blending several centers
What it looks likeExpecting a test face's reconstruction to look like a mix of two or three training faces.
Why it failsThe k-means path selects one closest center by index and returns it. No blending happens at any stage.
Do this insteadRead the k-means reconstruction as a lookup of one stored center, and the PCA or NMF reconstruction as a weighted combination of several components.
Common mistake Treating the random seed as a detail that does not affect results
What it looks likeChanging random_state and expecting identical cluster centers and reconstructions.
Why it failsThe result of k-means depends on random initialization, so a different seed can give different centers.
Do this insteadFix random_state for repeatable runs, and rely on scikit-learn's several initializations to reduce dependence on any single starting point.
Common mistake Expecting agglomerative clustering to discover the number of clusters
What it looks likeRunning the algorithm and assuming it will report how many natural groups exist.
Why it failsIn scikit-learn the number of clusters is the stopping criterion you supply, so the algorithm merges until that number remains.
Do this insteadDecide the target cluster count as an input, and evaluate whether it matches the structure you expect in the data.
Common mistake Thinking a linkage criterion compares single points at the moment of merging
What it looks likeBelieving the rule only looks at two individual points even after clusters have grown.
Why it failsA linkage criterion always compares two clusters that already exist, and it is recalculated as the set of clusters changes.
Do this insteadRemember that every comparison is between current clusters, which may themselves contain many points.
Common mistake Using vector quantization features as if they were a faithful reconstruction of the input
Practice Tracing a Merge Sequence
Six points P, Q, R, S, T, and U each start as their own cluster. Suppose the merges happen in this order: P with Q, then S with T, then R with ST, then PQ with RST. If the stopping criterion is 3 clusters, which clusters remain at the end, and how many merges happened?
Hints
- Start by counting the clusters: six singletons means six clusters before any merge.
- Each merge reduces the cluster count by exactly one. Track how many merges you have applied when the count reaches 3.
- Stop as soon as the count equals the target, even if more merges would be possible.
Explain, in your own words, why a linkage criterion must be recalculated after every merge. Use the phrase existing clusters in your answer.
Hints
- Think about what changes after one merge: which clusters exist and what pairs can be compared.
- A merged cluster is a new object that the linkage rule must now score against the others.
Where DBSCAN Fits in This Article
Key Takeaways
- k-means reconstructs a test face by finding the closest stored cluster center and returning that single center, while PCA and NMF build reconstructions by combining several learned components.
- Vector quantization replaces each point with its cluster assignment, producing a feature vector with a 1 at the assigned cluster and 0 elsewhere, and distances to centers can add more information.
- Agglomerative clustering starts with every point as its own cluster and repeatedly merges the two most similar clusters, so the final grouping emerges from the merge sequence.
- The stopping criterion, which in scikit-learn is the number of clusters you specify, ends merging, and the linkage criterion defines similarity between two existing clusters.