Dimensionality Reduction
Dimensionality reduction, principal component analysis, manifold learning, and techniques for preserving useful structure in lower-dimensional spaces.
Dimensionality reduction transforms data from a feature space with dimension into a smaller space with dimension , where , while preserving as much useful information as possible.
It can reduce training time, simplify visualization, remove noise, and sometimes improve model performance. However, it usually loses some information and makes the machine learning pipeline more complex. A model should therefore be trained on the original features before dimensionality reduction is considered.
Curse of Dimensionality
Definition (Curse of dimensionality).
The curse of dimensionality refers to the mathematical and computational problems that arise as the number of features grows. In a large dimensional space, data becomes sparse, distances increase, and learning reliable patterns requires exponentially more observations.
For two independent points uniformly distributed inside a dimensional unit hypercube, their expected squared Euclidean distance is . Thus, the typical distance between points grows approximately as .
As dimensionality increases, most observations lie close to the boundary of the space and far from one another. A new observation may therefore be far from every training observation, forcing the model to make unreliable extrapolations.
A larger number of features also increases model flexibility, making overfitting more likely. Obtaining sufficiently dense data would require the number of training observations to grow exponentially with the number of dimensions.
Definition (Hypercube).
A dimensional hypercube is the set when each side has unit length. It generalizes a line segment, square, and cube to any dimension.
Definition (Sparsity).
A dataset is sparse in its feature space when observations occupy only a small portion of that space and are typically far apart.
Definition (Tractable problem).
A tractable problem can be solved using a reasonable amount of time and computational resources. An intractable problem requires impractical resources as its size increases.
Projection
Definition (Linear subspace).
A subset is a linear subspace if it contains the zero vector and is closed under vector addition and scalar multiplication.
Definition (Projection).
Let be a linear subspace of an inner product space . The orthogonal projection of onto is the unique vector such that . Equivalently, minimizes the distance over all .

For an affine subspace , the projection is .
Definition (Orthogonal).
Two vectors and are orthogonal when their inner product is zero: .
Definition (Hyperplane).
A hyperplane in is an affine set with dimension , commonly written as for some nonzero vector .
Projection works well when observations lie near a lower dimensional linear or affine subspace. For example, observations near a plane in three dimensional space can be projected onto that plane and represented using two new coordinates, and .
Projection may perform poorly when the underlying structure is curved. Projecting a Swiss roll directly onto a plane can collapse distinct layers and destroy important relationships.
Manifold Learning
Definition (Manifold).
A dimensional topological manifold is a space in which every point has a neighborhood that is homeomorphic to an open subset of . A smooth manifold additionally requires the coordinate transformations between overlapping neighborhoods to be differentiable.
Definition (Embedded manifold).
Let . The set is a dimensional embedded manifold if, for every point , there exists an open neighborhood containing and a smooth invertible coordinate map with smooth inverse such that . Thus, although may be globally curved inside , locally it has the structure of .
Remark (Embedded manifold).
A dimensional manifold embedded in , where , is a subset that may be globally curved but locally resembles a dimensional plane.
Definition (Homeomorphism).
Let and be topological spaces. A function is a homeomorphism if is bijective and continuous and its inverse is also continuous. If such a function exists, and are called homeomorphic, written . A homeomorphism preserves topological properties such as connectedness and continuity while allowing continuous stretching and bending.


Definition (Intrinsic dimension).
The intrinsic dimension is the number of independent coordinates needed to describe the data locally. A Swiss roll embedded in has intrinsic dimension because two coordinates are sufficient to describe positions on its surface.
Definition (Manifold learning).
Let be observations assumed to lie on or near a dimensional manifold , where . Manifold learning seeks a mapping that assigns each observation a lower dimensional representation while approximately preserving relevant geometric or neighborhood structure of . For methods that preserve distances, this may be expressed as , where denotes an appropriate distance measured along the manifold.
Remark (Manifold learning).
Manifold learning estimates the lower dimensional manifold on or near which observations lie and maps the observations to coordinates that preserve its meaningful structure.
The manifold hypothesis states that many real world datasets occupy a much lower dimensional manifold within their observed feature space. For example, valid handwritten digit images represent only a small fraction of all possible pixel combinations.
Unrolling a Swiss roll preserves its surface structure better than projecting it onto a plane. However, dimensionality reduction does not guarantee a simpler learning problem. A decision boundary that is complicated in the original space may become simple on the manifold, but the reverse can also occur.

Principal Component Analysis
Principal component analysis, or PCA, reduces dimensionality by finding a lower dimensional subspace that lies as close as possible to the data, then projecting the data onto that subspace.
Preserving Variance
PCA selects the projection axis that preserves the greatest amount of variance. This retains more information than projecting onto an axis with little variance.
==The axis that maximizes projected variance also minimizes the mean squared distance between the original observations and their projections.==
Principal Components
Definition (Principal component).
The th principal component is the unit vector defining the direction that captures the greatest remaining variance, subject to being orthogonal to all preceding principal components.
The first principal component, , captures the greatest variance. The second component, , captures the greatest remaining variance while satisfying .
For dimensional data, PCA can identify up to mutually orthogonal principal components.
Definition (Unit vector).
A unit vector is a vector whose Euclidean length is . Formally, is a unit vector if .
Definition (Orthogonal vectors).
Two vectors and are orthogonal if their inner product is zero: .
The signs of principal component vectors are not stable. The vectors and point in opposite directions but define the same axis. Small changes to the data may therefore reverse a component’s direction without changing the principal axis.
When two components capture very similar amounts of variance, they may rotate or exchange positions after small changes to the data. However, the subspace they collectively define generally remains stable.
Singular Value Decomposition
Definition (Singular value decomposition).
For any real matrix , a singular value decomposition is a factorization , where and are orthogonal matrices, and is a rectangular diagonal matrix containing nonnegative singular values .
An orthogonal matrix satisfies or . Therefore, its columns form an orthonormal basis.
The columns of are the right singular vectors of . When SVD is applied to a centered dataset, these vectors define the principal component directions. Equivalently, the rows of contain the principal component vectors.
The singular values measure how much variation exists along their corresponding directions. For a centered dataset with observations, the variance captured by component is .
The principal component matrix is .
Computing Principal Components
import numpy as np
X = [...] # create a small 3D dataset
X_centered = X - X.mean(axis=0)
U, s, Vt = np.linalg.svd(X_centered)
c1 = Vt[0]
c2 = Vt[1]
X_centered is created by subtracting each feature’s mean so that the dataset is centered around the origin.
Vt[0] contains the first principal component, while Vt[1] contains the second principal component. The array s contains the singular values in decreasing order.
PCA requires centered data. Scikit Learn’s PCA classes center the data automatically, but manual SVD requires centering before decomposition.
Projecting Down to Dimensions
After identifying the principal components, PCA reduces the dataset to dimensions by projecting it onto the hyperplane formed by the first principal components. These components preserve as much variance as possible.
If contains the first columns of , the reduced dataset is calculated as .
W2 = Vt[:2].T
X2D = X_centered @ W2
Using Scikit Learn
Scikit Learn uses SVD to perform PCA and automatically centers the data before applying the transformation.
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X2D = pca.fit_transform(X)
After fitting, components_ contains one row for each selected principal component. It corresponds to the transpose of .
Explained Variance Ratio
The explained variance ratio measures the proportion of the dataset’s total variance captured by each principal component.
pca.explained_variance_ratio_
array([0.7578477, 0.15186921])
The first principal component captures approximately of the variance, while the second captures approximately . Together, they preserve about of the total variance.
Choosing the Number of Dimensions
A common approach is to select the smallest number of principal components that preserve a chosen amount of variance, such as . For visualization, the data is usually reduced to two or three dimensions instead.
from sklearn.datasets import fetch_openml
mnist = fetch_openml("mnist_784", as_frame=False)
X_train, y_train = mnist.data[:60_000], mnist.target[:60_000]
X_test, y_test = mnist.data[60_000:], mnist.target[60_000:]
pca = PCA()
pca.fit(X_train)
cumsum = np.cumsum(pca.explained_variance_ratio_)
d = np.argmax(cumsum >= 0.95) + 1
For MNIST, the minimum number of components required to preserve of the variance is .
Scikit Learn can determine this number automatically by passing the desired variance ratio to n_components.
pca = PCA(n_components=0.95)
X_reduced = pca.fit_transform(X_train)
The selected number of components is stored in n_components_.
pca.n_components_
Another method is to plot cumulative explained variance against the number of dimensions. The elbow is the point where additional components provide much smaller increases in explained variance.
Choosing Dimensions for a Supervised Model
When PCA is used before a supervised model, the number of components can be treated as a hyperparameter and tuned together with the model.
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV
from sklearn.pipeline import make_pipeline
clf = make_pipeline(
PCA(random_state=42),
RandomForestClassifier(random_state=42)
)
param_distrib = {
"pca__n_components": np.arange(10, 80),
"randomforestclassifier__n_estimators": np.arange(50, 500)
}
rnd_search = RandomizedSearchCV(
clf,
param_distrib,
n_iter=10,
cv=3,
random_state=42
)
rnd_search.fit(X_train[:1000], y_train[:1000])
{ "randomforestclassifier__n_estimators": 465, "pca__n_components": 23 }
The optimal number of components depends on the predictive model. In this example, the random forest performs best with only components. A linear model may require more components because it cannot learn patterns as flexibly.
PCA for Compression
PCA reduces storage by representing data with fewer principal component features. For MNIST, preserving of the variance reduces each observation from features to features, using less than of the original size.
The reduced data can be approximately reconstructed using the inverse transformation. For centered data, . Scikit Learn also restores the feature means automatically.
X_recovered = pca.inverse_transform(X_reduced)
The reconstruction is not exact because some variance was discarded. The mean squared distance between the original and reconstructed data is called the reconstruction error.
Randomized PCA
Randomized PCA approximates the first principal components using a stochastic algorithm. Its complexity is , making it much faster than full SVD when is much smaller than .
rnd_pca = PCA(
n_components=154,
svd_solver="randomized",
random_state=42
)
X_reduced = rnd_pca.fit_transform(X_train)
With svd_solver="auto", Scikit Learn automatically chooses between randomized PCA and full SVD based on the dataset shape and requested number of components.
Incremental PCA
Incremental PCA processes the training set in batches, so the entire dataset does not need to fit in memory. Each batch is passed to partial_fit().
from sklearn.decomposition import IncrementalPCA
n_batches = 100
inc_pca = IncrementalPCA(n_components=154)
for X_batch in np.array_split(X_train, n_batches):
inc_pca.partial_fit(X_batch)
X_reduced = inc_pca.transform(X_train)
A memory mapped array stores a large array in a binary file and loads only the required portions into memory.
filename = "my_mnist.mmap"
X_mmap = np.memmap(
filename,
dtype="float32",
mode="write",
shape=X_train.shape
)
X_mmap[:] = X_train
X_mmap.flush()
The data type and shape must be specified when reopening the file because only raw binary values are stored.
X_mmap = np.memmap(
filename,
dtype="float32",
mode="readonly"
).reshape(-1, 784)
batch_size = X_mmap.shape[0] // n_batches
inc_pca = IncrementalPCA(
n_components=154,
batch_size=batch_size
)
inc_pca.fit(X_mmap)
Random Projection
Random projection reduces dimensionality using a randomly generated linear transformation. It requires no training because the data values are not examined when creating the projection matrix.
Johnson Lindenstrauss Lemma
Definition (Johnson Lindenstrauss lemma).
Let contain points, and let . There exists a mapping , where , such that every pair satisfies . A suitably scaled random linear projection satisfies this condition for all pairs with high probability.
The sufficient target dimension used by Scikit Learn is . It depends on the number of observations and the accepted distortion , but not on the original number of features .
For and , the required dimension is approximately .
from sklearn.random_projection import (
johnson_lindenstrauss_min_dim
)
m, ε = 5_000, 0.1
d = johnson_lindenstrauss_min_dim(m, eps=ε)
#d = 7300
Gaussian Random Projection
A Gaussian projection matrix has shape , with each entry sampled from . Dividing standard normal values by produces the required variance.
n = 20_000
np.random.seed(42)
P = np.random.randn(d, n) / np.sqrt(d)
X = np.random.randn(m, n)
X_reduced = X @ P.T
The transformation changes the dataset from shape to .
Scikit Learn can determine from , generate the matrix, store it in components_, and apply the projection.
from sklearn.random_projection import (
GaussianRandomProjection
)
gaussian_rnd_proj = GaussianRandomProjection(
eps=ε,
random_state=42
)
X_reduced = gaussian_rnd_proj.fit_transform(X)
Sparse Random Projection
SparseRandomProjection uses a sparse matrix instead of a dense Gaussian matrix. It usually requires much less memory and is faster, especially for large or sparse datasets, while providing comparable distance preservation.
Its default density is . Each matrix entry is nonzero with probability , and a nonzero entry is either or , where .
Approximate Inverse Transformation
Random projection does not provide a direct inverse. An approximate reconstruction can be obtained using the pseudoinverse of the projection matrix.
components_pinv = np.linalg.pinv(
gaussian_rnd_proj.components_
)
X_recovered = X_reduced @ components_pinv.T
Computing the pseudoinverse can be expensive for a large projection matrix. Random projection is therefore mainly useful for fast and memory efficient dimensionality reduction rather than accurate reconstruction.
Locally Linear Embedding
Locally linear embedding, or LLE, is a nonlinear dimensionality reduction and manifold learning technique. Unlike PCA and random projection, it does not project data onto learned axes. It preserves local relationships by reconstructing each observation from its nearest neighbors and then reproducing those relationships in a lower dimensional space.
from sklearn.datasets import make_swiss_roll
from sklearn.manifold import LocallyLinearEmbedding
X_swiss, t = make_swiss_roll(
n_samples=1000,
noise=0.2,
random_state=42
)
lle = LocallyLinearEmbedding(
n_components=2,
n_neighbors=10,
random_state=42
)
X_unrolled = lle.fit_transform(X_swiss)

X_swiss has shape , while X_unrolled has shape . The variable t contains each observation’s position along the rolled axis and can be used as a regression target or for coloring the visualization.
LLE preserves distances and relationships locally, but it does not necessarily preserve large scale distances or the overall global shape.
Step 1: Constrained Optimization
For every observation , LLE identifies its nearest neighbors and finds weights that reconstruct as accurately as possible.
Here, is the set of the nearest neighbors of . The first constraint ensures that only neighbors contribute. The second normalizes the weights so they sum to .
The resulting matrix encodes the local linear relationships among the original observations.
Step 2: Unconstrained Optimization
LLE keeps the learned weights fixed and finds lower dimensional coordinates that preserve the same reconstruction relationships.
The matrix contains all lower dimensional observations. Row of is the new representation corresponding to .
Although the book calls this unconstrained optimization, a completely unconstrained problem permits the trivial solution . Practical LLE removes this ambiguity using centering and scale conditions such as and .
LLE obtains by forming and using the eigenvectors associated with its smallest nonzero eigenvalues.
Computational Complexity
The approximate costs are for finding neighbors, for optimizing the weights, and for constructing the lower dimensional representation. The term causes LLE to scale poorly to very large datasets.
Other Dimensionality Reduction Techniques
Multidimensional Scaling
sklearn.manifold.MDS reduces dimensionality while attempting to preserve pairwise distances. It works better when the original data is not extremely high dimensional.
Isomap
sklearn.manifold.Isomap connects each observation to its nearest neighbors and creates a graph. It preserves geodesic distance, which is the shortest path distance between two graph nodes.
t-SNE
sklearn.manifold.TSNE attempts to keep similar observations close and dissimilar observations apart. It is mainly used to visualize clusters in two or three dimensions.
Linear Discriminant Analysis
sklearn.discriminant_analysis.LinearDiscriminantAnalysis is a supervised classification technique that learns axes that separate classes as much as possible. These axes can also be used for dimensionality reduction before classification.
