PCA Algorithm

Version 9 | Created: 2026-08-11 | Updated: 2026-11-07 13:46 UTC
- 1. Notation
- 2. The Procedure
- 3. Two Routes To The Same Answer
- 4. What Comes Out
- 5. Choosing The Component Count
- 6. Applying It To New Data
- 7. Conventions And Numerical Details
- Appendix A. Terminology
The other documents in this folder say which variant to reach for. This one says what the plain procedure actually does. It fixes the notation, walks the steps in order, states what each step produces, and lists the conventions that decide whether two implementations agree.
The PCA algorithm is a procedure for moving data expressed in
- The loadings
$V_k$ ($p \times k$ ) are the new axes themselves —$k$ orthogonal directions listed from the one along which the data spreads most. Each axis records the combination of the original variables it is built from, which is what answers where the variation in the data mainly comes from. - The scores
$T$ ($n \times k$ ) are the coordinates on the new axes — each sample moved from$p$ dimensions to$k$ , and the data that regression, classification, or any other downstream model actually receives. This is the product of the dimension reduction. - The explained variance
$\lambda_j$ is the weight of each axis — the fraction of the total variation each component carries. Since the sum of the discarded$\lambda_j$ equals the reconstruction error (the last paragraph of §4), it gives a quantitative answer to what is lost by keeping only$k$ components.
PCA (Principal Component Analysis) is short enough to write out in full, and writing it out settles questions that a library call hides — what the sign of a component means, why the rank stops at
Table 1. Symbols used throughout
| Symbol | Shape | Meaning |
|---|---|---|
| The data matrix, one sample per row and one variable per column | ||
| — | The number of samples | |
| — | The number of variables | |
| — | The number of components kept | |
| The column means of |
||
| The column standard deviations of |
||
| The centered, optionally scaled data | ||
| The loadings, one principal direction per column | ||
| The scores, the coordinates of each sample in the new basis | ||
| — | The variance explained by component |
|
| — | The rank of |
The loadings are the new axes and the scores are the coordinates on them. The two are easy to confuse, because they come out of the same decomposition and both get called "the components" in casual use.
Step 1 is not optional. Skip the centering and the first component points at the mean of the data rather than at the direction of its variation, because the criterion being maximized becomes the second moment about the origin instead of the variance.
Step 2 is a modelling choice. Without scaling, the variable with the largest numerical range dominates, so a table that mixes nanometres with ohms has to be scaled. With scaling the decomposition runs on the correlation matrix instead of the covariance matrix, and the two give different components.
The decomposition in step 3 can be reached by either of two routes, and they agree in exact arithmetic.
Table 2. Covariance route against SVD route
| Aspect | Covariance route | SVD route |
|---|---|---|
| What is formed |
|
The SVD of |
| Loadings | The eigenvectors of |
The columns of |
| Variance | The eigenvalues of |
|
| Cost | ||
| Conditioning | Forming |
It works on |
The SVD route is the default, and the reason is numerical. Squaring the condition number means that variables which are nearly collinear — the normal state of measurement data — lose roughly half the available digits before the eigensolver ever starts. The covariance route stays useful when
Table 3. The three outputs and what each is for
| Output | Shape | Reading |
|---|---|---|
| Loadings |
How much each original variable contributes to each component | |
| Scores |
Where each sample sits in the reduced space, and the input to any downstream model | |
| Explained variance |
How much of the total variation each component accounts for |
The explained variance ratio is
Reconstruction follows the same path backwards. With
Table 4. Ways to fix
| Method | Rule | Caution |
|---|---|---|
| Cumulative variance | Keep enough components to pass a stated fraction | The fraction is a choice, not a finding |
| Scree | Keep the components before the bend in the eigenvalue plot | The bend is often not sharp |
| Kaiser | On the correlation matrix, keep eigenvalues above one | Crude, and it tends to keep too many |
| Parallel analysis | Keep the components whose eigenvalues exceed those of shuffled data | It needs repeated runs but answers the right question |
| Cross-validation | Keep the count that minimizes held-out reconstruction error | The most defensible, and the most expensive |
Parallel analysis compares the eigenvalues against a null in which there is no structure, and cross-validation asks directly which count generalizes. The other three settle the question by a threshold the reader has to choose.
A fitted PCA is three stored objects:
# Python
T_new = ((X_new - mu) / sigma) @ V_k # scores for new rows
X_hat = (T_new @ V_k.T) * sigma + mu # back to the original unitsRecomputing
The same rule governs cross-validation. The centering, the scaling, and the decomposition all belong inside each training fold; performing them once on the whole dataset lets the held-out rows influence the axes they are later scored against.
Table 5. Details that decide whether two implementations agree
| Detail | Statement |
|---|---|
| Sign |
|
| Rank | Centering removes one degree of freedom, so |
| Denominator |
|
| Ties | Equal eigenvalues leave the rotation inside that subspace undetermined, so individual loadings are not interpretable there |
| Missing values | The decomposition has no notion of a missing entry, so it must be imputed first or a probabilistic variant used instead |
The sign convention matters more than it appears. Two runs that differ only in sign produce loading plots that look mirrored and scores that flip about zero, which reads as a change in the data when it is nothing of the kind.
The terms below appear in the body without being defined there.
- Condition number measures how much a matrix amplifies relative error, so a large one means fewer reliable digits in the result.
- Correlation matrix is the covariance matrix of the standardized variables.
- Covariance matrix holds the pairwise covariances of the variables, with the variances on its diagonal.
- Eigendecomposition writes a symmetric matrix as its eigenvectors scaled by its eigenvalues.
- Kaiser rule keeps the components whose eigenvalue on the correlation matrix exceeds one.
- Leakage is the use of information from held-out data while fitting, which makes a validation estimate optimistic.
- Loading is the coefficient of an original variable in a principal component.
- Parallel analysis compares the observed eigenvalues against those obtained from data with the same shape but no structure.
- Rank is the number of linearly independent directions a matrix spans.
- Score is the coordinate of a sample along a principal component.
- Scree is the plot of eigenvalues in decreasing order, whose bend is taken as the component count.
- SVD decomposes a matrix into left singular vectors, singular values, and right singular vectors.
- Thin SVD returns only the singular vectors that correspond to non-zero singular values.
