Knowledge / Artificial Intelligence and Data Science / Dimensionality reduction and PCA

Dimensionality reduction and PCA

Artificial Intelligence and Data Science (B.E./B.Tech) · Machine learning · free study notes for exams, placements and interviews

In short

Dimensionality reduction maps data with p features into k ≪ p new features that keep most of the information, either by selecting features or by building new ones as combinations of the old. Principal Component Analysis (PCA) is the standard linear method: it finds orthogonal directions of maximum variance, which turn out to be the eigenvectors of the covariance matrix (equivalently the right singular vectors of the centred data matrix). It is examined heavily in university ML and pattern-recognition papers, in GATE CS/DA (linear algebra plus ML sections) and in almost every data-science interview, because eigen-decomposition and variance accounting can be tested with small numbers. In Indian practice it appears in credit-risk scoring, biometric and face recognition, hyperspectral remote sensing at NRSC/ISRO, sensor and telecom monitoring, and as a preprocessing step before regression, clustering or anomaly detection.

Explained

Why reduce dimensions — the curse of dimensionality

PCA: idea, derivation and algorithm

SVD route and numerical practice

Choosing k, and reading the output

Assumptions, advantages, limitations

Other dimensionality-reduction methods (comparisons examiners ask for)

Key points

Formulas and examples

Mean-centring: x̃ᵢⱼ = xᵢⱼ − x̄ⱼ (x̄ⱼ = column mean, same unit as the feature)
Covariance matrix: Σ = X̃ᵀX̃/(n − 1), size p × p, units = (unit of feature)²
Correlation-matrix PCA: standardise zᵢⱼ = (xᵢⱼ − x̄ⱼ)/sⱼ, then Σ becomes the correlation matrix R (dimensionless, trace = p)
Projection objective: maximise wᵀΣw subject to wᵀw = 1 ⇒ Σw = λw
Variance along component i = λᵢ; total variance = Σᵢλᵢ = trace(Σ)
Explained variance ratio of PCᵢ = λᵢ/Σⱼλⱼ; cumulative = (λ₁+…+λk)/Σⱼλⱼ
Scores: Z = X̃W (n × k); Reconstruction: X̂ = ZWᵀ + x̄
Residual (lost) variance = λ(k+1) + … + λp
SVD link: X̃ = UDVᵀ, W = V, λᵢ = σᵢ²/(n − 1), Z = UD
Eigenvalues of a 2×2 Σ: λ = [tr ± √(tr² − 4·det)]/2, tr = Σ₁₁+Σ₂₂, det = Σ₁₁Σ₂₂ − Σ₁₂²
Whitening: z*ᵢ = zᵢ/√λᵢ (unit variance)
Fisher/LDA criterion: J(w) = wᵀS_B w / wᵀS_W w ⇒ S_W⁻¹S_B w = λw
Johnson–Lindenstrauss: k ≥ O(ε⁻² log n) preserves pairwise distances within (1 ± ε)

Example 1 — full 2-D PCA by hand (the standard exam question). Marks of 5 students in Maths (M) and Physics (P): M = 4, 8, 13, 7, 18 ; P = 5, 9, 11, 9, 16. Both means are 10.

StudentM devP dev(M dev)²(P dev)²product
1−6−5362530
2−2−1412
331913
4−3−1913
586643648
Σ001226486

Example 2 — choosing k from eigenvalues. Correlation-matrix PCA on p = 7 features gives λ = 3.0, 1.6, 1.1, 0.7, 0.3, 0.2, 0.1 (sum = 7 = p, as it must be).

PCλShareCumulative
13.042.86%42.86%
21.622.86%65.71%
31.115.71%81.43%
40.710.00%91.43%
50.34.29%95.71%
60.22.86%98.57%
70.11.43%100%

Kaiser rule (λ > 1) keeps 3 components (81.4%); a 90% variance target needs 4 (91.4%); the scree elbow is after PC3. Answer: k = 3 for interpretation, k = 4 if 90% is specified.

Example 3 — why standardisation matters. Two features: annual salary (sd ₹2,00,000 ⇒ variance 4 × 10¹⁰ rupee²) and experience (sd 3 years ⇒ variance 9 year²), correlation 0.4 ⇒ covariance = 0.4 × 2,00,000 × 3 = 2.4 × 10⁵.

Example 4 — compression and reconstruction. 1,000 grayscale face images of 64 × 64 = 4,096 pixels, keeping 50 components ("eigenfaces").

Example 5 — eigenvalues from singular values. Centred X with n = 101 samples has singular values 12, 5, 2.

from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

pipe = make_pipeline(StandardScaler(), PCA(n_components=0.95, svd_solver="full"))
Z = pipe.fit_transform(X_train)          # fit on train only
Z_test = pipe.transform(X_test)          # same mean, sd and components
pca = pipe.named_steps["pca"]
print(pca.n_components_, pca.explained_variance_ratio_.cumsum())
X_back = pipe.inverse_transform(Z)       # approximate reconstruction
MethodSupervised?Linear?Inverse transformTypical use
PCANoYesYes (exact formula)Compression, decorrelation, preprocessing
LDAYesYesNoClassification with ≤ C−1 dims
Kernel PCANoNoApproximate onlyCurved manifolds, moderate n
t-SNE / UMAPNoNoNo / approximate2-D visualisation of clusters
AutoencoderNoNoYes (decoder)Images, text, non-linear compression
Random projectionNoYesApproximateVery high p, speed-critical
Feature selectionEither—Not neededInterpretability, regulatory models

Where it is used

Interview questions

Common mistakes

Related topics

Feature selection and model interpretability · Clustering: k-means and hierarchical · Overfitting, bias-variance and regularisation · Anomaly detection · Machine learning workflow · Linear algebra: eigenvalues and SVD

Open in the Knowledge reader →Find jobs →