Dimensionality Reduction
Curse of dimensionality, the six PCA steps, and autoencoders as non-linear PCA.
Unsupervised learning, part one. Two methods, one goal: keep the information, drop the dimensions.
The taxonomy you should be able to draw

> Do you have labeled data? Yes → supervised → what to predict? category → classification (kNN, logistic regression, SVM, decision trees); numeric → regression. No → unsupervised → group the data? yes → clustering (k-means, hierarchical); no → dimensionality reduction (PCA, autoencoder).
Unsupervised learning splits three ways: clustering (homogeneous subgroups), dimensionality reduction (a lower-dimensional representation that characterises and visualises the data), density estimation (approximates the true data distribution).

Why unsupervised is hard: the goal is not clearly defined, performance is difficult to assess, and the data is high-dimensional.
The curse of dimensionality
Four consequences, each with its own bullet:
- With more features $d$, more samples $N$ are required → overfitting.
- High-dimensional data is difficult to visualise → loss of interpretability of the underlying structure.
- It is computationally expensive to analyse and process.
- It can be sparse, with many features having little or no variation → uninformative features.

The scale of the problem on the deck's examples: a $2400\times2400$ image is 5.76 million dimensions; a 3-D CT volume of $512\times512\times700$ is 183 million.


The "more features is better" belief is the misconception being attacked, and the performance-versus-number-of-features curve (rise, peak, decline) is the reason dimensionality reduction exists at all.

What reduction buys you
- Reduction of features retaining as much information as possible
- Fewer dimensions → fewer parameters → simpler models
- Data compression keeping maximum information
- Visualisation — understanding data in 2-D
- Noise reduction — discovering the intrinsic dimensionality
PCA
> Assumption: the data lies on a lower-dimensional space. PCA is an unsupervised linear transformation that finds the direction of maximum variance in the reduced space, producing a smaller number of highly uncorrelated features.

It constructs a $d\times k$ transformation matrix $W$ and projects:
$$Z = XW, \qquad X \in \mathbb{R}^{N\times d},\; W\in\mathbb{R}^{d\times k},\; Z\in\mathbb{R}^{N\times k}$$

The six steps — reproduce this list
- Standardise the $d$-dimensional dataset (zero mean, unit variance).
- Construct the $d\times d$ covariance matrix.
- Decompose it into eigenvectors and eigenvalues.
- Select the $k$ eigenvectors corresponding to the $k$ largest eigenvalues ($k \ll d$).
- Construct the projection matrix $W$ from those top $k$ eigenvectors.
- Transform $X$ with $W$ to obtain the new $k$-dimensional feature subspace.

Step 1 is not optional. PCA maximises variance, and variance is scale-dependent: a feature in dollars has variance $10^6$ times a feature in metres, so un-standardised PCA simply returns that one feature as PC1. The notebook's StandardScaler().fit_transform(...) before PCA(...) is the code counterpart of this step.
Choosing $k$ — cumulative explained variance. Each eigenvalue divided by the sum of eigenvalues is the proportion of variance carried by that component; take the smallest $k$ whose cumulative sum crosses your threshold (90% in the assignment).
$$\text{explained variance ratio}_j = \frac{\lambda_j}{\sum_{i=1}^{d}\lambda_i}$$
Where PCA is used in the course
- Breast cancer Wisconsin, 569×30 → 5 components carry ~90% of the variance, and KNN accuracy actually improves from 0.93 to 0.97.
- 8×8 digits, 1797×64 → 2 components and KNN still reaches ~61%.
- MRI compression: a 128×96 slice compressed to 16 components and reconstructed, with an error map to show what was lost.


Autoencoders
> A special form of neural network whose input and output are the same; the goal is to minimise reconstruction error.

Two components:
- Encoder $E$ — maps the input to a lower-dimensional latent code
- Decoder $D$ — reconstructs the input from the latent code
$$z = E(x) = f_E(W_1x), \qquad y = D(z) = f_D(W_2z)$$
$$ \min_{W_1,W_2}\; \frac{1}{2N}\sum_{n=1}^{N}\big\|x^{(n)} - y^{(n)}\big\|^2 $$

"PCA vs autoencoder?" PCA is a linear projection: it finds the $k$ orthogonal directions of maximum variance via the eigendecomposition of the covariance matrix, and the encoder/decoder are a single fixed matrix pair with a closed-form solution. An autoencoder is a neural network trained by gradient descent to minimise reconstruction error; the encoder and decoder are learned non-linear maps. If both $f_E$ and $f_D$ are linear, the autoencoder's optimum coincides with PCA — the slide states this explicitly — so the autoencoder is best understood as non-linear PCA. Consequences: the autoencoder can capture curved manifolds that PCA cannot, but it needs more data, a training procedure and hyperparameters, and its latent dimensions are not ordered by explained variance nor guaranteed orthogonal.
The slide's own probe — "Is it supervised or unsupervised?" — has a precise answer worth giving: self-supervised. There are no external labels, but there are targets, namely the inputs themselves. That is why it sits in the unsupervised half of the taxonomy while still being trained with a supervised-style loss.






















































