Introduction to 2D k-means centroids and WCSS
This k-means clustering calculator divides a set of two-dimensional observations into a chosen number of groups. Enter each observation as an x,y coordinate, select k, and the calculator assigns every point to a cluster, locates a centroid for each cluster, and draws the result. It is deliberately limited to two dimensions so the numerical answer and the picture can be read together rather than treated as a black box.
K-means does not discover a universally correct set of groups. Instead, it optimises a specific target: the within-cluster sum of squares, usually shortened to WCSS or called inertia. A point pays a cost equal to its squared straight-line distance from the centroid it belongs to. The algorithm looks for assignments and centroid positions that make the total cost small. That definition is useful because it is precise, but it also explains why a visually unusual pattern can receive an unhelpful k-means partition.
Squaring the distances makes distant observations influential. A point twice as far from a centroid contributes four times as much to WCSS, so a lone outlier can pull a mean away from a dense cloud. In return, compact, roughly circular groups of comparable spread are a natural fit. Long strips, crescents, rings, clusters with very different densities, and data with important categorical meaning are not automatically good candidates for Euclidean k-means.
The calculator uses Lloyd’s familiar alternating procedure. It begins with the first k distinct points in the entered order, assigns every point to its nearest current centroid, moves each non-empty centroid to the mean of the points it owns, and repeats. Using the first distinct points means that a given input order is reproducible. It also makes a practical warning visible: reordering exactly the same coordinates may change the starting positions and land at another local minimum.
How to use the k-means point and k inputs
Paste one coordinate pair per line into the form. Commas, semicolons, tabs, and spaces separate x from y, so 1,2, 1; 2, and 1 2 are equivalent. Decimals, negative values, and scientific notation such as 1.2e3, -4 are accepted. Blank lines are ignored. A malformed nonblank line is skipped and identified after the run, rather than quietly becoming a point at the origin.
Choose a whole-number value of k from 1 through the number of valid points. Start with a small number that makes sense for the question you are asking. Then try nearby values and compare the plot as well as WCSS. Adding centroids can never increase WCSS: with more centres, the old solution is still available. Therefore a lower WCSS at k = 4 than at k = 3 is expected, not proof that four groups are more meaningful.
Read the centroid table and the plot together. The coloured points show final membership, while each X marks a centroid. Faint spokes connect points to the centre that contributes their squared distance. A broad cluster, a cluster dominated by a single far-away point, or an apparently empty cluster is often easier to diagnose in the scatter plot than in a single summary number.
Formula for the k-means objective and centroid update
Let the entered observations be
Formula: p_1, p_2, …, p_n , where each point has coordinates p_i = (x_i, y_i). You choose k centres, c_1, c_2, …, c_k, and clusters S_1, S_2, …, S_k . The assignment stage sends point p_j to the closest available centroid: a_j = argmin i ∈{1, …, k} |p_j−c_i|^2
, where each point has coordinates .
You choose centres,
,
and clusters . The assignment stage sends point to the closest available centroid:
K-means then minimises
Formula: J = ∑ i = 1 k ∑ p ∈ S_i |p−c_i|^2
where is Euclidean distance. In this calculator’s x,y plane, it is
Formula: | p − c_i | = sqrt((x−x_c)^2 + (y−y_c)^2)
The reported WCSS is the same quantity without taking a square root:
Formula: WCSS = ∑ i = 1 k ∑ p ∈ S_i [(x_p−x_i)^2 + (y_p−y_i)^2]
For a fixed cluster membership, the least-squares best centroid is simply the coordinate mean:
Formula: c_i = (∑ p ∈ S_i p) / (| S_i |)
The total spread used for the fit comparison is measured around the grand mean :
Formula: TSS = ∑ j = 1 n |p_j−p¯|^2
This is why the update step uses arithmetic means. The calculator also rescales WCSS against total spread around the grand mean, reporting:
Formula: explained = 1 − WCSS / TSS
Lloyd’s iterations stop when repeated assignment no longer changes any label:
Formula: a_j^( t + 1) = a_j^( t) for every j
At k = 1 this explained fraction is zero, except for the degenerate case where every input point is identical. It rises as k rises, so treat it as a compact description of fit rather than an automatic cluster-count selector.
Worked example with two separated point clouds
The supplied example contains (0,0), (0,1), (1,0), (5,5), (5,6), and (6,5). With k = 2, the final centroids are approximately (0.333,0.333) and (5.333,5.333). The three points in each compact cloud lie close to their own mean, so each cloud contributes about 1.3333 and total WCSS is about 2.6667.
The example is also a reminder not to overinterpret initialisation. The calculator begins from the first distinct points in the entered order. If k = 3, those first three seeds all begin in the left cloud. Lloyd’s steps may leave an empty centre and produce the same WCSS as k = 2, even though a better three-centre arrangement exists. Reordering the rows changes the deterministic seeds. In larger analytical work, multiple careful starts or k-means++ initialisation are common ways to reduce this risk.
Interpreting WCSS, cluster labels, and the scatter plot
WCSS is most useful when comparing runs on the same scaled data with the same k. A cluster’s share of WCSS and RMS radius indicate where looseness is concentrated. A large radius might represent a genuinely diverse group, poor feature scaling, an outlier, or a shape that is not well represented by a centroid. Cluster labels are identifiers only: cluster 1 is not higher quality, larger, or nearer the origin than cluster 2.
Scale deserves special attention. If x ranges from 0 to 10,000 and y ranges from 0 to 10, then x dominates squared Euclidean distance even when the two measurements matter equally in the real-world question. Standardize both coordinates before entering them when appropriate. Conversely, do not standardize blindly when the original units are intentionally meant to weight one axis more heavily.
Assumptions and limitations of this 2D k-means tool
This tool accepts only finite numeric x,y pairs, uses squared Euclidean distance, keeps empty centroids at their previous locations, and stops after 100 assignment passes. It is an exploratory calculator rather than a claim that the plotted groups are causally real. K-means is strongest for numeric, compact, similarly sized clouds. Consider density-based clustering for irregular shapes and explicit noise, hierarchical methods for nested structure, k-medoids when outliers matter, or Gaussian mixtures when overlapping elliptical groups and soft membership are important.
Finally, choose k using subject knowledge as well as numerical diagnostics. An elbow in WCSS can be helpful, but it can be shallow or misleading. Ask whether clusters are stable under reasonable preprocessing choices, whether they describe useful differences, and whether their sizes and centroids are plausible in context. Those checks make the result more informative than a low objective value alone.
Sources for Lloyd k-means and the inertia objective
The assign-then-average method follows S. P. Lloyd, “Least squares quantization in PCM”, IEEE Transactions on Information Theory 28(2), 129–137 (1982), and J. MacQueen, “Some methods for classification and analysis of multivariate observations” (1967). For a broader treatment, see The Elements of Statistical Learning and the scikit-learn clustering guide.
Centroid Chase: tune centres under pressure
Centroid Chase turns the calculator’s objective into a short optional arcade challenge. Drag each glowing centroid toward the point cloud it should represent. Every correct tuning move lowers live WCSS, builds a streak, and earns time. At 25 seconds, a noisy outlier wave appears; at 50 seconds, the clouds shift. It is a quick visual lesson in why centroids are means and why a distant point can be expensive.
Pointer or touch: drag a centroid. Keyboard: focus the board, use Arrow keys to nudge the selected centroid, C to cycle centroids, Space for one Lloyd step, and R to restart. The game is optional and does not affect calculator results.
Live WCSS
—
Par WCSS
—
Click to play when you are ready.
Clustering takeaway: each point’s squared distance adds to WCSS. Moving a centroid toward the mean of its assigned points is the move that reduces that cluster’s squared-error total most directly.
