🎯 K-Means Clustering
Lloyd's algorithm & Voronoi cells
Iteration 0
Inertia:
Preset
Setup
k-means++ seeding
Controls
Stats
Iteration
0
Points
0
Inertia (SSE)
Status
Ready
Click the canvas to add points.
Info & Theory

K-means partitions points into k clusters by minimising the total squared distance from each point to its cluster centre. Lloyd's algorithm alternates two cheap steps until nothing changes.

The two steps

  • Assign: attach every point to its nearest centroid. This carves the plane into Voronoi cells.
  • Update: move each centroid to the mean of the points assigned to it.

Repeat. Each step can only lower the objective, so the inertia (within-cluster sum of squared errors) never rises and the process converges.

Inertia

inertia = Σ‖xᵢ − μ_c(i)‖², the sum over all points of the squared distance to their assigned centroid μ. Watch it fall every iteration.

k-means++ seeding

Random initial centroids can give a poor local optimum. k-means++ picks the first centre at random, then chooses each subsequent centre with probability proportional to its squared distance from the nearest existing centre — spreading seeds out and giving far better, more stable results.

Limitations

K-means assumes roughly spherical, similarly sized clusters and needs you to fix k in advance. It struggles with elongated or nested shapes (try the Moons preset) — that is where density methods like DBSCAN shine.

About K-Means Clustering Simulator

K-means clustering is an unsupervised machine learning algorithm that partitions a dataset into k groups (clusters) such that points within each cluster are closer to their cluster's centroid (mean position) than to any other cluster's centroid. The algorithm iterates between two steps: assignment (each data point is assigned to the nearest centroid) and update (each centroid is recomputed as the mean of all points assigned to it). This continues until assignments stabilise.

The algorithm minimises the within-cluster sum of squared distances (WCSS): W = Σₖ Σᵢ∈Cₖ ||xᵢ - μₖ||². While each iteration is guaranteed to decrease W, the algorithm converges to a local rather than global minimum. The solution depends on the initial centroid placement; the k-means++ initialisation (choosing centroids with probability proportional to their squared distance from existing centroids) significantly improves average solution quality and convergence speed.

K-means is widely used in data analysis and machine learning for customer segmentation (grouping customers by purchasing behaviour), image compression (replacing each pixel with its cluster centroid colour, reducing colour palette size), document clustering (grouping articles by topic), genomics (clustering gene expression profiles), and as an initialisation step for more complex algorithms. Choosing k is non-trivial; the elbow method plots WCSS against k and looks for a kink where adding more clusters yields diminishing returns.

Frequently Asked Questions

How does the k-means algorithm work step by step?

Initialise k centroids (randomly or via k-means++). Assign each data point to the nearest centroid by Euclidean distance. Recompute each centroid as the mean position of all assigned points. Repeat assignment and update until no point changes cluster between iterations (convergence). Run multiple times with different initialisations and keep the result with the lowest WCSS.

How do you choose the number of clusters k?

The elbow method plots WCSS against values of k; the optimal k is at the bend (elbow) where adding more clusters provides diminishing improvement. The silhouette score measures how well each point fits its cluster relative to neighbouring clusters (ranges -1 to +1; higher is better). Domain knowledge often provides the best guide — for example, a retailer segmenting customers might know they want 5 customer personas.

What are the limitations of k-means clustering?

K-means assumes spherical clusters of similar size and density, and struggles with elongated, irregular, or nested clusters. It is sensitive to outliers (which can pull centroids away from the bulk of data) and to the choice of k. It uses Euclidean distance, making it unsuitable for categorical data without preprocessing. Algorithms like DBSCAN, Gaussian mixture models, or hierarchical clustering address some of these limitations.

What is k-means++ initialisation?

K-means++ improves initial centroid selection to avoid poor starting configurations. The first centroid is chosen randomly. Each subsequent centroid is chosen with probability proportional to the squared distance from the nearest existing centroid, ensuring centroids start well-separated and spread across the data. This typically yields better final cluster quality and faster convergence than purely random initialisation.

How is k-means used for image compression?

K-means applied to image compression treats each pixel's RGB colour as a 3D data point and clusters all pixels into k colour groups. Each pixel is then replaced by its cluster's centroid colour, reducing the image to a k-colour palette. With k=16, a 24-bit colour image is approximated using only 4 bits per pixel plus a 16-colour lookup table — a significant compression with moderate quality loss.