Skip to main content
Back to projects

KMeans Clustering

Unsupervised learning

Fundamentals Interactive
Python k-means
Interactive demo runs k-means in your browser

Click in the plot to place points (or add random ones), choose how many clusters you want, then run k-means and watch the centroids settle. Points are recoloured by whichever centroid is nearest, then the centroids move to the mean of their cluster and the loop repeats.

Click in the plot to add points, then press "Run k-means".

How it works

  1. 1 Place data points
  2. 2 Choose k centroids
  3. 3 Assign each point
  4. 4 Move centroids and repeat

How it works

K-means groups points into k clusters by repeating two steps: assign every point to its nearest centroid, then move each centroid to the mean of the points assigned to it. It is unsupervised — nothing is labelled, and the structure emerges purely from distances.

Building it, step by step

1. Choose k

The number of clusters is picked up front. Too few merges distinct groups; too many splits them.

2. Assign and move

Points take the colour of the nearest centroid, then each centroid slides to the middle of its points. The assignment changes, so the centroids move again.

3. Converge

The loop stops when the centroids stop moving. The result is a local partition of the data.

What I learned

K-means is simple but sensitive to its starting centroids and to k, and it measures distance in straight lines — so its clusters are round-ish by design, which is worth remembering before reading meaning into the output.