🎯 K-Means Clustering AI / Machine Learning 🇺🇦 Українська
Configuration
Number of clusters K
3
Show Voronoi regions
K-Means++ init
Show centroids trail
Actions
Status
Points0
Iteration0
WCSS
Converged
Elbow Method
Legend
K-Means: Assign each point to nearest centroid, then move centroids to cluster mean. Repeat until convergence.

K-Means++: Smarter initialisation — each centroid chosen with probability ∝ d² from nearest existing centroid.

Elbow Method: Plot WCSS vs K; optimal K is at the "elbow" bend.

About K-Means Clustering

This simulation visualises K-Means, an unsupervised algorithm that partitions a set of 2D points into K groups. It alternates two steps until the centroids stop moving: assignment, where every point joins the nearest centroid by squared Euclidean distance, and update, where each centroid moves to the mean position of its assigned points. The shaded Voronoi background shows the region each centroid currently owns, and convergence is declared when centroid movement falls below a tiny threshold.

The K slider sets the number of clusters (2 to 8); Step performs one iteration, Run loops automatically, and Reset clears the assignment. Toggles control the Voronoi overlay, centroid trails, and K-Means++ initialisation. Generators create blobs, half-moons, and ring data, while the Elbow panel plots within-cluster sum of squares (WCSS) against K. K-Means underpins customer segmentation, image colour quantisation, and document grouping.

Frequently Asked Questions

What is K-Means clustering?

K-Means is an unsupervised learning algorithm that splits unlabelled data into K groups so that points within a cluster are as close as possible to one another. It does this by finding K centroids (cluster centres) and assigning each point to its nearest one. Here you can add points, choose K, and watch the centroids settle into place.

How does the algorithm actually work?

It repeats two steps. First, each point is assigned to the nearest centroid using squared Euclidean distance. Second, every centroid is moved to the mean of the points assigned to it. These two steps alternate, and the total spread shrinks each round until the centroids barely move, at which point the algorithm has converged.

What do the controls on this page do?

The K slider sets the cluster count (2 to 8). Step runs one iteration; Run animates iterations every 300 ms; Reset clears assignments. The toggles switch the Voronoi regions, the dashed centroid trails, and K-Means++ initialisation on or off. The data buttons generate blob, half-moon, or ring point sets, and you can also click the canvas to add points by hand.

What is WCSS and what does the Status panel show?

WCSS is the within-cluster sum of squares: the total squared distance from every point to its assigned centroid. Lower WCSS means tighter clusters. The Status panel reports the point count, the current iteration, the live WCSS (shown in thousands), and whether the algorithm has converged.

What is the Elbow method shown in the sidebar?

The Elbow method helps choose a sensible K. It runs K-Means for K from 2 to 8 and plots the resulting WCSS as bars. WCSS always falls as K rises, but the rate of improvement slows sharply at a point that looks like an elbow in the curve. That bend usually marks a good trade-off between fit and simplicity.

How does K-Means++ initialisation differ from random?

Random initialisation simply picks K existing points as starting centroids, which can place several near each other and lead to poor results. K-Means++ spreads them out: after a random first centroid, each subsequent one is chosen with probability proportional to its squared distance from the nearest existing centroid, giving faster, more reliable convergence.

What are the Voronoi regions and the dashed lines?

The faint coloured background shows Voronoi regions: every pixel is tinted by whichever centroid is nearest, so the boundaries reveal the decision frontier between clusters. The dashed lines are centroid trails, tracing the path each centroid travels from its starting position to its final resting place across the iterations.

Will K-Means handle the half-moons and ring shapes well?

Often not, and that is the point of including them. K-Means assumes roughly round, similarly sized clusters because it relies on distance to a single centre. The half-moons and concentric ring data are non-convex, so K-Means tends to slice them awkwardly. They illustrate where density- or graph-based methods would do better.

Why can the same data give different results each run?

K-Means is sensitive to its starting centroids, and initialisation is randomised here. Different starts can settle into different local optima, so WCSS and the final groupings may vary between runs. K-Means++ reduces this variability, and in practice analysts run the algorithm several times and keep the result with the lowest WCSS.

Where is K-Means used in the real world?

It is one of the most widely used clustering methods. Applications include customer and market segmentation, compressing images by reducing them to K representative colours, grouping documents or search results by topic, detecting anomalies, and pre-processing data before other machine learning models. Its speed and simplicity make it a common first choice.