Two Different Ways to Form Clusters
Hierarchical clustering and k-means both organize observations into groups, but they do so through different processes. An agglomerative hierarchical method repeatedly merges clusters to build a nested sequence of partitions. K-means instead starts with a chosen number of clusters and repeatedly reassigns observations and updates cluster centers. These differences matter: the methods can give different answers on the same data, and each is sensitive to different modeling choices.
The main comparison is not simply “which method is better?” Ask what output is needed, whether the observations have a meaningful centroid geometry, how much the results should depend on initialization, and whether extreme observations may dominate the analysis. Both methods can be affected by variable scaling, but their mechanisms and assumptions differ.
What Must Be Chosen in Advance?
For standard k-means, the number \(K\) of clusters must be specified before fitting. The algorithm returns a partition with exactly \(K\) nonempty clusters, provided the data and implementation permit such a solution. Choosing \(K\) is therefore part of using the method; k-means itself does not produce a full hierarchy of solutions for every possible cluster count.
Agglomerative hierarchical clustering can build a complete merge tree without selecting the final number of clusters in advance. A user may then choose a number of clusters by cutting the tree, or use a dissimilarity threshold. This postpones the choice, rather than eliminating it: interpreting a particular partition still requires deciding where or how to cut.
The distinction is useful when the analysis needs a record of how groups combine at multiple levels. A dendrogram provides that nested history. K-means provides a single fitted partition for the selected \(K\), not a sequence of nested partitions. Its solution for \(K=3\) need not contain the solution for \(K=4\) as a refinement.
| Feature | Agglomerative hierarchical clustering | K-means |
|---|---|---|
| Number of clusters | Can build a tree first; choose a cut to obtain a partition | Specify \(K\) before fitting |
| Main operation | Merge two existing clusters; earlier merges are retained | Reassign observations and update cluster means |
| Distance geometry | Uses a chosen dissimilarity and linkage; compatibility depends on the choices | Uses squared Euclidean distances to cluster means in its standard form |
| Initialization | No starting cluster centers; ties and choices of distance or linkage can still affect the tree | Starting centers can affect the final solution |
| Extreme observations | Effect depends on the linkage and dissimilarity | Can have strong influence because distances are squared and means can shift |
K-Means: Reassignment and Centroid Geometry
Suppose the observations are vectors \(x_1,\ldots,x_n\), with each \(x_i\) having \(p\) numerical features. For a partition into \(K\) nonempty clusters \(C_1,\ldots,C_K\), the mean of cluster \(C_j\) is \(\bar{x}_j\). The standard k-means objective is the within-cluster sum of squares:
The standard iterative algorithm alternates two operations. Given current centers, it assigns each observation to a nearest center using squared Euclidean distance. Given those assignments, it replaces each center by the mean of its assigned observations. Each operation does not increase the objective: nearest-center assignment minimizes each observation’s squared distance among the current centers, and the mean minimizes the sum of squared distances within a fixed cluster. The process stops when assignments or centers no longer change, or when a specified stopping rule is met.
The cross-product term vanishes because \(\sum_i(x_i-\bar{x})=0\). The second term on the right is nonnegative and is zero only when \(c=\bar{x}\). Therefore the mean minimizes the sum of squared distances. This explains why updating a cluster center to its mean cannot increase the k-means objective while assignments are fixed.
The objective is tied to Euclidean geometry and squared distances. A numerical feature measured on a much larger scale can dominate the distance unless features are appropriately scaled for the application. Standard k-means is not a general method for arbitrary dissimilarity matrices: it needs meaningful numerical means and Euclidean distances to those means.
Hierarchical Clustering: Merges Are Retained
In agglomerative hierarchical clustering, once two clusters have merged, that merge is not undone at a later step. The rule for choosing the next pair depends on the specified linkage and dissimilarity. As established in the earlier material on linkage rules, single, complete, average, and Ward linkage can favor different merge sequences and cluster shapes. A dendrogram records those successive merges and allows a later cut to produce a partition.
This creates a different kind of dependence from k-means initialization. A hierarchical algorithm does not need initial centers, but its tree depends on the chosen distance and linkage. Tied distances can also allow nonunique merge sequences. In contrast, k-means can change assignments after each center update, but the final partition can depend on the starting centers. Neither process guarantees that every plausible modeling choice leads to the same groups.
Worked Example: Same Observations, Different Fitting Processes
Consider four one-dimensional observations \(-2,0,7,9\). Fit k-means with \(K=2\), starting centers \(-2\) and \(9\). Separately, consider agglomerative complete linkage with ordinary absolute distance, then cut its tree to two clusters. Compare the steps and the resulting within-cluster sum of squares for the k-means partition.
K-means assignment. The initial centers are \(-2\) and \(9\). Their midpoint is \((-2+9)/2=3.5\), so \(-2\) and \(0\) are assigned to the first center, while \(7\) and \(9\) are assigned to the second. The updated means are \(\bar{x}_1=(-2+0)/2=-1\) and \(\bar{x}_2=(7+9)/2=8\). The midpoint of these updated centers is \((-1+8)/2=3.5\), so the assignments remain unchanged. The algorithm has converged for this initialization.
Check the objective. The first cluster contributes \((-2-(-1))^2+(0-(-1))^2=1+1=2\). The second contributes \((7-8)^2+(9-8)^2=1+1=2\). Thus the k-means WSS is \(2+2=4\).
Complete-linkage merges. The pairwise distances are \(2\) between \(-2\) and \(0\), \(7\) between \(0\) and \(7\), and \(2\) between \(7\) and \(9\); all other pairwise distances are larger. The two nearest pairs therefore merge at distance \(2\), producing \(\{-2,0\}\) and \(\{7,9\}\). The complete-linkage distance between these clusters is the largest cross-cluster distance, which is \(|-2-9|=11\). Cutting before that final merge gives the same two groups as k-means here. The match is a feature of these data and choices, not a guarantee that the methods agree.
Method and mistake avoided. The k-means calculation alternates assignment and mean updates; complete linkage compares cluster-to-cluster distances. Do not treat a k-means center as though it were a hierarchical cluster, or assume that agreement on one data set means the procedures are equivalent.
Initialization and Local Solutions
K-means reduces its objective through alternating steps, but the objective is not generally minimized globally by every run. Different initial centers can lead to different final assignments and different values of WSS. Multiple starts can help reveal this sensitivity, but they do not turn a single run into a proof of a global minimum.
Hierarchical clustering does not have this particular dependence on starting centers because it starts with singleton clusters. It is not thereby free of choices: the selected distance, linkage, tie handling, and cut all matter. In particular, because merges are retained, a locally appealing early merge can constrain all later partitions in that tree.
Worked Example: Two K-Means Initializations, Two Outcomes
Use observations \(0,2,3,10\) and \(K=2\). Compare a k-means run initialized at centers \(0\) and \(2\) with one initialized at \(0\) and \(10\). Show that the first run can stop at a higher-WSS solution. As a reference, also identify the two-cluster partition produced by single linkage when cut after the merge at distance \(2\).
Start at \(0\) and \(2\). The initial midpoint is \(1\). Assign \(0\) to the center \(0\), and \(2,3,10\) to the center \(2\); the point \(2\) is at distance \(0\) from that center. The new means are \(0\) and \((2+3+10)/3=5\). The midpoint is \(2.5\), so the next assignment is \(\{0,2\}\) and \(\{3,10\}\). The updated means are \(1\) and \((3+10)/2=6.5\). Their midpoint is \(3.75\), so these assignments remain unchanged.
The WSS at this solution is \((0-1)^2+(2-1)^2+(3-6.5)^2+(10-6.5)^2=1+1+12.25+12.25=26.5\).
Start at \(0\) and \(10\). The midpoint is \(5\), so the initial groups are \(\{0,2,3\}\) and \(\{10\}\). Their means are \(5/3\) and \(10\). The new midpoint is \((5/3+10)/2=35/6\), which remains between \(3\) and \(10\); the assignments do not change. The WSS is \((0-5/3)^2+(2-5/3)^2+(3-5/3)^2+(10-10)^2 =25/9+1/9+16/9+0=42/9=14/3\), approximately \(4.667\).
Single-linkage reference. The closest pair is \(2,3\), at distance \(1\). The single-linkage distance from \(\{2,3\}\) to \(0\) is \(2\), so these observations merge at distance \(2\). The remaining distance from \(\{0,2,3\}\) to \(10\) is \(7\). A cut after the distance-\(2\) merge gives \(\{0,2,3\}\) and \(\{10\}\), matching the better k-means partition in this example.
Check. The second k-means run has lower WSS, \(14/3<26.5\). Both runs stopped with unchanged assignments, but they did not reach solutions of equal quality. The single-linkage result follows its own merge rule; it is not a k-means initialization.
Distance Geometry and Outliers
K-means favors groups that can be represented by means and judged by squared Euclidean distance. It is therefore most interpretable when the features are quantitative and a mean is meaningful. A very distant observation can have a large effect: squared distance gives large residuals disproportionate weight, and the mean itself can move toward the extreme value.
Hierarchical clustering can use different dissimilarities and linkage rules, subject to the requirements of the particular method. Its response to an outlier is not uniform. A distant observation may remain a separate branch until a large merge height, while other linkages can let it affect cluster-to-cluster distances differently. The earlier material on outliers under different linkage rules is important here: “hierarchical clustering is robust to outliers” is not a general conclusion.
Both approaches are affected by feature scaling when distance calculations are used. For example, if one feature is measured in thousands and another in tenths, the first may dominate ordinary Euclidean distances. Rescaling should reflect the meaning and purpose of the analysis, not be applied mechanically; standardizing variables changes the geometry and can change the resulting clusters.
Worked Example: An Extreme Value and the Choice of Method
Consider the one-dimensional observations \(0,1,2,3,20\). For k-means with \(K=2\), start with centers \(0\) and \(20\). For comparison, use single-linkage hierarchical clustering with absolute distance and cut to two clusters. Calculate the k-means center and WSS, and identify where the hierarchical cut occurs.
K-means. The initial midpoint is \((0+20)/2=10\), so \(0,1,2,3\) join the first cluster and \(20\) is alone. The updated means are \((0+1+2+3)/4=6/4=1.5\) and \(20\). Their midpoint is \(10.75\), so the assignments stay the same. The WSS is \((0-1.5)^2+(1-1.5)^2+(2-1.5)^2+(3-1.5)^2+(20-20)^2 =2.25+0.25+0.25+2.25+0=5\).
Single linkage. The consecutive gaps among the observations are \(1,1,1,17\). Single linkage successively joins the four observations \(0,1,2,3\) at distance \(1\); the distance from that cluster to \(20\) is \(17\). A cut after the low observations have joined but before the final merge gives \(\{0,1,2,3\}\) and \(\{20\}\).
Interpretation and trap. Both methods isolate \(20\) in this particular two-cluster solution, but they got there by different rules. Do not infer that every linkage isolates an outlier, or that k-means is unaffected because this initialization produced a singleton. The large separation and the selected \(K\) are doing substantial work in this example.
Choosing Between the Methods
Choose hierarchical clustering when a nested merge history or a flexible dissimilarity-and-linkage specification is central to the task, and when the cost of building and storing the hierarchy is acceptable for the data size. Choose k-means when a specified number of compact, mean-representable groups is useful and squared Euclidean distance is appropriate. These are modeling considerations, not guarantees that either method will discover uniquely “true” groups.
A practical comparison should hold the observations and feature preprocessing fixed, then examine whether the resulting partitions answer the intended question. With k-means, compare multiple initializations and inspect the resulting WSS and assignments. With hierarchical clustering, state the dissimilarity, linkage, and cutting rule. In both cases, assess whether scaling and extreme observations are driving the result.
- Use k-means only when cluster means and the squared Euclidean objective are meaningful for the features.
- Use a hierarchical tree when nested groupings or a choice of cut after fitting is valuable.
- Do not equate “no starting centers” with “no modeling choices”; linkage and distance define the hierarchical procedure.
- Do not equate “the algorithm converged” with “the k-means solution is globally optimal.”
Check Your Understanding
For each problem, identify the method-specific choice or calculation that drives the result.
- For observations \(1,3,8,10\), k-means uses \(K=2\) and starts at centers \(2\) and \(9\). State the final groups, updated centers, and WSS. Under complete linkage with absolute distance, what two groups result after merging the two nearest pairs? (Answer: K-means groups \(\{1,3\}\) and \(\{8,10\}\), centers \(2\) and \(9\), WSS \(4\); complete linkage gives the same two groups after the distance-2 pair merges.)
- For observations \(0,2,3,10\), a \(K=2\) k-means run starts at \(0\) and \(2\). Give its converged groups and WSS, then compare with the groups and WSS from starting at \(0\) and \(10\). (Answer: The first start converges to \(\{0,2\}\), \(\{3,10\}\) with WSS \(26.5\); the second gives \(\{0,2,3\}\), \(\{10\}\) with WSS \(14/3\), approximately \(4.667\).)
- Single linkage with absolute distance is applied to \(0,1,4,10\). List the successive merge distances and the groups at a cut that leaves two clusters. (Answer: Merge \(0\) with \(1\) at distance \(1\), then merge \(4\) with \(\{0,1\}\) at distance \(3\); the next merge is at distance \(6\), so the two groups are \(\{0,1,4\}\) and \(\{10\}\).)
- For \(2,3,4,5,30\), find the one-cluster k-means center. Then find the mean of \(2,3,4,5\) without \(30\), and explain why the difference matters for k-means. (Answer: The one-cluster mean is \(44/5=8.8\); without \(30\), the mean is \(14/4=3.5\). The extreme value shifts the mean and contributes a large squared distance.)
- Four observations have features \((0,0),(1,0),(0,100),(1,100)\). Under ordinary Euclidean distance, which pairwise gap is smaller: between points differing only in the first feature or between points differing only in the second? If the second feature is divided by \(100\), what happens to those two gaps? (Answer: The first-feature gap is \(1\), while the second-feature gap is \(100\); after division by \(100\), both gaps are \(1\). Scaling therefore changes the distance geometry and can change either method’s clustering.)