Home › Learn AI › K-Means Clustering Visualizer

K-Means Clustering Visualizer

Watch k-means clustering work step by step. Generate blobs or tap to add points, choose k, compare k-means++ with random initialisation, track inertia and use the elbow method to pick the best k.

Tap the plot to add points. ✚ marks a centroid and the faint line behind it shows where it has moved.

0Iterations
–Inertia
0Points

The algorithm in four lines

  1. Initialise: pick k starting centroids, either random points or spread-out points with k-means++.
  2. Assign: colour every point by its nearest centroid. The faint background shows each centroid's territory.
  3. Update: move each centroid to the average position of the points assigned to it.
  4. Repeat steps 2–3 until no point changes colour. The inertia (sum of squared distances to the nearest centroid) never goes up.

When k-means fails (and why)

  • Moons and Rings: k-means assumes clusters are round blobs around a centre. It cuts the shapes with straight lines instead of following them. Density-based methods such as DBSCAN, or spectral clustering, handle these shapes.
  • Unequal: a big, spread-out cluster next to small, tight ones gets split in half, because k-means prefers clusters of similar size. Gaussian mixture models handle different spreads better.
  • No clusters: k-means always returns k groups, even from uniform noise. The elbow chart has no clear bend, which tells you there is no natural grouping.
  • Bad starts: pick Random initialisation and press New start a few times on Blobs. Sometimes two centroids share a blob while another blob is split. That is a local minimum, and it is why k-means++ and multiple restarts are used.

Where k-means is used

Customer segmentation, grouping documents or embeddings by topic, image colour quantisation, compressing data (vector quantisation) and as a quick first look at any unlabelled dataset.

What is k-means clustering?

K-means is an unsupervised learning algorithm that splits unlabelled data into k groups. It repeats two steps: assign every point to its nearest centroid, then move every centroid to the mean of its points. It stops when no point changes cluster. Each round can only lower the total squared distance (the inertia), so the algorithm always finishes, although not always at the best possible answer.

What is k-means++?

k-means++ is a smarter way to choose the starting centroids. The first is picked at random. Each next one is picked with probability proportional to its squared distance from the nearest centroid already chosen, so the starting centroids are spread out. It usually converges faster and finds better clusters than random starts, and it is the default in scikit-learn.

How do I choose k?

Plot the inertia for k = 1, 2, 3 … and look for the elbow: the point where adding another cluster stops reducing the inertia by much. The silhouette score and domain knowledge are other common ways to choose.

Common interview questions

  • Time complexity: O(n · k · d · iterations) for n points in d dimensions.
  • Is it deterministic? No. The result depends on the starting centroids, so libraries run it several times and keep the lowest-inertia result (n_init).
  • Should I scale features? Yes. K-means uses Euclidean distance, so a feature with a large range would dominate.

More Learn AI