Sub topic 2
Dimensionality reduction and what it costs
This chapter assumes you know basic Python and NumPy, the scikit-learn fit/transform interface, Euclidean distance, and the idea of a feature vector. It covers what dimensionality reduction does and what it gives up. Later chapters build on this.
What dimensionality reduction does
Dimensionality reduction maps each point from a space with many features to a space with few, usually two for plotting. The 8x8 digit images have 64 features each. A reduction to two dimensions gives every image an (x, y) position that you can plot.
Methods fall into two groups:
- Linear methods compute each new coordinate as a weighted sum of the original features. PCA is the main example. The map is a matrix multiplication, so it is fast and easy to apply to new data.
- Nonlinear methods build the map from the data's neighbourhood structure. t-SNE and UMAP belong here. They can unfold curved structure that a linear map flattens.
What it costs
Going from 64 dimensions to 2 cannot keep everything. Some information is lost. Which information you lose depends on the method.
- Distances are distorted. Many points that are far apart in 64 dimensions can land near each other in 2. Some pairs that were close get pulled apart.
- Global structure suffers. Nonlinear methods keep local neighbourhoods and often sacrifice the distances between groups.
- The map is not invertible. You cannot recover the original 64 values from the two coordinates.
- Results depend on settings. Parameters and random seeds change the layout.
Measuring what was lost
A simple check is to ask whether each point's nearest neighbours in the original space are still its neighbours in the embedding. scikit-learn's trustworthiness gives a score from 0 to 1. Higher means better neighbour preservation. The code below compares PCA and t-SNE.
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE, trustworthiness
X, y = load_digits(return_X_y=True)
Z_pca = PCA(n_components=2, random_state=0).fit_transform(X)
Z_tsne = TSNE(init="pca", learning_rate="auto",
random_state=0).fit_transform(X)
for name, Z in [("PCA", Z_pca), ("t-SNE", Z_tsne)]:
score = trustworthiness(X, Z, n_neighbors=10)
print(f"{name}: trustworthiness = {score:.3f}")Expect t-SNE to score clearly higher than PCA on this data. The exact numbers vary with library versions. This measures local neighbour preservation only. It says nothing about whether the distances between clusters are meaningful.
Choosing a method
| Goal | Better fit |
|---|---|
| A fast, repeatable baseline | PCA |
| Preserving global variance | PCA |
| Seeing local clusters | t-SNE or UMAP |
| Projecting new points into an existing map | PCA or UMAP |
The next chapter computes the PCA baseline in detail.
Exercise
A colleague says that two clusters sit far apart on a t-SNE plot, so the original data must also have them far apart. Why is this not safe to conclude, and what does the trustworthiness score tell you about it?
Show answer
Nonlinear methods keep local neighbourhoods and often distort the distances between groups, so the gap on the plot may not reflect the gap in the original space. Trustworthiness only measures whether each point's nearest neighbours are preserved. A high score supports reading local clusters but says nothing about the distances between clusters.