ML Playground
ClusteringIntermediateUnsupervised learning

K-Means

Available

Run a clustering algorithm iteration by iteration and watch the centroids move.

Description

Qué haceSplits a set of points into k groups, trying to keep the points within each group as close together as possible.

Para qué sirveDiscovering structure or segments in unlabeled data (customers, documents, images) when you don't know in advance which group each item belongs to.

Groups points into k clusters by alternating assignment and centroid updates. Synthetic dataset; the algorithm only ever sees coordinates, never the original labels.

Assignment, update, and inertia

Assignment

Each point joins the nearest centroid by Euclidean distance.

Update

Each centroid moves to the average of the points assigned to it. This is Lloyd's algorithm.

Inertia

Σ ‖point − its centroid‖²

Measures how compact the clusters are. It never goes up from one assignment to the next.

Choosing k

It’s an external decision: the “Elbow method” sweeps k comparing inertia.

What the chart shows

Phase / iteration

Where in the assignment-update alternation the run currently is, and how many have happened so far.

Centroids

Marked larger, with a ring and a number (C0, C1...). The dashed line shows their most recent movement.

Strengths and limits

Simple and fast

O(k · n) per iteration

Every point belongs to exactly one cluster. Works well with roughly spherical groups of similar size.

Only convex regions

Doesn't separate structures like concentric rings — see the “Poor fit” case.

Sensitive to outliers

The mean gets pulled by extreme values — see the “Outlier sensitivity” case.

Sensitive to initialization

The same dataset can converge differently depending on the seed. k-means++ reduces the risk, without eliminating it.

Playground

Training

Points, cluster assignment, and centroids-6.3-3.00.33.77.0-7.2-3.8-0.33.16.5xyC0C1C2
120 points not assigned yet. The 3 initial centroids are shown before the first assignment.
  • Unassigned point
  • Cluster 0 (centroid C0)
  • Cluster 1 (centroid C1)
  • Cluster 2 (centroid C2)

Each point takes the color and shape of its nearest centroid's cluster (Euclidean distance). The dashed accent line shows how much each centroid moved in the last update.

Run state

Phase
Ready

Initial centroids computed. Step forward to start assigning points.

Iteration
0 / 100
Current inertia

Dataset generation

Changing these controls generates a new synthetic dataset and resets the run.

Seed for the deterministic generator.

40
5120
1.2

Standard deviation of each group's Gaussian noise.

0.24.0

K-Means

3

Number of clusters. Limited to the palette's color/shape combinations.

16
Centroid initialization
1

How many different initializations to try, keeping the one with the lowest final inertia. n_init=1 is the original behavior: a single run, no comparison.

110

Educational cases

Inertia

Inertia evolution per assignment0.00.30.50.81.00.00.30.50.81.0AssignmentInertia
No assignments recorded yet.

Run

Status: Ready. Initial centroids computed. Step forward to start assigning points.

Run controls are available. Shortcut: spacebar starts or pauses (if focus isn't in a text field or control).

Choosing k

Elbow method: final inertia vs. k0.31.93.55.16.8-67.1557.11181.31805.52429.7kFinal inertia
Final inertia per k: k=1 → 2141.63, k=2 → 1139.24, k=3 → 341.50, k=4 → 306.88, k=5 → 263.13, k=6 → 220.96. The largest inertia drop happens right before k=2, making it a reasonable candidate.

Related concepts

References