Notes on PCA (principal component analysis).
0. What problem does PCA solve? (Why PCA)
High-dimensional data is everywhere, and it typically has:
- High correlation between dimensions
- Lots of noise, making the structure hard to observe
- Downstream models (linear regression / classification) that suffer badly from redundant dimensions
- A need for dimensionality reduction to lower complexity and improve visualization, computational efficiency and generalization
Goal:
Find a set of new orthogonal directions such that
1) each direction carries as much “information” as possible,
2) the directions are mutually uncorrelated,
3) a small number of directions is enough to approximate the original data.
These directions are the principal components.
1. PCA data preprocessing
Dataset:
Centering:
Reasons:
- It makes variance = second moment:
- The covariance matrix simplifies to
- Otherwise the mean complicates the entire analysis
Define the data matrix:
Covariance matrix (sample estimate):
Properties of the covariance matrix:
is a symmetric matrix: is positive semi-definite: for any , - The eigenvalues of
are all non-negative real numbers - The eigenvectors of
are mutually orthogonal
2. The core mathematical objective of PCA
2.1 Definition of the projection
Given a direction vector
Convention: throughout this post we use the
notation, i.e. a row vector times a column vector gives a scalar.
2.2 Why measure information by variance?
Intuition:
- If all points project to the same location (variance = 0), that direction cannot distinguish the data
- The more spread out the projections (the larger the variance), the more information the direction carries
Mathematical formulation: the information in direction
Since the data is centered,
2.3 Matrix form of the projected variance
Since
This is the core formula of PCA; every derivation that follows is equivalent to maximizing it.
2.4 The PCA optimization problem
Constraint: to prevent the variance from growing without bound as
So the PCA optimization problem is:
Subsequent principal components need an additional orthogonality constraint (keeping the directions mutually orthogonal):
3. The “three rigorous derivations” of PCA
3.1 Derivation 1: maximizing the projected variance (Rayleigh Quotient)
1) Detailed derivation of the one-dimensional principal component
Step 1: Set up the optimization problem
We want to find a unit vector
Step 2: Construct the Lagrangian
Introduce a Lagrange multiplier
Step 3: Differentiate and set to zero
Differentiating with respect to
(because is symmetric)
Therefore:
Simplifying:
This is exactly an eigenvalue problem!
Step 4: Determine which eigenvector is the optimum
Substitute
Therefore, projected variance = eigenvalue
To maximize the variance, we should choose the eigenvector
Step 5: Understanding the Rayleigh Quotient
Define the Rayleigh quotient:
For any non-zero vector
- The maximum of
= (the largest eigenvalue) - The minimum of
= (the smallest eigenvalue) - The extrema are attained exactly at the corresponding eigenvectors
Intuition: the Rayleigh quotient measures how much the vector
2) Derivation of the second principal component
Step 1: Add the orthogonality constraint
The second principal component
- Unit length:
- Orthogonality to the first principal component:
Optimization problem:
Step 2: Lagrangian
Step 3: Differentiate
Step 4: Use orthogonality to eliminate
Left-multiply by
Since
We get
Step 5: Substitute back to obtain the eigen-equation
3) General derivation for K principal components
The optimization problem in matrix form
Find a matrix
Objective function (total variance):
Why the trace?
Solving via the eigendecomposition
Let the eigendecomposition of
where
Key lemma: for any matrix
Equality holds if and only if the columns of
Proof sketch:
Let
Let
Since $Q^\top Q = IK
To maximize the expression above, the
Final conclusion:
The optimal projection matrix consists of the eigenvectors corresponding to the
3.2 Derivation 2: minimizing the reconstruction error (Reconstruction View)
This viewpoint interprets PCA as a best linear approximation problem: approximating high-dimensional data with a low-dimensional subspace.
(1) Detailed derivation of the one-dimensional case
Step 1: Define the reconstruction process
Given a unit direction
- Projection: compute the coordinate along
, (a scalar) - Reconstruction:
Geometric interpretation:
Step 2: Compute the reconstruction error of a single point
Expanding this norm:
Step 3: Compute the average reconstruction error
Split it into two parts:
Therefore:
Step 4: Equivalence
Minimizing the reconstruction error
Intuition:
- Total energy = retained energy + lost energy
- Retaining more (
large) losing less ( small)
(2) Detailed derivation of the K-dimensional case
Step 1: Define the K-dimensional projection space
Let
The columns of
Step 2: The projection matrix
The projection matrix onto the subspace spanned by
Properties of the projection matrix:
(idempotence) (symmetry)
Step 3: The reconstruction process
where
Step 4: Compute the reconstruction error
Let
(using $P\perp^2 = P\perp, P\perp^\top = P\perp$)
Step 5: Matrix form of the average error
Using the linearity of the trace:
Therefore:
Step 6: The optimal solution
By the conclusion of Derivation 1, the optimal solution is:
Information retention ratio:
This ratio is commonly used to choose the size of
3.3 Derivation 3: the SVD view (the most practical and most stable)
This viewpoint starts directly from the data matrix and uses the singular value decomposition to establish the connection with PCA.
(1) SVD recap
Any matrix
where:
: the matrix of left singular vectors, : a diagonal matrix (singular values ) : the matrix of right singular vectors,
Economy SVD (when
where
(2) Relationship between the SVD and the covariance matrix
Covariance matrix:
(using
Let
where
Core conclusions:
- The eigenvectors of
= the left singular vectors of - The eigenvalues of
= the squared singular values divided by
Therefore, the PCA principal component directions = the left singular vectors of the SVD!
(3) Best low-rank approximation (the Eckart-Young theorem)
Theorem: under the Frobenius norm, the best rank-
where
Approximation error:
Connection with PCA:
The PCA reconstruction:
One can verify that this is equivalent to the SVD low-rank approximation:
(4) Why is the SVD more stable than the eigendecomposition?
| Aspect | Eigendecomposition | SVD |
|---|---|---|
| Object computed | ||
| Numerical stability | Computing | More stable |
| Condition number | ||
| Singular cases | May be numerically unstable | Handles rank deficiency naturally |
Dual PCA (when
Compute the eigendecomposition of
Then recover the left singular vectors:
(5) Algorithm for implementing PCA via SVD
1 | 输入: 数据矩阵 X ∈ R^{D×N},目标维度 K |
4. Geometric interpretation of PCA
4.1 The ellipsoid and its principal axes
The covariance matrix
Principal axes of the ellipsoid:
- Directions = the eigenvectors of
- Lengths ∝
(the square roots of the eigenvalues)
The principal components found by PCA are exactly the principal axes of this ellipsoid!
4.2 Rotating into the principal-axis coordinate system
Covariance matrix of the original data:
After the projection
A diagonal matrix! This means:
- The principal components are uncorrelated with one another
- The variance of the
-th principal component =
4.3 Geometric meaning of the maximum-variance direction
Imagine the data points forming a cloud:
- First principal component: the direction in which the cloud is “longest”
- Second principal component: the longest direction within the plane perpendicular to the first principal component
- And so on…
1 | PC1 |
5. Practical applications of PCA
5.1 Python implementation
From scratch
1 | import numpy as np |
Using scikit-learn
1 | from sklearn.decomposition import PCA |
5.2 Application scenarios
| Application | Description |
|---|---|
| Data visualization | Reduce high-dimensional data to 2D/3D for visualization |
| Noise filtering | Remove the components corresponding to small eigenvalues (noise) |
| Feature extraction | As a preprocessing step for machine learning |
| Data compression | Image compression, signal compression |
| Anomaly detection | Points with a large reconstruction error may be anomalies |
6. Limitations and extensions of PCA
6.1 Limitations of PCA
| Limitation | Description |
|---|---|
| Linearity assumption | Can only capture linear relationships |
| Variance assumption | Assumes large variance = important information |
| Orthogonality constraint | Principal components must be orthogonal, which may be unnatural |
| Sensitivity to outliers | Based on the covariance matrix, so heavily affected by extreme values |
| Sensitivity to scale | Features on different scales affect the result |
6.2 Extended algorithms
(1) Probabilistic PCA (PPCA)
The probabilistic model view:
Advantages:
- Can handle missing values
- Provides a probabilistic interpretation
- Can be solved with the EM algorithm
(2) Sparse PCA
Add a sparsity constraint:
Advantage: the principal components are more interpretable (each involves only a few original features)1
2
3
4from sklearn.decomposition import SparsePCA
spca = SparsePCA(n_components=2, alpha=0.1)
X_spca = spca.fit_transform(X)
(3) Incremental PCA
Handles large-scale data by computing in batches:1
2
3
4
5
6from sklearn.decomposition import IncrementalPCA
ipca = IncrementalPCA(n_components=2, batch_size=100)
for batch in data_generator:
ipca.partial_fit(batch)
X_ipca = ipca.transform(X)
(4) Robust PCA
Decompose into low-rank + sparse:
Used to remove sparse noise / outliers.
7. Summary
7.1 Unifying the three derivations
| View | Objective | Optimal solution | ||
|---|---|---|---|---|
| Maximum variance | Top eigenvectors of | |||
| Minimum reconstruction error | $\min \ | X - \hat{X}\ | ^2$ | Top eigenvectors of |
| SVD low-rank approximation | $\min \ | X - \hat{X}\ | _F$ | First |
All three are equivalent!
7.2 Quick reference for the core formulas
| Concept | Formula | ||
|---|---|---|---|
| Projection | |||
| Reconstruction | |||
| Projected variance | |||
| Reconstruction error | $ | x - \hat{x} | ^2 = \sum_{i>K} \lambda_i$ |
| Variance retention ratio | |||
| SVD–eigenvalue relationship |
Translated from the Chinese original.

