Skip to content
AI360XpertBeta
Core ML

Visual explainer

Clustering

Trace centroid assignment, inertia, density connectivity and feature scaling, then see how a bridge can merge two intended groups.

Four unlabelled observations can suggest two groups.
Four unlabelled observations can suggest two groups.

Clustering groups observations according to a chosen representation and rule. Different rules can produce different useful groupings; coordinates alone do not reveal a unique semantic truth. Start with four synthetic observations, 1,2,9,10, on a common number line. The gap from 2 to 9 is 7, but there are no supplied target labels.

Compare Distances

Point 9 joins center 2 because 7 is smaller than 8.
Point 9 joins center 2 because 7 is smaller than 8.

Use k-means as our first concrete grouping rule. The diamonds mark initial centers 1 and 2; the circle is observation 9. Its distances are |9−1|=8 and |9−2|=7. The solid caliper wins. The centers were chosen for illustration, so a poor initial assignment is unsurprising.

The official clustering guide describes k-means as nearest-center assignment alternating with mean updates. Its objective is the sum of squared distances to assigned centers. Density methods instead depend on local connectivity and core points. A grouping rule encodes assumptions about what should count as similar.

Assign Every Observation

Seeds 1 and 2 initially split one point from three.
Seeds 1 and 2 initially split one point from three.

The equidistant boundary is (1+2)/2=1.5. Observation 1 joins the first group, shown open; 2,9,10 join the second, shown solid. All number-line frames use 48 SVG units per data unit. A tie would go to the first center in this example; no point ties here.

For fixed centers, choosing a nearest center minimizes each observation's squared-distance contribution. The initial total is 0+0+7²+8²=113. Membership is an algorithmic assignment, not a known class.

Replace Centers With Means

A centroid moves to the mean of its assigned members.
A centroid moves to the mean of its assigned members.

Focus on the second group, retaining its members 2,9,10. Their mean is (2+9+10)/3=7, so the center moves directly from 2 to 7. Group 1 contains only 1 and stays centered at 1. The diamond above the ruler locates the new mean; it is not another observation.

With assignments held fixed, squared error becomes (2−7)²+(9−7)²+(10−7)²=25+4+9=38. A mean minimizes this fixed group's squared error. It need not coincide with a sample.

Reassign And Settle

Reassignment produces two pairs with centers 1.5 and 9.5.
Reassignment produces two pairs with centers 1.5 and 9.5.

Centers 1 and 7 move the boundary to 4. Observation 2 switches to the first group; the new groups are 2 and 10. Their means become 1.5 and 9.5, placing the final boundary at 5.5. No observation switches again.

Each observation lies .5 from its center, so total squared error is 4×.5²=1. This verifies the result for this fixture. It does not prove k-means recovers the intended groups on arbitrary data.

More Groups Reduce Inertia

More centers lower optimal inertia even without better meaning.
More centers lower optimal inertia even without better meaning.

The plot shows the globally optimal inertia for each K on our four observations; it does not describe arbitrary local fits. K=1 has mean 5.5 and error 20.25+12.25+12.25+20.25=65. K=2 gives 1. K=3 can split one pair into singletons, leaving .5. K=4 places one center on every point, giving zero.

All heights use the same linear scale of three SVG units per squared-error unit. The tiny differences at the bottom are real; labels identify them without exaggerating the geometry. A bend near K=2 suggests a compact summary here. The elbow is a heuristic, not a certificate of a correct group count. More centers buy smaller residuals at the cost of a more complex partition.

Curved Groups Change The Question

A centroid bisector can cut both curved groups.
A centroid bisector can cut both curved groups.

Switch explicitly to eight two-dimensional points. The upper chain is (0,2),(1,3),(2,3),(3,2); the lower is (0,0),(1,−1),(2,−1),(3,0). Plots now use 60 SVG units per feature unit in both directions. Solid and open circles track the two intended chains; dashed guides show their order.

Candidate centers (.5,1) and (2.5,1) have the exact vertical bisector x=1.5. Nearest-center assignment splits both chains left versus right. These are candidate centers, not a claimed converged optimum. The official assumptions example illustrates why centroid geometry can mismatch elongated or uneven groups. A different family of methods may better match the intended relationship.

Follow Local Connections

Local connectivity can follow each curved group.
Local connectivity can follow each curved group.

Keep those exact eight coordinates and the same scale. Adjacent chain distances are √2,1,√2; the smallest cross-chain distance is 2. With DBSCAN radius eps=1.5 and min_samples=2, counting the observation itself, every point is a core point. Adjacent points connect, while no edge crosses between chains. Two core-connected components result.

The short gaps around the circles keep connections traceable; edge endpoints do not change the underlying measured distances. These deliberately permissive settings make the example easy to verify. They are not a recommended universal density threshold. See the DBSCAN visual for core, border and noise cases.

Representation Changes Similarity

Rescaling one feature can change the nearest neighbor.
Rescaling one feature can change the nearest neighbor.

Switch to a rectangle A=(0,0), B=(0,2), C=(3,0), D=(3,2). Both panels use 50 SVG units per plotted feature unit. In raw coordinates, A is closer to B: distance 2 versus distance 3 to C.

Multiply y by 2 before computing distances. AB becomes 4 while AC remains 3, so C becomes the nearer neighbor. The highlighted edge changes because the representation changes, not because the observations acquire new target labels. Choose units, scaling and distance measures according to the intended meaning of similarity.

Where It Breaks

A core-point bridge can merge intended groups.
A core-point bridge can merge intended groups.

Return to the eight chain points, eps=1.5 and min_samples=2. Add the diamond at (0,1). It lies 1 from both (0,2) and (0,0), so it is core and joins the two previously separate core components. All nine observations become one density-connected group. The filled/open circles retain their original chain identities so the unwanted merger remains visible.

Density connectivity solved one shape problem but now joins groups through a bridge. A lower inertia, a clean plot or a connected component cannot establish semantic correctness. Compare groupings with domain evidence and sensitivity to representation and parameters. Use the k-means visual for the centroid algorithm's update details.

The Quick Version

  • Clustering needs a representation and a grouping rule.
  • A nearer center wins the k-means assignment.
  • Initial seeds can create lopsided memberships.
  • Means minimize squared error for fixed memberships.
  • Reassignment can move observations between groups.
  • More centers reduce optimal inertia without proving meaning.
  • Centroid bisectors can cut intended curved groups.
  • Density connectivity can follow non-convex groups.
  • Feature scaling can reverse nearest-neighbor relationships.
  • A core-point bridge can merge intended density groups.