K-Means vs. Hierarchical Clustering

Last Updated July 20, 2026

K-Means starts with a chosen number of clusters and repeatedly moves their centroids as points are reassigned. Hierarchical Clustering builds a nested cluster tree by repeatedly merging or splitting groups.

  • Use K-Means when you need one flat partition, already have a reasonable value of kk, and expect compact groups around centroids.
  • Use Hierarchical Clustering when you want to explore smaller groups inside larger ones before selecting a final clustering level.
K-Means refines one flat partition; Hierarchical Clustering builds several nested levels.

Head-to-Head Showdown

Cluster Count Choice

K-Means: Set kk before fitting

Hierarchical Clustering: Often chosen from hierarchy

The Implication: K-Means requires one value of kk before centroid refinement begins. Hierarchical Clustering can build several grouping levels first and let the final cluster count be selected by cutting the hierarchy, although some implementations can also stop at a requested count.

Grouping Process

K-Means: Reassigns points

Hierarchical Clustering: Merges or splits groups

The Implication: K-Means may move a point to another cluster when updated centroids change which center is nearest. Agglomerative Hierarchical Clustering permanently merges clusters, while divisive Hierarchical Clustering begins with a larger group and repeatedly splits it.

Result Structure

K-Means: One flat partition

Hierarchical Clustering: One nested hierarchy

The Implication: K-Means returns one grouping for the selected value of kk. Hierarchical Clustering preserves parent-child relationships across several grouping levels, usually displayed through a dendrogram.

Selection Criteria

Scenario:A retail team needs exactly four numeric customer segments and expects each segment to form a compact group around an average spending profile.

Choose K-Means:K-Means directly refines four centroid-based clusters, matching the known value of kk and the expected compact group structure. Hierarchical Clustering would preserve additional nested levels that are unnecessary when the team only needs one flat four-cluster result.

Scenario:A research team does not yet know how many meaningful patient groups exist and wants to inspect subgroups inside larger groups.

Choose Hierarchical Clustering:Hierarchical Clustering preserves several grouping levels in one tree, allowing researchers to inspect the dendrogram before selecting a final cut. K-Means requires a separate value of kk for each run and does not preserve nested parent-child relationships.

Scenario:An engineering team must divide several million numeric sensor readings into exactly six compact operating-pattern groups.

Choose K-Means:K-Means uses repeated point-to-centroid assignments and is generally more practical for large datasets when compact centroid-based groups are suitable. Standard agglomerative Hierarchical Clustering usually requires substantially more pairwise work and memory unless approximation, sampling, or specialized constraints are used.

Side By Side Trace

Both methods cluster the same six points using Height and Weight. K-Means uses k=2k=2 with supplied starting centroids C1=P1=(185,72)C_1=P1=(185,72) and C2=P2=(170,56)C_2=P2=(170,56), while Hierarchical Clustering uses agglomerative single linkage with Euclidean distance. These are controlled teaching settings rather than universal requirements for either family. The hierarchy is cut when two clusters remain, and both methods happen to produce the same final groups through different calculations.

Data PointHeightWeight
P118572
P217056
P316860
P417968
P518272
P618877

Step 1: Prepare the Clustering Process

K-Means

K-Means fixes k=2k=2 before clustering and uses the supplied starting centroids C1=P1=(185,72)C_1=P1=(185,72) and C2=P2=(170,56)C_2=P2=(170,56). Every point will be assigned to whichever centroid has the smaller Euclidean distance.

Hierarchical Clustering

Agglomerative Hierarchical Clustering begins with six separate clusters: {P1}\{P1\}, {P2}\{P2\}, {P3}\{P3\}, {P4}\{P4\}, {P5}\{P5\}, and {P6}\{P6\}. It will repeatedly merge the two clusters with the smallest single-linkage distance until two clusters remain.

Step 2: Point of Divergence

K-Means

Using the starting centroids, the distances to C1C_1 and C2C_2 are: P1:(0,21.932)P1:(0,21.932), P2:(21.932,0)P2:(21.932,0), P3:(20.809,4.472)P3:(20.809,4.472), P4:(7.211,15)P4:(7.211,15), P5:(3,20)P5:(3,20), and P6:(5.831,27.659)P6:(5.831,27.659). The nearest-centroid assignments are therefore C1={P1,P4,P5,P6}C_1=\{P1,P4,P5,P6\} and C2={P2,P3}C_2=\{P2,P3\}. Recalculating the means gives C1=(185+179+182+1884,72+68+72+774)=(183.5,72.25)C_1=\left(\frac{185+179+182+188}{4},\frac{72+68+72+77}{4}\right)=(183.5,72.25) and C2=(170+1682,56+602)=(169,58)C_2=\left(\frac{170+168}{2},\frac{56+60}{2}\right)=(169,58).

Hierarchical Clustering

Hierarchical Clustering compares distances between the individual points. The closest pair is P1P1 and P5P5, with d(P1,P5)=(185182)2+(7272)2=3d(P1,P5)=\sqrt{(185-182)^2+(72-72)^2}=3, so it permanently merges them into {P1,P5}\{P1,P5\}. K-Means groups every point around centroids and may later revise assignments, while agglomerative Hierarchical Clustering commits to one nearest-cluster merge at a time and does not undo it.

Step 3: Continue Refinement or Merging

K-Means

Using the updated centroids (183.5,72.25)(183.5,72.25) and (169,58)(169,58), the distances remain closest to the same clusters: P1:(1.521,21.260)P1:(1.521,21.260), P2:(21.126,2.236)P2:(21.126,2.236), P3:(19.756,2.236)P3:(19.756,2.236), P4:(6.190,14.142)P4:(6.190,14.142), P5:(1.521,19.105)P5:(1.521,19.105), and P6:(6.543,26.870)P6:(6.543,26.870). The assignments stay {P1,P4,P5,P6}\{P1,P4,P5,P6\} and {P2,P3}\{P2,P3\}, so the centroids do not change and K-Means converges after two iterations.

Hierarchical Clustering

The next closest pair is P2P2 and P3P3, with d(P2,P3)=(170168)2+(5660)2=204.472d(P2,P3)=\sqrt{(170-168)^2+(56-60)^2}=\sqrt{20}\approx4.472, producing {P2,P3}\{P2,P3\}. Single linkage then gives d({P1,P5},{P4})=min(d(P1,P4),d(P5,P4))=min(7.211,5)=5d(\{P1,P5\},\{P4\})=\min(d(P1,P4),d(P5,P4))=\min(7.211,5)=5, so P4P4 joins to form {P1,P4,P5}\{P1,P4,P5\}. Finally, d({P1,P4,P5},{P6})=min(5.831,12.728,7.810)=5.831d(\{P1,P4,P5\},\{P6\})=\min(5.831,12.728,7.810)=5.831, so P6P6 joins and exactly two clusters remain.

Step 4: Produce Final Clusters

K-Means

K-Means produces Cluster C1={P1,P4,P5,P6}C_1=\{P1,P4,P5,P6\} with centroid (183.5,72.25)(183.5,72.25) and Cluster C2={P2,P3}C_2=\{P2,P3\} with centroid (169,58)(169,58). It reached these groups through two rounds of point-to-centroid assignment and centroid recalculation.

Hierarchical Clustering

Hierarchical Clustering produces {P1,P4,P5,P6}\{P1,P4,P5,P6\} and {P2,P3}\{P2,P3\} after four irreversible nearest-cluster merges, and the hierarchy is cut when these two clusters remain. It used minimum member-to-member distances rather than calculating or updating centroids.

Final Result

K-Means:K-Means began with k=2k=2 and the supplied centroids P1=(185,72)P1=(185,72) and P2=(170,56)P2=(170,56). It assigned all six points, updated the centroids to (183.5,72.25)(183.5,72.25) and (169,58)(169,58), and stopped after the second iteration kept the same assignments. Its final clusters were {P1,P4,P5,P6}\{P1,P4,P5,P6\} and {P2,P3}\{P2,P3\}.

Hierarchical Clustering:Hierarchical Clustering began with six one-point clusters and used agglomerative single-linkage merging. It merged P1P1 with P5P5 at 3.0003.000, P2P2 with P3P3 at 4.4724.472, P4P4 with {P1,P5}\{P1,P5\} at 5.0005.000, and P6P6 with {P1,P4,P5}\{P1,P4,P5\} at 5.8315.831, then cut the hierarchy at two clusters. Both methods produced {P1,P4,P5,P6}\{P1,P4,P5,P6\} and {P2,P3}\{P2,P3\}, but K-Means reached them through centroid assignment and recalculation while Hierarchical Clustering reached them through permanent nearest-cluster merges.

Common Pitfalls & Exam Mistakes

  • Assuming both methods need the cluster count first.

    The Mistake: Students believe K-Means and Hierarchical Clustering must both receive the final number of clusters before any grouping begins.

    Why It's Wrong: K-Means requires kk before centroid refinement starts. Hierarchical Clustering can build several grouping levels first and select a flat result later, although some implementations may stop when a requested cluster count is reached.

  • Treating K-Means assignments as permanent merges.

    The Mistake: Students assume that once K-Means places a point in a cluster, the point can never move again.

    Why It's Wrong: K-Means may reassign points whenever updated centroids change the nearest center. Agglomerative Hierarchical Clustering instead preserves each completed merge while building progressively larger clusters.

  • Assuming Hierarchical Clustering always uses centroids.

    The Mistake: Students believe Hierarchical Clustering measures cluster distance exactly as K-Means measures point-to-centroid distance.

    Why It's Wrong: K-Means assigns points according to centroid distance. Hierarchical Clustering uses a selected linkage rule, which may compare nearest members, farthest members, average distances, variance increases, or other cluster relationships.

Comparative Analysis

AttributeK-MeansHierarchical Clustering
Core MechanismAssign and update centroidsMerge or split clusters
Cluster CountRequired before fittingOften chosen from hierarchy
Output StructureOne flat partitionNested cluster tree
Membership ChangesPoints may be reassignedAgglomerative merges stay fixed
Key SensitivityInitialization and kkDistance and linkage
Typical ScaleBetter for large datasetsUsually smaller datasets

Common Questions & Edge Cases

  • Is K-Means usually the more direct choice when kk is fixed and compact centroid-based clusters are expected?

    Yes. K-Means is designed to refine one selected number of centroid-based clusters and usually scales better for this setting. Hierarchical Clustering may still be useful when nested relationships or several possible grouping levels are also important.

  • Can one K-Means run replace Hierarchical Clustering when nested grouping levels must be preserved?

    No. One K-Means run produces only one flat partition for its selected value of kk. Running K-Means repeatedly with different values creates separate partitions but does not preserve the parent-child merge structure contained in one hierarchy.

  • Can K-Means and Hierarchical Clustering use the same data and distance measure?

    Yes. Both can use the same numeric features and Euclidean distance when that measure fits the problem. K-Means applies it to point-to-centroid assignment, while Hierarchical Clustering uses it through a selected linkage rule between clusters.

  • Can K-Means and Hierarchical Clustering produce the same final clusters?

    Yes. Clearly separated data can lead both methods to the same flat partition. Matching clusters do not mean matching mechanics because K-Means refines centroids while Hierarchical Clustering builds groups through merges or splits.

Explore the Algorithms in Action

Open the theory pages or try the interactive solvers for the algorithms compared above.