Skip to content
StackDecode
AI & ML Atlas
Machine LearningalgorithmInteractive simulation

K-Means Clustering

Groups data into k clusters by moving centres to the average of their points.

The idea

Pick k centres, assign every point to its nearest centre, then move each centre to the mean of its assigned points. Repeat; the total within-cluster distance never increases, so it settles. Where it settles depends on the starting centres, so k-means++ chooses well-spread starts.

minimise Σ ‖xᵢ − μ_c(i)‖²

Watch it run

This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.

  • cluster 1
  • cluster 2
  • cluster 3

Drop 3 centroids at random data points.

1/9

Ringed circles are the cluster centres.

Step by step

  1. 1Choose k and place k centres (for example at random points).
  2. 2Assign each point to the closest centre.
  3. 3Move each centre to the mean of its points.
  4. 4Repeat until the assignments stop changing.

Where it is used

Customer segmentation, colour quantisation, vector quantisation.

Watch out for

You must choose k; it finds round clusters of similar size and is sensitive to outliers.

Related