Clustering and Principal Component Analysis
Clustering and Principal Component Analysis
Two unsupervised tools, two different jobs
Both k-means clustering and PCA work on unlabeled numeric data, but they answer different questions. Clustering asks which observations are similar to each other and sorts them into groups.
PCA asks which linear combinations of variables explain the most variance. Variance is not the same as scientific relevance: a high-variance noise variable can dominate a component without informing the scientific question.
PCA re-expresses the data along a smaller number of new axes. Students often treat them as interchangeable "dimension reduction" tools; they are not.
K-means: assign, then update, repeat
K-means starts by picking k initial centroids (often randomly). Each iteration has two steps:
- Assignment: every point is assigned to the centroid it is closest to, using Euclidean distance.
- Update: each centroid is recomputed as the mean of all points currently assigned to it.
These two steps repeat until assignments stop changing. Because the algorithm only ever moves centroids to the average of their current members, it tends to produce roughly spherical, similarly sized clusters, it can perform poorly on elongated or unevenly sized groups.
Worked example: assigning a point
Suppose two centroids sit at A = (2, 3) and B = (8, 7). A new point X = (4, 5) needs an assignment. Euclidean distance is sqrt((x1-x2)^2 + (y1-y2)^2):
- Distance to A: sqrt((4-2)^2 + (5-3)^2) = sqrt(8) ≈ 2.83
- Distance to B: sqrt((4-8)^2 + (5-7)^2) = sqrt(20) ≈ 4.47
X is closer to A, so it joins cluster A. After every point is reassigned this way, both centroids are recomputed as the mean of their new members, and the cycle repeats.
PCA: re-expressing variance along new axes
PCA starts from the covariance (or correlation) matrix of the variables and finds new axes, principal components, that are linear combinations of the originals, ordered so PC1 explains the most variance, PC2 the next most among directions orthogonal to PC1, and so on.
Each component has an associated eigenvalue, and the proportion of total variance it explains is that eigenvalue divided by the sum of all eigenvalues.
Which matrix you start from is a modelling decision rather than a formality: a covariance-matrix PCA maximizes and reports variance in the original measured coordinates, while a correlation-matrix PCA, identical to standardizing every variable first, maximizes variance in standardized coordinates, where each variable contributes exactly one unit of variance and the eigenvalues sum to the number of variables.
Worked example: variance explained
Suppose a 3-variable dataset yields eigenvalues 4.8, 1.0, and 0.2 (total variance = 6.0). PC1 alone explains 4.8 / 6.0 = 80% of total variance. PC1 and PC2 together explain (4.8 + 1.0) / 6.0 ≈ 96.7%.
This is why a "scree plot" of variance explained per component is used to decide how many components to keep, once additional components add only a sliver of variance, they are usually dropped.
Scree plot: variance explained per principal component
During a k-means run that has not yet converged, immediately after every data point has been assigned to its nearest centroid, and with every centroid holding at least one assigned point, what does the algorithm do next?
Mark Yourself Question
- Grab a piece of paper and try this problem yourself.
- When you're done, check the "I have answered this question" box below.
- View the solution and report whether you got it right or wrong.
Two centroids are located at A = (1, 2) and B = (7, 10). A new point X = (3, 5) must be assigned to whichever centroid it is closer to under Euclidean distance. Which cluster does X join, and what are the two distances (rounded to two decimals)?