Home▸Machine Learning & Neural Networks▸K-Means Clustering (2D) — Lloyd's Algorithm

K-Means Clustering (2D) — Lloyd's Algorithm

Interactive 2D k-means: assign every point to its nearest centroid, move each centroid to the mean of the points assigned to it, and repeat. Watch the Voronoi cells settle and inertia fall, with k-means++ seeding, a k slider and Blobs/Moons/Random data presets.

Machine Learning & Neural Networks2DModerate60 FPS📱 Mobile-adapted⇄ 3D version
2d-k-means ↗ Open standalone

This 2D companion runs the same Lloyd's-algorithm loop as the 3D version on a plain canvas: click to add points, pick a k, and watch the two-step iteration — assign every point to its nearest centroid, then move each centroid to the mean of its assigned points — carve the plane into Voronoi cells that settle as inertia (the sum of squared distances from each point to its centroid) falls. A k-means++ toggle lets you compare seeding strategies, and the Blobs/Moons preset makes clear where k-means succeeds (round, similarly-sized clusters) and where it struggles (the interleaving Moons shapes).

⚙ Under the hood

Interactive 2D k-means: assign each point to its nearest centroid, move centroids to the mean, repeat. Watch the Voronoi cells settle and inertia drop, with k-means++ seeding and Blobs/Moons/Random presets.

k-meansclusteringlloyd's algorithmunsupervisedvoronoicanvas 2d

2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install

What did you find?

Add reproduction steps (optional)