Dimensionality, Features, and the Curse
High-dimensional data is normal in text, images, and logs. Geometry changes: distances concentrate, nearest neighbors weaken, and feature design often matters more than the choice among similar models.
Diagram: curse sketch
d=1 points spread on a line
d=2 square
d=3 cube
d large volume concentrates near the "shell"
most mass far from center
1. The curse of dimensionality (geometry)
In high \(d\), several effects appear for “typical” random data:
- Volume concentration: most of a high-\(d\) ball’s volume is near the surface
- Distance concentration: pairwise distances between random points become relatively similar
- Sparsity: fixed \(n\) samples cover \([0,1]^d\) vanishingly; local neighborhoods empty
Consequence: k-NN, distance-based density, and some clustering methods degrade unless structure (manifold, sparsity, metric learning) is exploited.
Worked example 1 — intuition
In high \(d\), ratio of max to min distance among random pairs often approaches \(1\) — “nearest” and “farthest” neighbors hard to distinguish.
When the curse is milder
- Data lie near a low-dimensional manifold
- Features are sparse (few nonzeros per row)
- Strong signal in few coordinates
- Task uses inner products / learned metrics, not raw Euclidean distance
2. Feature types and encodings
| Type | Examples | Encoding notes |
|---|---|---|
| Numeric | age, latency | scale / normalize; watch outliers |
| Categorical | country | one-hot / target / embeddings |
| Ordinal | rating 1–5 | not always linear spacing |
| Text | tokens | BoW, TF-IDF, embeddings |
| Time | timestamps | cyclic (sin/cos), lags, calendars |
| IDs | user_id | embeddings; rare IDs → unk |
| Multi-hot | tags | sparse binary vector |
Worked example 2 — one-hot countries
\(200\) countries ⇒ \(d\approx 200\) binary features (or \(199\) with a reference level in regression). High cardinality (millions of IDs) makes one-hot impractical — prefer embeddings or hashing.
3. Scaling and leakage
Standardize: \((x-\mu)/\sigma\) per feature.
Min-max: map to \([0,1]\).
Robust: median / IQR for heavy tails.
| Models sensitive to scale | Often less sensitive |
|---|---|
| Linear / logistic / SVM / k-NN / k-means / neural nets / PCA | Tree ensembles (axis-aligned splits) |
Leakage rule: fit scalers, imputers, PCA, target encoders on train only, then apply to val/test. Using full-data \(\mu,\sigma\) leaks future information into training transforms.
Worked example 3 — PCA scaling
PCA maximizes variance. A feature measured in milliseconds (large numbers) dominates one in kilometers if unscaled — not because it is more informative.
4. Dimensionality reduction map
raw features R^d
│
├── PCA / SVD (linear subspace)
├── random projections (JL lemma)
├── feature selection (filter / wrapper)
└── learned embeddings (nonlinear)
│
v
R^k k ≪ d for viz / speed / denoise
PCA: directions of maximum variance (see advanced LA / SVD chapters).
Johnson–Lindenstrauss: random projections can preserve pairwise distances approximately with \(k=\mathcal{O}(\varepsilon^{-2}\log n)\).
Feature selection: drop coordinates; MI / regularization / tree importances (careful with interpretation).
5. Feature crosses and interactions
A linear model on \([x_1,x_2]\) cannot represent an AND-like interaction \(x_1 x_2\) without an explicit product feature or a nonlinear model (trees, nets, kernels).
Worked example 4
Spam: “free” and “money” each mild; co-occurrence strong. Cross feature \(\mathbf{1}_{\mathrm{free}}\cdot\mathbf{1}_{\mathrm{money}}\) or use a model that builds interactions.
6. Sparsity
One-hot, n-grams, multi-hot tags ⇒ sparse high-\(d\) vectors.
- Algorithms: sparse matrix formats, sparse-aware linear models
- Regularization: \(L_1\) selects features; elastic net for correlated groups
- Hashing trick: map tokens to \(2^b\) bins with collisions — controlled approximation
7. Metrics: when Euclidean fails
| Similarity | Use when |
|---|---|
| Euclidean | dense, scaled numeric |
| Cosine | text BoW / TF-IDF (direction > length) |
| Manhattan | some sparse / robust settings |
| Learned (Mahalanobis, metric learning) | task-specific distances |
Worked example 5 — cosine for documents
Long documents have larger BoW counts; cosine normalizes length so similarity is about relative word composition.
8. The “average distance to k-NN” trap
In high \(d\), novelty scores based on distance to neighbors can become uninformative as distances concentrate. Prefer:
- Domain features
- Dimensionality reduction first
- One-class models with appropriate kernels
- Reconstruction error of autoencoders / PCA (with caveats)
9. Embeddings vs one-hot
| One-hot | Embedding |
|---|---|
| No notion of similarity between levels | Learned similarity |
| \(d=\#\) levels | \(d=\) embedding dim (small) |
| Interpretable coefficients | Dense geometry |
| Great for low cardinality | High-cardinality IDs, words, items |
Worked example 6
User IDs: millions of levels. Embedding dim \(32\)–\(128\) common in recommenders; cold-start needs defaults / content features.
10. Pipeline checklist
- Define label and unit of analysis
- Split train/val/test before heavy featurization that needs fit
- Encode categoricals; handle unknowns
- Scale if model requires
- Optional: select / reduce dimensions on train
- Fit model; evaluate on held-out with same transforms
- Inspect errors; iterate features
11. Pitfalls
- Scaling with full-data statistics
- Target encoding without CV (leakage)
- Huge one-hots into dense neural nets without embeddings
- Interpreting PCA components as causal factors
- k-NN in raw high-\(d\) Euclidean space
- Dropping rare categories inconsistently between train and serve
12. Checkpoint
- Explain distance concentration
- Choose encodings for numeric / categorical / text
- Scale with train-only fit
- Sketch PCA vs embeddings vs selection
- Know when cosine beats Euclidean
- State leakage risks in feature pipelines
Exercises
Easy
- Why is cosine similarity common for text bags-of-words?
- If all features are one-hot countries (200 levels), what is \(d\) roughly?
- Give one reason to standardize before PCA.
- Explain train-only fitting of scalers (no leakage).
- Sketch when embeddings beat one-hot for high-cardinality IDs.
Medium
- Thought: In high \(d\), why might “average distance to 10-NN” stop being a good novelty score?
- Feature cross: write a linear model that includes \(x_1,x_2,x_1 x_2\).
- Hashing trick: what happens when two tokens collide?
- Compare \(L_1\) vs \(L_2\) regularization for sparse one-hot features.
- Time feature: encode hour of day with sin/cos; why not integer \(0..23\) alone for linear models?
Challenge
- JL lemma idea: why random projections preserve distances with high probability (statement-level).
- Show that for i.i.d. standard Gaussian coordinates, \(\|x\|_2/\sqrt{d}\to 1\) in probability — concentration sketch.
- Design a leakage-safe target encoding with \(K\)-fold scheme.
- Manifold assumption: how does it reconcile high ambient \(d\) with successful k-NN on images?
- End-to-end: propose features for churn prediction; mark which need fit-on-train only.
Checks
- Direction matters more than length; normalizes document length.
- \(\approx 200\) (or \(199\) with reference level).
- PCA is not scale-invariant; large-scale features dominate variance.
Summary
High dimension changes geometry and stress-tests naive distance methods. Strong pipelines encode features carefully, scale without leakage, reduce or embed when needed, and pick metrics that match the data type. Feature design remains a primary lever in data science math — models only see the representation you build.