K-Means Clustering Visualizer

Click to place data points, choose how many clusters, and watch K-Means group them step by step.

Advertisement

Click anywhere on the canvas to add a data point.

0Iteration
0Points
Centroid Movement
Status
Advertisement

How K-Means Clustering Works

K-Means groups data points into K clusters through a simple two-step loop repeated until convergence. First, the assignment step: every point gets assigned to its nearest centroid (cluster center), using standard Euclidean distance. Second, the update step: each centroid moves to the average position of all points currently assigned to it. These two steps repeat, assign, then update, until the centroids stop moving meaningfully, at which point the algorithm has converged.

Why Starting Position Matters

K-Means is sensitive to where the centroids start. Click "Random Points" and "Step" a few times, you'll typically see reasonable convergence, but occasionally a poor random initialization causes the algorithm to settle into a suboptimal clustering, two genuinely separate groups get merged, or one group splits awkwardly. In production, this is why K-Means is usually run multiple times with different random initializations (a technique called K-Means++ improves initial centroid placement specifically to reduce this problem), keeping whichever run achieved the lowest total distance from points to their assigned centroids.

Choosing the Right Number of Clusters (K)

K-Means requires you to specify K upfront, the algorithm doesn't discover the "right" number of clusters on its own. Choosing K too low forces genuinely distinct groups to merge, choosing K too high splits a genuine single group into artificial subgroups. In practice, the "elbow method" is commonly used, running K-Means across a range of K values and plotting total within-cluster distance against K, looking for the point where increasing K stops meaningfully reducing that distance, the "elbow" in the curve.

Real-World Applications

  • Customer segmentation: grouping customers by purchasing behavior for targeted marketing.
  • Image compression: reducing an image's color palette by clustering similar colors together.
  • Document clustering: grouping similar articles or documents by topic using text embeddings.
  • Anomaly detection: points that end up far from every cluster centroid can indicate outliers worth investigating.

Limitations Worth Knowing

K-Means assumes clusters are roughly spherical and similarly sized, it struggles with clusters of very different shapes, densities, or sizes, and with clusters that aren't linearly separable by distance. It's also sensitive to feature scaling, features on very different numeric scales will dominate the distance calculation unless normalized first. For data that doesn't fit K-Means' spherical-cluster assumption, alternative algorithms like DBSCAN (density-based) or hierarchical clustering often perform better.