A table has many correlated columns, and you want a smaller representation that keeps the main patterns. PCA approaches this through directions of variation, while SVD decomposes a matrix into components that can also be used for compression and recommendation. Connecting PCA and SVD makes it easier to understand why these apparently different tasks share a calculation.
The shared calculation can be checked both numerically and visually. PCA's two derivations agree to about . A rank-10 SVD reduces a 4,096-pixel face to 1,290 numbers while keeping it recognisable. We derive both routes and reconstruct the image as components are removed.
PCA on the Iris Dataset
Watch the Iris dataset project from four dimensions down to two. Each point is a flower; each colour is a species:

Let's Build It
Click the badge to run this yourself:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
# Load and standardise the Iris dataset
iris = load_iris()
X = StandardScaler().fit_transform(iris.data) # 150 x 4
labels = iris.target
# PCA via eigen decomposition of the correlation matrix
cov_matrix = np.cov(X, rowvar=False) # 4 x 4
eigenvalues, eigenvectors = np.linalg.eigh(cov_matrix)
# Sort by decreasing eigenvalue
idx = np.argsort(eigenvalues)[::-1]
eigenvalues = eigenvalues[idx]
eigenvectors = eigenvectors[:, idx]
# Project onto the first two principal components
scores = X @ eigenvectors[:, :2]
Eigenvalues: [2.938 0.920 0.148 0.021]
Variance explained: [73.0% 22.9% 3.7% 0.5%]
Cumulative: [73.0% 95.8% 99.5% 100.0%]
Four features compressed to two, retaining 95.8% of the variance. Setosa separates cleanly on the first component alone; versicolor and virginica need both. If you have seen our post on LDA vs PCA, you will recognise this plot. The difference is that PCA found these axes without ever looking at the species labels.
What Just Happened?
Standardisation: Making Features Comparable
The original R code uses scale(X), which centres each feature to mean zero and divides by the standard deviation. Our Python equivalent is StandardScaler. Without this step, a feature measured in centimetres would dominate one measured in millimetres, simply because it has a larger variance. Standardising puts all features on equal footing, so PCA finds directions of genuine multivariate variation rather than tracking whichever variable has the largest units.
The R code explicitly notes this choice:
"You tend to use the covariance matrix when the variable scales are similar and the correlation matrix when variables are on different scales. Using the correlation matrix standardises the data."
When you standardise first and then compute the covariance matrix, you get the correlation matrix. Our code does exactly this.
Eigen Decomposition: Finding the Best Axes
The covariance matrix is a symmetric matrix (here 4 x 4). Its eigenvectors are the principal component directions; its eigenvalues are the variances along those directions.
The first eigenvector points in the direction that captures the most variance. The second, , captures the most remaining variance while being orthogonal to . And so on. The eigenvalue tells you how much variance that component explains.
Choosing How Many Components to Keep

The variance explained chart shows that two components capture 95.8% of the total variance. The third and fourth components contribute only 3.7% and 0.5%, respectively. For the Iris dataset, two dimensions are enough. For higher-dimensional datasets, you would look for the "elbow" in the cumulative curve, similar to the elbow method for choosing k in K-Means.
Going Deeper: The SVD Connection
Here is the key insight. There is a second way to compute PCA that never forms the covariance matrix. Instead, you decompose the centred data matrix directly using SVD:
where is (150 x 4), is , is with singular values on the diagonal, and is .
The columns of are the principal component directions (the eigenvectors we computed above). The singular values relate to the eigenvalues by:
Let's verify this:
# PCA via SVD of the centred data matrix
U, S, Vt = np.linalg.svd(X, full_matrices=False)
# Eigenvectors from SVD
V_svd = Vt.T
# Eigenvalues from singular values
n = X.shape[0]
eigenvalues_from_svd = S**2 / (n - 1)
Eigen SVD s²/(n-1) Difference
PC1 2.9381 2.9381 2.78e-16
PC2 0.9202 0.9202 2.22e-16
PC3 0.1477 0.1477 1.39e-17
PC4 0.0207 0.0207 4.34e-18

The differences are on the order of , which is machine epsilon for 64-bit floating point. The two methods are mathematically identical.
The Underlying Mechanism
Substitute the SVD into the covariance formula:
Since has orthonormal columns ():
This is exactly the eigendecomposition of , with eigenvectors and eigenvalues . The R code computes this as sdev <- s$d / sqrt(var) where var = nrow(X) - 1, then values <- sdev * sdev.
The Geometry of SVD
SVD says that every linear transformation can be decomposed into three steps: a rotation, a scaling, and another rotation. To see this, imagine applying the matrix to a unit circle:

- rotates the coordinate system to align with the principal directions.
- scales each axis by the corresponding singular value, stretching the circle into an ellipse.
- rotates the ellipse into its final orientation in the output space.
The singular values are the semi-axis lengths of the ellipse. The largest singular value corresponds to the direction of greatest "stretch," which is exactly the first principal component.
This geometric interpretation explains why PCA and SVD are the same: PCA finds the axes of the ellipse that best describe the data cloud. SVD finds the axes of the ellipse that describe any linear transformation. When the "transformation" is the centred data matrix itself, you get PCA.
Image Compression with Truncated SVD
The real power of SVD emerges when you truncate it. Keep only the top singular values and their corresponding vectors:
This is the best rank-k approximation in the Frobenius norm (the Eckart-Young-Mirsky theorem). Watch a face emerge as we increase the rank:

from sklearn.datasets import fetch_olivetti_faces
faces = fetch_olivetti_faces()
face = faces.images[0] # 64 x 64 pixels
U, S, Vt = np.linalg.svd(face, full_matrices=False)
# Reconstruct with k components
def reconstruct(U, S, Vt, k):
return U[:, :k] @ np.diag(S[:k]) @ Vt[:k, :]

The original image has 64 x 64 = 4,096 pixel values. A rank-10 approximation stores only 10 x (64 + 1 + 64) = 1,290 numbers (the 10 columns of U, 10 singular values, and 10 rows of V^T), just 31% of the original, yet the face is already recognisable. By rank 20, most fine detail is recovered at 63% of the original storage, a worthwhile trade when applied to thousands of images.
Why This Works: The Singular Value Spectrum

The singular values decay rapidly. The first few capture the coarse structure (overall brightness, face shape), while later ones encode fine detail and noise. Truncating at rank k discards the small singular values, keeping the signal and removing the noise. This is the same principle behind denoising, dimensionality reduction for regression (PCR uses exactly this truncation), and latent semantic analysis in NLP.
Where This Comes From
PCA and SVD have separate historical origins that converged over decades.
PCA was introduced by Karl Pearson in 1901 as a method for fitting lines and planes to point clouds. Harold Hotelling independently developed it in 1933 as a tool for analysing psychological test scores, introducing the term "principal components" and the eigenvalue formulation we use today.
"The object of this paper is to find a few 'principal components' of the scores, a number of hypothetical variables possessing certain optimal properties."
Hotelling, H. (1933). Analysis of a complex of statistical variables into principal components. Journal of Educational Psychology, 24(6), 417-441.
SVD was developed by Eugenio Beltrami (1873) and Camille Jordan (1874) for square matrices, and extended to rectangular matrices by Carl Eckart and Gale Young in 1936. The connection to low-rank approximation (the Eckart-Young theorem) made SVD fundamental to numerical linear algebra. Gene Golub and William Kahan published the definitive numerical algorithm in 1965, later refined in Golub and Van Loan's classic textbook Matrix Computations (1996).
The practical equivalence of PCA and SVD was well understood by the 1960s, but using SVD for PCA became the standard numerical approach because it avoids forming explicitly. Squaring a matrix squares its condition number, amplifying numerical errors. SVD works directly with the data matrix, giving more accurate results for ill-conditioned datasets. This is why scikit-learn's PCA uses SVD internally, not eigen decomposition.
The original R code in this post's companion repository demonstrates both paths: eigen(cor(X)) for the eigenvalue approach and svd(scale(X), nu=0) for the SVD approach, verifying that they give the same eigenvalues, eigenvectors, and scores.
Further Reading
- The original PCA paper: Hotelling, H. (1933). Analysis of a complex of statistical variables into principal components. Journal of Educational Psychology, 24(6), 417-441.
- The SVD numerical algorithm: Golub, G. & Van Loan, C. (1996). Matrix Computations (3rd ed.). Johns Hopkins University Press. Chapters 2 and 8 cover SVD theory and computation.
- The low-rank approximation theorem: Eckart, C. & Young, G. (1936). The approximation of one matrix by another of lower rank. Psychometrika, 1(3), 211-218.
- Next algorithm to learn: For a supervised version of PCA, see our post on LDA vs PCA, which uses class labels to find directions that separate groups rather than maximise variance.
Try It Yourself
- Try a larger dataset. Replace Iris with the Wine or Breast Cancer dataset from scikit-learn. How many components do you need to capture 95% of the variance?
- Compress a colour image. Apply SVD separately to the R, G, and B channels. How does the compression ratio compare to greyscale?
- Denoise with truncated SVD. Add Gaussian noise to a face image, then reconstruct at various ranks. Find the rank that minimises reconstruction error on a held-out clean image.
- Compare with scikit-learn. Run
sklearn.decomposition.PCAon the Iris data and verify that itscomponents_match our eigenvectors and itsexplained_variance_matches our eigenvalues.
Interactive Tools
- PCA Visualizer: Drag a point cloud and watch the principal components, scree plot and explained variance follow, then project onto one component to see what the reduction discards.
- Eigenvalue and SVD Visualizer: Load the covariance-matrix preset and read the principal components straight off the plot as eigenvectors, then switch to the SVD tab to see the unit circle become an ellipse through the rotate-scale-rotate decomposition this post derives.
- Distribution Explorer: Visualise the multivariate Gaussian whose covariance matrix PCA diagonalises.
Related Posts
- LDA vs PCA: Supervised Meets Unsupervised Dimensionality Reduction: PCA finds directions of maximum variance; LDA finds directions of maximum class separation. This post covers the mechanics of both eigenvalue problems.
- Probabilistic PCA, EM-PCA, Bayesian PCA, Factor Analysis: Recasts the eigendecomposition derived here as a Gaussian latent-variable model, opening the door to EM, missing data, and automatic dimensionality selection.
- PCR vs PLS: When Fewer Features Beat More: Principal Component Regression uses PCA's truncated components for prediction, while PLS finds directions that maximise covariance with the response.
- From K-Means to GMM: Hard vs Soft Clustering: K-Means uses Euclidean distance in the original feature space; PCA can reduce dimensionality before clustering to improve performance.
- Gaussian Mixture Models: The EM Algorithm in Practice: GMMs model clusters as ellipsoids whose shape is determined by covariance matrices, the same matrices PCA diagonalises.
- Candlestick Pattern Discovery with K-Means: Uses StandardScaler and K-Means on financial features, where standardisation (the preprocessing step for PCA) is critical.
- SVD for Recommender Systems: Matrix Factorisation in NumPy: SVD put to work: the same truncated factorisation applied to a sparse user-item matrix to predict missing ratings.







