Picture a Grafana board with 48 panels on it. Twelve services, and for each one its CPU, its memory, and the bytes it sends and receives every five minutes. I have three months of exactly that, and the question I want answered is not “what is each panel doing”. It is “how many different things are happening here?”
Principal Component Analysis (PCA) answers it. I had just finished the linear algebra course of the Maths for Machine Learning specialisation, which ends on PCA, and wanted to run it on data I understand. Just for fun, I used Claude to build a notebook and walk me through implementing PCA on my own data, step by step - first how the raw metrics move into a matrix, then the algorithm itself. The companion notebook (read it with its results or download it to run yourself) follows the same steps on a fake cluster, so you can run them without my lab.
Why I could not just look at the panels#
I expected about twelve pairs of panels to move together - every request a service receives comes back as a response, so network in and out should track each other. There were 170 pairs with a correlation above 0.95.
Forty-eight panels are not forty-eight pieces of information when most tell the same story in different units, and a linear model fitted on them struggles to tell their effects apart.
Where the 48 columns come from#
The lab is a small Kubernetes cluster on Oracle Cloud Infrastructure (OCI), running Google’s Online Boutique demo shop - eleven services plus the Redis database behind the cart, so twelve. One of the eleven is a load generator that sends fake shoppers on a daily schedule, Prometheus records the metrics, and a collector copies them to storage every hour. That pipeline, and the bug that left it a third short of every hour, is its own post. Turning it into a matrix took four decisions, and each one changes what PCA can find:
| Step | What I did | Why |
|---|---|---|
| Align time | Rounded every timestamp down to 5 minutes | Not every sample lands exactly on the mark; rows only line up on a shared clock |
| Pods to services | Added up each service’s pods | Pods are replaced on every deploy; services are what persists |
| Restarts | Dropped the restart counter | Mostly zero with rare spikes - it would dominate a component for the wrong reason |
| Gaps | Dropped 254 incomplete rows (1%), then standardised | Filling gaps invents values; scaling must come last |
That leaves 25,820 rows - five-minute snapshots over 91 days, minus the gaps - and 48 columns.
The part that clicked: directions, not columns#
Each row is a point in 48 dimensions. You cannot draw that, but like the cloud above, the points spread further in some directions than others, and PCA finds those directions, longest first.
First, standardise every column - mean 0, standard deviation 1. Otherwise memory, in tens of millions of bytes, outweighs CPU, in fractions of a core, purely because of units, and PCA “discovers” a fact about bytes, not about my cluster. Section 7 of the companion notebook does this on purpose so you can watch it happen.
Then the part the lectures did not make click until I ran it. The covariance matrix C records how
much each pair of columns varies together. Multiply any direction by C and you usually get a rotated
one. An eigenvector is a direction C only stretches, and the stretch, its eigenvalue, is the
variance of the data along it. So the eigenvector with the biggest eigenvalue is the direction the cloud
is longest along. The recipe is four lines:
C = X.T @ X / (len(X) - 1) # covariance: how much each pair of columns varies together
vals, vecs = np.linalg.eigh(C) # eigenvectors = directions, eigenvalues = variance along each
order = np.argsort(vals)[::-1] # eigh returns smallest first; PCA wants largest first
vals, vecs = vals[order], vecs[:, order]eigh is the variant for
symmetric matrices, which a covariance always is. Each eigenvector is a weight for every one of the 48
columns, and is called a principal component.
Before trusting it, I tested it on the picture’s data: two columns that were each a hidden signal plus
a little noise. PCA returned [0.707, 0.707] - that is 1/√2, because eigenvectors have length 1 -
carrying 96% of the variance. If it cannot find a signal I put there, I have no business believing what
it says about mine.
The first component is the day#
On the real data, PC1 carries 62.6% of the variance. Its weights are positive on 43 of the 48 columns. Network weights sit between 0.14 and 0.18 for every service, CPU is close to that for all but one spiky service, and memory mostly barely registers. To see when it is active, every moment gets a score: the dot product of its 48 values with PC1’s weights. I flipped the sign so that positive means busier than average. Averaged by hour:
Quietest at 02:00 UTC, busiest at 14:00, with a dip at 12:00. That is not a discovery - I wrote the load generator’s daily table, lunchtime dip and all. So this was the planted-answer test again, at full scale, and PCA recovered the shape I put in. It also explains the 170 pairs: every service gets busier together when traffic arrives.
What it is for, and what it costs#
The point is fewer, better columns. Fourteen components hold 95% of the variance, and keeping them is one multiplication:
Z = X @ vecs[:, :14] # 25,820 x 48 -> 25,820 x 14Forecasting can start from those 14 inputs, and a moment that does not fit the main components is a candidate anomaly. Both are plans - I have built neither yet. The lab also injects faults on a schedule, so some odd moments are there on purpose.
It is not free. Components are harder to read than panels - “PC4 is high” means nothing to whoever is on call. It only finds straight-line relationships. And it is sensitive to outliers: one service’s CPU sits near zero and then spikes hard, with a standard deviation nine times its mean, and spikes like that can pull a component towards them.
What tripped me up, and what I would tell you#
The order of two lines. My first version standardised the columns but never removed the incomplete rows, and the check that covariance equals correlation failed. Removing them after scaling would not have fixed it either - dropping rows shifts each column’s mean - so scaling has to come last.
If you try this on your own metrics: standardise first, or the column with the biggest units wins; plant an answer before you trust one, as in the notebook; and count your rows per hour before any maths, because a gap in the data looks like a pattern to PCA.
What I do not know yet#
PC2 carries 9.0% and PC3 3.9%. PC3 is mostly the memory of a few services, but the names are pseudonymised and I have not mapped them back, so I cannot say why. I would rather leave that open than guess.
Next is the calculus course, then the forecasting work. When I get there, I will write down whether starting from 14 components instead of 48 columns actually helped.
If you are further along and something above is wrong, please tell me. I will be glad to hear from you if you have any question(s) or feedback, have fun!