Tutorials › High-Dimensional Genomic Data Analysis › The K-Means Objective and Its Geometry

Dimension Reduction and Clustering · Tutorial 611 of 795

The K-Means Objective and Its Geometry

Learn how k-means measures within-cluster variation, why cluster centers are means, and how nearest-center regions partition profile space.

Intermediate 12 min read

What You'll Learn

  • Define the k-means objective for a specified number of nonempty clusters.
  • Derive why the mean minimizes the sum of squared distances within a cluster.
  • Calculate within-cluster sum of squares for hypothetical genomic profiles.
  • Describe nearest-centroid assignments and their Voronoi boundaries.
  • Identify geometric assumptions and common ways the objective can mislead.
  • Compare the squared-Euclidean objective with other clustering criteria.

What the K-Means Objective Measures

A clustering is useful only relative to the feature space and criterion used to define it. K-means uses a particular geometric criterion: it seeks groups whose profiles lie close to their group means, with closeness measured by squared Euclidean distance. The objective is therefore a measure of within-cluster variation, not a probability model and not a direct measure of biological similarity.

Suppose \(x_1,\ldots,x_n\) are complete profile vectors in \(\mathbb{R}^p\), where each coordinate is a measured or transformed feature, and let \(K\) be a chosen integer with \(1\leq K\leq n\). All coordinates must be represented in a common Euclidean space. A clustering into \(K\) groups is a partition \(C_1,\ldots,C_K\) of the observations: each observation belongs to exactly one group, and each group is nonempty. The Euclidean squared distance between vectors \(x\) and \(y\) is \(\|x-y\|^2=\sum_{j=1}^{p}(x_j-y_j)^2\).

Definition: For a cluster \(C_k\), its centroid is the coordinate-wise mean \(\bar{x}_k=|C_k|^{-1}\sum_{i\in C_k}x_i\). The within-cluster sum of squares (WCSS) of a partition is $$ W(C_1,\ldots,C_K)=\sum_{k=1}^{K}\sum_{i\in C_k}\|x_i-\bar{x}_k\|^2. $$ The k-means objective is to find a partition into \(K\) nonempty clusters that minimizes this quantity. Smaller WCSS means that observations are, in aggregate, closer to their own cluster centroids in the chosen coordinates.

The objective gives every observation equal weight and squares every coordinate difference. Thus a few large deviations can contribute substantially, and features with large numerical scales can dominate unless the scale is scientifically appropriate or the data have been transformed. In genomic profiles, the coordinates might be normalized expression measurements or other profile features; the objective itself does not decide how those features should be prepared.

Why the Centroid Is the Mean

The centroid is not simply a convenient representative. For squared Euclidean distance, the arithmetic mean is the point that minimizes the total squared distance from the observations in a fixed cluster. This fact explains why the objective uses centroids and is central to its geometry.

Result: For any nonempty set of vectors \(C\), its mean \(\bar{x}=|C|^{-1}\sum_{i\in C}x_i\) minimizes \(\sum_{i\in C}\|x_i-c\|^2\) over all \(c\in\mathbb{R}^p\).

To verify the result, write \(x_i-c=(x_i-\bar{x})+(\bar{x}-c)\), square the norm, and sum over the cluster. The cross-term vanishes because the deviations from the mean sum to zero:

$$ \begin{aligned} \sum_{i\in C}\|x_i-c\|^2 &=\sum_{i\in C}\|x_i-\bar{x}\|^2 +2(\bar{x}-c)^\mathsf{T}\sum_{i\in C}(x_i-\bar{x}) +|C|\|\bar{x}-c\|^2\\ &=\sum_{i\in C}\|x_i-\bar{x}\|^2+|C|\|\bar{x}-c\|^2. \end{aligned} $$

The final term is nonnegative and equals zero at \(c=\bar{x}\), proving the claim. For a fixed partition, the best possible representative for each cluster is therefore its centroid. This is a statement about squared Euclidean loss; with a different loss, the best representative can differ.

Worked Example: Finding the Centroid and WCSS

Consider a hypothetical one-feature profile with values \(2,4,6\), all placed in one cluster. The centroid is $$ \bar{x}=\frac{2+4+6}{3}=4. $$ The cluster's WCSS is the sum of squared deviations from that mean: $$ (2-4)^2+(4-4)^2+(6-4)^2=4+0+4=8. $$

To check that \(4\) is the minimizing center rather than merely a plausible summary, let \(c\) be any proposed center. The identity above gives $$ \sum_{i=1}^{3}(x_i-c)^2=8+3(c-4)^2. $$ For example, choosing \(c=5\) gives \(8+3(1)^2=11\), which also follows directly from \((2-5)^2+(4-5)^2+(6-5)^2=9+1+1=11\). This exceeds 8, and every other \(c\neq4\) adds a positive term.

Nearest Centers and Voronoi Geometry

For a fixed set of centers \(c_1,\ldots,c_K\), the squared-distance rule assigns a profile to a closest center. The region of space closest to a given center is its Voronoi region. These regions are separated by boundaries where distances to two or more centers are equal. For distinct centers in ordinary Euclidean space, each pairwise equal-distance locus is a hyperplane, and each Voronoi region is an intersection of the corresponding half-spaces, hence is convex. The shared boundary for a pair of regions may be empty; when nonempty, it is a portion of the corresponding hyperplane.

Definition: The Voronoi region for center \(c_k\) is the set of points \(x\) for which \(\|x-c_k\|^2\leq\|x-c_\ell\|^2\) for every \(\ell\). A point on a shared boundary is tied between centers; an assignment rule must resolve the tie if each observation is to receive exactly one label.

For two centers \(c_a\) and \(c_b\), equality of squared distances gives a useful boundary equation. Expanding both sides and cancelling \(x^\mathsf{T}x\) yields

$$ \|x-c_a\|^2=\|x-c_b\|^2 \quad\Longleftrightarrow\quad 2x^\mathsf{T}(c_b-c_a)=\|c_b\|^2-\|c_a\|^2. $$

This equation describes a hyperplane perpendicular to the line joining the centers. If the centers coincide, their distance comparisons tie everywhere; distinct centers give the usual separating boundary. Importantly, nearest-center assignment is a rule for fixed centers. If some center has no observations assigned to it, the induced assignment is not a partition into \(K\) nonempty clusters; its Voronoi region may nevertheless contain observed profiles tied with another center.

Worked Example: Profile Assignments and a Voronoi Boundary

Suppose four hypothetical profiles have two measured, already prepared coordinates: \(x_1=(1,1)\), \(x_2=(2,1)\), \(x_3=(5,4)\), and \(x_4=(6,4)\). Consider centers \(c_1=(1.5,1)\) and \(c_2=(5.5,4)\). These are the means of the proposed groups \(\{x_1,x_2\}\) and \(\{x_3,x_4\}\), respectively.

The squared distances to \(c_1,c_2\), in that order, are \(0.25,29.25\) for \(x_1\); \(0.25,21.25\) for \(x_2\); \(21.25,0.25\) for \(x_3\); and \(29.25,0.25\) for \(x_4\). For instance, the distance from \(x_2=(2,1)\) to \(c_2=(5.5,4)\) is \((2-5.5)^2+(1-4)^2=12.25+9=21.25\). Thus the nearest-center assignments agree with the proposed groups.

The WCSS for these assignments is the sum of the four within-group squared distances: $$ W=0.25+0.25+0.25+0.25=1.00. $$ The boundary between the centers satisfies \(2x^\mathsf{T}(c_2-c_1)=\|c_2\|^2-\|c_1\|^2\). Here \(c_2-c_1=(4,3)\), \(\|c_2\|^2=46.25\), and \(\|c_1\|^2=3.25\), so the boundary is \(4x_1+3x_2=21.5\). The second profile lies on the \(c_1\) side because \(4(2)+3(1)=11<21.5\); the third lies on the \(c_2\) side because \(4(5)+3(4)=32>21.5\).

Comparing Candidate Partitions

A WCSS value is meaningful only in relation to a specified coordinate system, value of \(K\), and partition. For a fixed data set and \(K\), a lower value is better according to this objective. It does not establish that the groups are biologically real or that the chosen \(K\) is appropriate. In particular, adding groups cannot increase the best attainable WCSS: a group can be split into nonempty subsets, and re-centering those subsets cannot increase the sum of squared distances. This monotonicity means WCSS alone cannot choose \(K\).

Worked Example: Comparing Two Two-Cluster Partitions

Consider hypothetical one-coordinate profiles \(0,2,3,10\) and \(K=2\). For partition A, use clusters \(\{0,2,3\}\) and \(\{10\}\). Their means are \(5/3\) and \(10\). The WCSS is $$ \left(0-\frac{5}{3}\right)^2+ \left(2-\frac{5}{3}\right)^2+ \left(3-\frac{5}{3}\right)^2+(10-10)^2 =\frac{25+1+16}{9}+0=\frac{14}{3}\approx4.667. $$

For partition B, use \(\{0,2\}\) and \(\{3,10\}\), whose means are \(1\) and \(6.5\). Its WCSS is $$ (0-1)^2+(2-1)^2+(3-6.5)^2+(10-6.5)^2 =1+1+12.25+12.25=26.50. $$ Partition A has the smaller WCSS of these two candidates. That comparison does not by itself prove that A is the global minimizer among every possible partition; it establishes only which of these specified candidates better fits the stated criterion.

Assumptions, Diagnostics, and Alternatives

The k-means objective is deterministic: it requires no sampling distribution or independence assumption to be calculated. Its interpretation nevertheless relies on substantive choices. Each observation must have comparable meaning in a common coordinate system, the selected distance must reflect relevant differences, and equal observation weights must be defensible. Squaring makes the criterion especially sensitive to extreme profiles. If one coordinate has a much larger scale or variance than others, it can dominate distances; centering and scaling may address scale differences, but should be chosen based on the scientific meaning of the measurements, not applied mechanically.

The geometry also constrains the kinds of groupings the objective favors. Nearest-centroid regions are convex, and the squared-distance criterion tends to favor compact groups around their means. It can represent awkwardly shaped or strongly overlapping groups poorly. In high dimensions, distance concentration can make pairwise distances less discriminating, so a nearest-center assignment may convey less separation than the low-dimensional picture suggests. A low WCSS is not a guarantee of stable assignments, meaningful separation, or useful biological interpretation.

Useful checks include confirming that the input coordinates and scaling are as intended, inspecting whether a small number of profiles or features dominate the squared deviations, and examining whether assignments depend on a narrow distance margin between competing centers. A profile exactly on a Voronoi boundary is tied; a profile nearly on one may be sensitive to small changes. Also inspect for empty groups when translating centers into assignments. These checks diagnose aspects of the criterion and its geometry; they do not replace a separate assessment of whether the resulting clusters answer the scientific question.

Alternative clustering criteria encode different preferences. A medoid-based criterion uses an observed profile as a representative rather than the coordinate-wise mean, and can be less affected by an extreme profile. Hierarchical methods define groups through a distance-and-linkage rule rather than by minimizing this centroid-based WCSS. Probabilistic mixture models describe profiles through distributions and quantify uncertainty differently. These approaches do not simply provide interchangeable calculations of the same quantity: they answer different modeling or geometric questions. The appropriate choice depends on the profile representation and scientific aim.

Common pitfall: “The WCSS is small, so these clusters are well separated and biologically meaningful.” This conclusion does not follow. WCSS measures total squared distance to assigned centroids, and generally decreases as \(K\) increases. It does not, by itself, establish separation, select \(K\), validate a biological interpretation, or account for uncertainty. State the coordinate scale and \(K\), and treat WCSS as a criterion-specific summary.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions below.

  1. For a cluster containing the one-dimensional values \(1\) and \(5\), find its centroid and WCSS.
  2. For centers \(c_1=0\) and \(c_2=4\) on a line, find the Voronoi boundary and describe where the point \(x=3\) is assigned.
  3. Why does the smallest WCSS among two candidate partitions not establish that either partition is biologically useful?
  4. State one reason that a large-scale coordinate can dominate the k-means objective and one diagnostic response.

Solutions. (1) The centroid is \((1+5)/2=3\), and WCSS is \((1-3)^2+(5-3)^2=4+4=8\). (2) Equal squared distances satisfy \((x-0)^2=(x-4)^2\), giving \(x=2\). Since \(3\) is closer to \(4\) than to \(0\), it is assigned to \(c_2\). (3) WCSS measures squared distance to centroids for the chosen coordinates and \(K\); it does not establish biological meaning, and the best attainable WCSS cannot increase when \(K\) increases. (4) Squaring coordinate differences allows a feature with a much larger numerical scale to contribute disproportionately. Check coordinate scales and whether the chosen transformation is scientifically justified before interpreting the assignments.

Clinical Biostats