K-Means Clustering Lab
Interactive 2D clustering simulator with Voronoi regions, centroid updates, and inertia tracking.
K-Means partitions data into K clusters by minimizing within-cluster squared distance (inertia).
$$ J = \sum_{i=1}^{N} \min_{1 \leq j \leq K} \left\lVert x_i - \mu_j \right\rVert^2 $$
Each sample is assigned to the nearest centroid, then centroids are recomputed as the mean of assigned samples.
The algorithm alternates between two deterministic steps:
$$ c_i \leftarrow \arg\min_j \lVert x_i - \mu_j \rVert^2 $$
$$ \mu_j \leftarrow \frac{1}{|C_j|}\sum_{x_i \in C_j} x_i $$
Inertia decreases monotonically until convergence to a local optimum.
Random initialization is simple but can start from poor seeds.
k-means++ spreads initial centroids, often yielding faster convergence and lower final inertia.
Use multiple runs with different seeds in production to reduce local-minimum sensitivity.
- Load demo points, then compare Random vs k-means++ initialization.
- Try different
Kvalues and inspect region boundaries and cluster counts. - Use Step to inspect one assign/update cycle at a time.
- Run until convergence and compare final inertia values across settings.
Click anywhere on the canvas to add points. Long-press on touch devices or right-click to remove the nearest point.