Unsupervised Learning: K-Means Clustering and PCA
Every model so far has been handed the right answer for every training example. This page is what happens when there isn't one.
Advanced
y to learn from. Unsupervised
learning works with X alone.
k centroids — points in the feature space that don't have to
coincide with any actual data point. The algorithm alternates between two simple steps until
nothing changes:
1. initialize k centroids (e.g. randomly chosen from the data points)
2. repeat until convergence:
a. ASSIGN: put every point into the cluster of its nearest centroid (centroids held fixed)
b. UPDATE: move each centroid to the mean position of the points now assigned to it (assignments held fixed)
k should be — that's chosen
beforehand, and there's no single correct answer. One practical approach: run k-means for a
range of k values, plot the final distortion against k, and look for
the elbow — the point where adding another cluster stops meaningfully reducing
the cost. (Distortion decreases monotonically as k grows, hitting zero when
k equals the number of data points — every point its own cluster, which
is obviously not a useful clustering.) If the failure data genuinely separates into around
four recurring shapes, the elbow plot should show a sharp drop from k=1 through k=4, then a
much flatter slope afterward, as additional clusters start splitting real groups apart rather
than separating genuinely different ones.
lines_changed (ranging into the thousands) would
dominate every distance calculation over time_of_day (ranging 0-23), and the
resulting clusters would essentially just be "small diffs" vs. "large diffs," ignoring
whatever timing signal actually exists in the data. Standardizing every feature (z-score
normalization, from that same page) before clustering is close to mandatory.
duration, lines_changed, hour_of_day, ...), PCA finds
new features — principal components — that are weighted combinations of the
originals, chosen so the first component captures as much of the data's total variation as
possible, the second component captures the most variation left over that's uncorrelated with
the first, and so on. The weights defining each component are called loadings.
Like k-means, PCA is scale-sensitive and is standard practice to run only on already-standardized
data.
| Use case | Why it works |
|---|---|
| Dimensionality reduction | when features are highly correlated (redundant), most of the real variation collapses into just the first few components — the rest can often be dropped with little information lost |
| Visualization | projecting many features down to the first 2-3 components makes it possible to actually plot and eyeball the failure clusters, something impossible to do directly with a dozen raw features |
| Noise reduction | shared background noise across correlated sensor-like features often concentrates in the low-variance components, which can be discarded to boost signal-to-noise |
| Decorrelation | algorithms that struggle with correlated inputs get uncorrelated components instead — a different fix for the same multicollinearity that motivates dropping redundant features elsewhere |