Home / Machine Learning

K-Means Clustering

Watch centroids navigate through data to find optimal clusters.

Parameters

5
150

Visualization

Iteration: 0
Adjust parameters or click Reset to begin.

Algorithm Insight

K-Means is an unsupervised learning algorithm used for clustering.

  • 1. Pick $K$ random points as starting centroids.
  • 2. Assign each point to its nearest centroid based on Euclidean distance.
  • 3. Move centroid to the mean position of its assigned points.
  • 4. Repeat until centroids no longer move (convergence).

Cluster Quality

WCSS (Inertia) --

Lower WCSS indicates tighter, more cohesive clusters.

Overview

Choose (k) initial centroids. Assignment: assign each point to its nearest centroid. Update: replace each centroid with the mean of its assigned points. Repeat until assignments stop changing or another stopping rule is reached.

The two steps

The objective, called inertia, is the sum of squared distances from each point to its assigned centroid. Assignment lowers or preserves it; updating to the mean lowers or preserves it. Because the objective can have local minima, different initial centroids can produce different results. Practical implementations run multiple starts and keep the lowest-inertia solution.

Follow a clustering

Set Clusters (K) and choose an Initial Layout. Press the step control to watch points change assignments and centroids move. Increase Points Count to see how the display changes as the dataset grows. Compare Gaussian Blobs with Nested Moons or Concentric Circles: k-means can split curved groups because each cluster is represented by a centroid and nearest-centroid regions.

Choosing k

The elbow method plots inertia for several values of (k). Inertia always decreases as (k) increases, reaching zero if every point gets its own cluster. A bend where additional centroids yield smaller improvements can suggest a useful value, but the bend may be unclear and is not a statistical proof.

Silhouette score compares each point’s distance to its own cluster with its distance to the nearest other cluster. Higher values indicate better separation under that distance measure, but it too favors some shapes and does not establish that clusters are meaningful. Use these diagnostics with domain knowledge and, when relevant, downstream validation.

When k-means fits

It is a reasonable baseline when features are numeric, clusters are roughly compact and similarly scaled, and assigning every point to a cluster is appropriate. Standardize features when measurement units differ. K-means is sensitive to outliers because both squared distances and means can be pulled by extreme points.

It is a poor match when clusters have very different densities or sizes, are strongly non-convex, or contain substantial noise. Consider DBSCAN for density-connected shapes and noise, or hierarchical clustering when examining multiple merge levels is useful.

Common mistakes with K-Means Clustering

  • Treating (k) as discovered: it is supplied by the user; choose and justify it.
  • Reading lower inertia as proof of better clusters: it almost always improves with larger (k).
  • Skipping feature scaling: large-unit features can dominate distance.
  • Trusting one initialization: run multiple starts and inspect stability.

Sources and next steps

Predict, then reveal

About to run: set Points Count to its maximum (500). Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “The two steps”?

  2. What does this module say about “Follow a clustering”?

  3. What does this module say about “Choosing k”?

Cheat sheet

K-Means Clustering

Place centroids and watch K-Means alternate between assigning points and recentring clusters until it converges.

MACHINE LEARNING · vizlearn.in/machine_learning/k_means.html

Further reading

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.