About K-Means Clustering

This simulation visualises k-means, a classic unsupervised learning algorithm that partitions points into k groups. It alternates two steps: assign every point to its nearest centroid (using squared Euclidean distance), then move each centroid to the mean of its assigned points. It repeats until assignments stop changing. The shaded Voronoi regions show each centroid's territory, and the inertia plot tracks the within-cluster sum of squares, WCSS = Σ|xᵢ − μ|².

The k slider sets the number of clusters (1 to 8), while the dataset menu offers Gaussian blobs, concentric circles, two moons, uniform noise, or a custom set you build by clicking the canvas. Step advances one iteration, Auto runs continuously, and Reset re-seeds the centroids. Initialisation uses k-means++, which spreads starting centroids apart for faster, more reliable convergence. K-means underpins customer segmentation, image colour quantisation, and document grouping.

Frequently Asked Questions

What is k-means clustering?

K-means is an unsupervised algorithm that splits data into k clusters by minimising the total squared distance between points and their cluster centre, called the centroid. It is unsupervised because no labels are given; the structure is discovered purely from how points are spread out. It is one of the most widely used clustering methods in machine learning.

How does the algorithm work step by step?

First k centroids are placed, then each point is assigned to its nearest centroid, and finally each centroid is moved to the average position of the points assigned to it. These assign-and-update steps repeat until no point changes cluster. In this simulation, each press of Step performs one full iteration of this loop.

What does the k slider do?

The k slider sets how many clusters the algorithm looks for, from 1 to 8. Changing it reloads the dataset and re-initialises the centroids. Choosing k well matters: too few merges distinct groups, while too many splits a single group into fragments.

What is inertia or WCSS?

Inertia, also called the within-cluster sum of squares (WCSS), is the total of the squared distances from each point to the centroid of its cluster. The formula is WCSS = Σ|xᵢ − μ|². K-means tries to make this value as small as possible, and the chart shows it decreasing as the algorithm converges.

What are the shaded Voronoi regions?

Each shaded region marks the area of the canvas that is closer to one particular centroid than to any other. These are Voronoi cells, and their boundaries are exactly where a point would switch from one cluster to the next. As the centroids move during iteration, the regions reshape to follow them.

Why do the centroids stop moving?

The algorithm has converged when an entire assign step produces no change in any point's cluster. At that moment the centroids already sit at the mean of their points, so the update step cannot move them further. The Converged indicator switches to Yes, and Auto stops automatically.

What is k-means++ initialisation?

K-means++ is a smarter way to choose the starting centroids. After picking the first one at random, each further centroid is chosen with probability proportional to its squared distance from the nearest existing centroid, spreading them out. This simulation uses k-means++, which reduces poor local minima and usually converges in fewer iterations.

Can k-means handle the circles and moons datasets?

Not well. K-means assumes roughly round, similarly sized clusters because it relies on distance to a single mean point. Concentric circles and interlocking moons are not separable this way, so k-means carves them into wedges rather than recovering their true shapes. They are included to show this important limitation.

Does k-means always find the best clustering?

No. K-means is guaranteed to converge, but only to a local minimum of the inertia, not necessarily the global best. The result depends on the starting centroids, which is why different resets can give different outcomes. K-means++ helps but does not fully remove this sensitivity.

Where is k-means used in the real world?

K-means is used for customer and market segmentation, compressing images by reducing them to a small palette of representative colours, grouping documents or news articles by topic, and detecting anomalies that fall far from any cluster. Its speed and simplicity make it a common first choice for exploratory data analysis.