Skip to content
ml-recall

Topic notes

Dimensionality Reduction

Fewer features with little information lost - PCA via eigendecomposition or SVD, explained variance, LSA and NMF, and non-linear maps such as kernel PCA, t-SNE and autoencoders.

4-min read AI-checked

Practise 39 questions Take a test

Key ideas

Formulas

Worked example

Centred 2-D data with covariance C=[5222]C = \begin{bmatrix} 5 & 2 \\ 2 & 2 \end{bmatrix}.

In code

import numpy as np
from sklearn.decomposition import PCA

rng = np.random.default_rng(0)
X = rng.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=500)
pca = PCA().fit(X)
print(pca.components_.round(2))             # [[ 0.9   0.44] [-0.44  0.9 ]]  rows = axes
print(pca.explained_variance_.round(2))     # [5.83 0.94]
print(np.linalg.eigvalsh(np.cov(X.T))[::-1].round(2))       # [5.83 0.94]  covariance eigenvalues
print((pca.singular_values_**2 / (len(X) - 1)).round(2))    # [5.83 0.94]  sigma^2 / (n - 1)
print(pca.explained_variance_ratio_.round(3))               # [0.861 0.139]

The sample estimates are close to the exact values (2,1)/5≈(0.89,0.45)(2, 1)/\sqrt5 \approx (0.89, 0.45), 6 and 1.

Common pitfalls

References

  1. scikit-learn User Guide: Decomposing signals in components — Sec. 2.5.1 PCA; 2.5.2 Kernel PCA; 2.5.3 Truncated SVD and latent semantic analysis; 2.5.5 Factor Analysis; 2.5.6 ICA; 2.5.7 NMF
  2. scikit-learn API: sklearn.decomposition.PCA — Parameters n_components, whiten; attributes components_, explained_variance_, explained_variance_ratio_, singular_values_
  3. Wikipedia: Principal component analysis — Overview; Intuition (scree plot); Details (First component, Covariances, Dimensionality reduction, Singular value decomposition); Further considerations; Properties and limitations; Computation using the covariance method (cumulative energy)
  4. Wikipedia: Dimensionality reduction — Introduction; Feature selection; Feature projection (Kernel PCA, Graph-based kernel PCA, t-SNE, UMAP)
  5. Wattenberg, Viégas & Johnson, How to Use t-SNE Effectively (Distill, 2016) — Sections 1–4 (hyperparameters, cluster sizes, distances between clusters, random noise)
  6. Wikipedia: Autoencoder — Applications — Dimensionality reduction — Principal component analysis