Sub topic 4
The manifold hypothesis and local neighbourhoods
This chapter assumes you know basic Python and NumPy, scikit-learn's fit/transform interface, Euclidean distance, and the idea of a feature vector. It explains why neighbour-embedding methods focus on local structure.
The idea
The manifold hypothesis says that real high-dimensional data does not fill its space. The points lie close to a lower-dimensional surface, called a manifold, that is curved and embedded in the larger space.
The digits data shows this. Each image has 64 pixel values, but the images vary in only a few ways: stroke thickness, slant, and which digit it is. Random 64-pixel vectors look like noise. Real digits occupy a tiny, structured part of the space.
The number of directions you can move along the surface is the intrinsic dimension. It is usually far smaller than the number of features.
Why curvature matters
A manifold can be curved, so straight-line distance can mislead. Think of a rolled-up sheet of paper. Two points on neighbouring layers are close in a straight line through the air. Along the paper, they are far apart.
- Over short distances, the surface looks flat. Euclidean distance between nearby points is a good estimate of distance along the surface.
- Over long distances, Euclidean distance can cut across gaps and folds. It no longer tells you how far apart the points are along the surface.
PCA preserves large-scale linear variance, so it cannot unroll a curved surface. This is why PCA is a baseline and not the final answer.
Local neighbourhoods
If only short distances are reliable, a sensible strategy is to keep only local information. For each point, find its nearest neighbours and ask the embedding to keep those neighbours close. Pairs of faraway points matter much less.
The next chapters build on this. t-SNE and UMAP both start by describing each point's neighbourhood, then search for a 2-D layout that reproduces it.
A quick check in code
You can see locality in the digits data. For each image, check how often its nearest neighbour has the same label.
from sklearn.datasets import load_digits
from sklearn.neighbors import NearestNeighbors
X, y = load_digits(return_X_y=True)
nn = NearestNeighbors(n_neighbors=2).fit(X)
_, idx = nn.kneighbors(X)
# idx[:, 0] is the point itself, idx[:, 1] is its nearest other point
same = (y[idx[:, 1]] == y).mean()
print(f"Nearest neighbour shares the label: {same:.1%}")The result is typically above 98%. Local neighbourhoods carry strong class information, even in 64 dimensions.
Exercise
Why is Euclidean distance reliable for nearby points on a curved manifold but not for distant ones, and how does this motivate neighbour embedding?
Show answer
Over short distances a curved surface looks almost flat, so straight-line distance closely matches distance along the surface. Over long distances the straight line can cross gaps or folds, so it misstates how far apart the points are along the surface. Neighbour embedding therefore keeps only local relationships, each point's nearest neighbours, because those are the distances that can be trusted.