lablaboratoryclusteringkmeansmachine-learningdata-science2026-11-22
Laboratory/interactive·TypeScript · D3.js · SVG · Canvas

Interactive K-Means Clustering Explorer

Browser-based two-dimensional K-Means laboratory comparing random and K-Means++ initialization, step-by-step centroid convergence, and within-cluster sum of squares (WCSS).

Diamonds mark centroids with dashed drift trails. Click or tap anywhere on the plot to add custom points.

How the Explorer Works

K-Means partitions nn observations in R2\mathbb{R}^2 into kk clusters by alternating between two steps until assignments stabilize:

  1. Assignment Step: Each point xix_i joins the nearest centroid μj\mu_j under Euclidean distance ∥xi−μj∥22\lVert x_i - \mu_j \rVert_2^2.
  2. Update Step: Each centroid μj\mu_j moves to the arithmetic mean of all points currently assigned to cluster jj.

Why Initialization Matters

Standard Random initialization places initial centroids uniformly across existing data points and can become trapped in poor local optima (especially when multiple centroids start inside the same dense cluster). K-Means++ selects initial centroids sequentially with probability proportional to D(x)2D(x)^2—the squared distance to the closest already-chosen centroid—dramatically improving convergence stability and lowering final within-cluster sum of squares (WCSS).

Experiments to Try

  • Switch between Gaussian Blobs and Two Moons to observe where Euclidean centroid geometry succeeds versus where non-convex manifolds break linear Voronoi boundaries.
  • Compare Random vs. K-Means++ on the Anisotropic preset at k=4k = 4.
  • Tap or click directly on the plot canvas to inject custom outlier points and step through centroid drift.
Full search page →