Sub topic 1
Why high-dimensional data is hard to see
Prerequisites
This chapter assumes you know basic Python and NumPy, what a feature vector is, and how Euclidean distance works. Later chapters also use scikit-learn's fit/transform interface, basic probability, and gradient descent. This chapter needs none of the last three.
What "high-dimensional" means
A dataset is a table. Each row is a point, and each column is a feature, which is one measured number. The number of columns is the dimension. A point with two features fits on a scatter plot. A point with 64 features does not.
scikit-learn's digits dataset is a small example. Each image is 8x8 pixels, so each digit is a vector of 64 numbers. Text embeddings (hundreds of numbers per document) and gene expression tables (thousands of genes per sample) are larger cases of the same problem.
Screens show two dimensions, and we can imagine three. Everything beyond that has to be projected down, and a projection always discards something.
Why we cannot just look
Three problems make high dimensions hard to inspect.
- Plots need two or three axes. With 64 features there are 2,016 pairs of axes. Checking every pair is impractical, and a pair may miss structure that only appears across many features at once.
- Distances behave differently. As dimension grows, the nearest and farthest points from a given point end up at similar distances. This is called distance concentration. "Near" and "far" lose contrast.
- Volume grows fast. To keep the same density of points, the number of points you need grows exponentially with dimension. Real datasets are sparse in their full space.
The code below shows distance concentration on random points.
import numpy as np
rng = np.random.default_rng(0)
for d in [2, 10, 100, 1000]:
X = rng.random((500, d))
dist = np.linalg.norm(X[1:] - X[0], axis=1)
print(f"d={d:4d} nearest/farthest = {dist.min() / dist.max():.2f}")The ratio of the nearest to the farthest distance is small for d=2. It climbs toward 1 as d grows. When every point looks about equally far away, a neighbour is hard to tell from a stranger.
What we want instead
We want a 2-D picture that answers questions such as:
- Are there groups in the data?
- Which points are similar to each other?
- Do the labels I already have match the structure of the features?
No 2-D picture can preserve all the distances of a 64-D dataset. A method has to choose what to keep. PCA keeps the directions of largest variance, which favours global layout. t-SNE and UMAP keep local neighbourhoods, which favours "who is near whom".
Common errors at this stage
- Treating a plot as ground truth. A 2-D embedding is a lossy summary. Reading it as the data itself leads to wrong conclusions, and later chapters return to this.
- Skipping scaling. If one feature has a much larger range than the others, it dominates the distances. Standardise features first when their units differ.
- Assuming more features means more information. Many features can be redundant or noisy, so the data often has fewer effective dimensions than columns.
The last point leads to the manifold hypothesis, which later chapters cover. It says that real data often lies near a lower-dimensional surface inside the high-dimensional space.
Exercise
In the distance-concentration code, what happens to the nearest/farthest ratio as d increases, and why does this hurt methods that rely on "nearest neighbours"?
Show answer
The ratio rises toward 1. Nearest and farthest points become almost equally distant, so the difference between a true neighbour and an unrelated point shrinks. Neighbour-based reasoning then becomes less reliable unless the data has lower-dimensional structure.