English interface, partly translated: navigation, lab, projects, series, about and contact are; the home page sections, the site search and the content itself (articles, tutorials, research) are still in French.

Interactive lab

K-Means step by step

Assign, update, assign… Follow the centroids until convergence, and discover where K-Means goes wrong: bad start, stretched clusters, rings.

Level
Beginner
Topics
Clustering · Inertie · k-means++
Figure 1 — Each point takes the colour of its nearest centroid. Click in the figure to add a point.
0 5·10^−7 1·10^−6
Figure 2 — Inertia (sum of squared distances to centroids): it can only decrease.
Dataset
Starting centroids
3
2

State

Centroids drawn. The next step assigns each point to its nearest centroid.

Points
135
Iteration
0
Inertia
—

How K-Means works

K-Means splits nn points into kk clusters by looking for kk centroids that minimise the inertia, the sum of squared distances from each point to its centroid:

J=∑i=1n∥xi−μc(i)∥2J = \sum_{i=1}^{n} \lVert x_i - \mu_{c(i)} \rVert^2

Lloyd's algorithm alternates two steps, each of which can only lower JJ:

  1. Assignment — each point joins its nearest centroid: c(i)=arg⁡min⁡j∥xi−μj∥2c(i) = \arg\min_j \lVert x_i - \mu_j \rVert^2;
  2. Update — each centroid moves to the mean of its points: μj=1∣Cj∣∑i∈Cjxi\mu_j = \frac{1}{\lvert C_j \rvert} \sum_{i \in C_j} x_i.

It stops when the assignment no longer changes. Convergence is guaranteed, but to a local minimum: the result depends on the starting centroids.

What the demo shows

  • Three clusters: the favourable case. Run it several times with "random" centroids: sometimes two centroids share one cluster while a third covers two. k-means++ spreads the centroids at the start and almost always avoids that trap.
  • Stretched clusters: K-Means measures Euclidean distances and assumes round clusters of similar size; elongated clusters get cut across.
  • No structure: the algorithm always returns kk clusters, even when there are none. It is up to you to check they make sense.
  • Two rings: no straight boundary separates one ring from the other; K-Means fails by design. A density-based (DBSCAN) or spectral method is suited.

Choosing k

Inertia always decreases as kk grows (it is zero for k=nk = n): it cannot choose kk on its own. Look for the "elbow" of the inertia-versus-kk curve, or use the silhouette score.

Other demos

  • Linear regression

    Artificial intelligence, Beginner

    Move the points and watch the least squares line follow. Then learn the same line by gradient descent and compare.

  • Convolution and filters

    Computer vision, Beginner

    Slide a 3 × 3 kernel over an image: blur, sharpen, edge detection. Edit the weights and follow the computation of one pixel, term by term.

  • Haar wavelets (DWT)

    Computer vision, Intermediate

    Decompose an image into LL, HL, LH and HH sub-bands over several levels, drop the small coefficients and measure what the reconstruction loses.