HomeMachine Learning & Neural NetworksK-Means Clustering — Lloyd's Algorithm

🎯 K-Means Clustering — Lloyd's Algorithm

Cluster 2D points with k-means: assign each to its nearest centroid, move centroids to the mean, repeat. Watch the Voronoi cells settle and inertia drop, and see k-means++ seeding.

Machine Learning & Neural Networks3DModerate60 FPS
k-means ↗ Open standalone

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.

⚙ Under the hood

Cluster 2D points with k-means: assign to nearest centroid, move to the mean, repeat. Watch Voronoi cells settle and inertia drop, with k-means++ seeding.

k-meansclusteringLloydunsupervisedCanvas 2D

3D · Three.js / WebGL renderer · 60 FPS target · runs fully client-side, no install

What did you find?

Add reproduction steps (optional)