b2KIT

K-Means Clustering Visualizer

Watch K-means clustering step through centroid assignment and update. Adjust K, compare with random initialization, and visualize Voronoi cells.

Tested tool guide Tested browser tools Checked August 16, 2026

What K-Means Clustering Visualizer does, with a checked example

Each step exposes the two operations that K-means normally hides: assigning points to their nearest centroid and moving each centroid to the mean of its assigned points. The canvas shows those changes together with the centroids' Voronoi cells, and random initialization makes it possible to compare different starting arrangements. The important surprise is that K-means does not discover the number of clusters. It produces exactly the selected K, and different initial centroids can produce different final partitions.

Worked example

A concrete input and expected output from the current implementation.

Input

Set K to 1 and place points at (0, 0) and (2, 0).

Expected output

Both points belong to the single cluster, whose centroid is (1, 0). The visible plotting area is one Voronoi cell.

With K = 1, both points must share a cluster. Their coordinate mean is ((0 + 2) / 2, (0 + 0) / 2) = (1, 0), which confirms the displayed centroid.

How the result is produced

1

Assignment and update

At an assignment step, every plotted point joins the cluster whose current centroid is nearest in Euclidean distance. At an update step, each centroid moves to the arithmetic mean of the x- and y-coordinates assigned to it. Repeating these alternating steps reaches a stable assignment when another pass produces no change.

2

Voronoi interpretation

The Voronoi overlay partitions the visible plane by the current centroids. Every location inside one cell is closer to that cell's centroid than to the others, while a boundary marks equal distance between neighboring centroids. The cells move after centroid updates. Random initialization changes the starting centroids, so another run can settle on a different partition.

Good uses

  • Following a classroom K-means exercise one assignment and centroid update at a time.
  • Checking whether a small two-dimensional dataset separates differently when K or the random initialization changes.
  • Seeing how moving an outlying point shifts a cluster mean and redraws nearby Voronoi boundaries.

Limits and checks

  • The selected K is an instruction, not evidence that the data naturally contains that many groups. A visually tidy partition does not validate K.
  • A stable result need not be the best possible K-means partition. Compare multiple random initializations before treating one arrangement as representative.
  • Voronoi cells describe nearest-centroid regions, not observed density or uncertainty. Large empty portions of a cell do not imply that data exists there.

Common questions

Does the final picture prove that K-means found the best clustering?

No. The alternating updates can stop at a result determined by the initial centroid positions, and another initialization may finish differently. Repeating the same point set with random initialization helps reveal that sensitivity. Even a compact-looking result is not proof that the selected K matches meaningful groups in the underlying subject.

Why can a centroid appear where there is no plotted point?

A centroid is the coordinate-wise arithmetic mean of every point assigned to its cluster, not a selected representative point. That mean can fall between observations or even in an otherwise empty-looking area. Moving one distant observation may pull the centroid noticeably because every assigned point contributes to the mean.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools