Sub topic 1
Why Reduce Dimensionality
Prerequisites
This chapter assumes you know basic Python and NumPy, and that you can compute a mean, a variance and a correlation. You should also be comfortable with feature matrices, where rows are samples and columns are features. Later chapters add matrix multiplication and the idea of a basis.
What dimensionality means
The dimensionality of a dataset is its number of features. A table of 200 customers with 3 measurements each has 3 dimensions. A set of 8x8 pixel images has 64 dimensions. Dimensionality reduction maps each sample to a new representation with fewer features while keeping the information that matters for your task.
Principal component analysis (PCA) does this with a linear map. It builds new features that are combinations of the old ones. Those new features are uncorrelated, and they are ordered by how much variance they capture. The rest of this tutorial builds that idea step by step. This chapter explains why you would want it.
Four reasons to reduce dimensions
Redundancy
Real features are often correlated. Height in centimeters and height in inches carry the same information. Two sensors placed next to each other report nearly the same signal. Correlated features mean the data really occupies fewer dimensions than the column count suggests. Dropping the redundancy loses little.
Noise
Noise tends to spread across all directions with similar size. Signal tends to concentrate in a few directions. If you keep only the high-variance directions, you keep most of the signal and discard much of the noise. This is a heuristic, not a guarantee, because a low-variance direction can still be the one that matters for your target.
Visualization
You can plot two or three dimensions. You cannot plot 64. Projecting to two dimensions lets you look for clusters, outliers and trends before you build a model.
Computation and model quality
Fewer features mean less memory and faster training. Fewer features can also reduce overfitting. With many features and few samples, a model can fit noise. Distances also become less informative as dimensions grow, a problem often called the curse of dimensionality. Methods that rely on distance, such as k-nearest neighbors, suffer most.
| Goal | What reduction gives you | Main risk |
|---|---|---|
| Remove redundancy | Fewer, uncorrelated features | Dropping a useful direction |
| Reduce noise | Cleaner signal | Discarding low-variance signal |
| Visualize | A 2D or 3D plot | Distortion of the full structure |
| Speed up models | Less memory and time | Loss of predictive information |
A small example of redundancy
The following code builds three features. The second is almost a copy of the first, and the third is nearly constant. The correlation matrix shows the redundancy directly.
import numpy as np
rng = np.random.default_rng(0)
a = rng.normal(size=500)
b = 2.0 * a + rng.normal(scale=0.1, size=500)
c = rng.normal(scale=0.05, size=500)
X = np.column_stack([a, b, c])
print(np.round(np.corrcoef(X, rowvar=False), 2))
print(np.round(X.var(axis=0), 3))Features a and b have a correlation near 1.0, so they describe one underlying quantity. Feature c has a tiny variance and adds almost nothing. Although the table has 3 columns, almost all of its variation lies along one direction. PCA finds that direction automatically, even when it is not aligned with any single column.
What you give up
Reduction is lossy. Each step that removes dimensions discards some information, and you cannot recover it exactly. PCA also optimizes variance, not usefulness for your task. The new features are mixtures of the originals, so they are harder to explain to someone else. Later chapters show how to measure the loss and how to choose how many dimensions to keep.
Where this chapter leads
To do this properly you need three tools. The first is the covariance matrix, which records how features vary together. The second is a way to find the directions along which variance is largest, which uses eigenvectors. The third is a way to project data onto those directions and map it back. The next chapters introduce each one.
Exercise
A dataset has 50 features, and 40 of them are near-copies of the other 10 with a little noise added. Name two benefits of reducing it to about 10 dimensions, and one risk.
Show answer
Benefits: the redundancy is removed, so training is faster and uses less memory, and some noise is discarded, which can reduce overfitting. Risk: PCA keeps high-variance directions, so if a useful signal lies in a low-variance direction it will be lost.
Exercise
In the code example, why does feature c add almost nothing to the dataset, even though it is a separate column?
Show answer
Its variance is very small (about 0.0025), so it barely changes between samples. A feature that hardly varies cannot help tell samples apart.