UMAP for dimensionality reduction
2.5.12· AI-ML › Unsupervised Learning
Socho jaise ek crumple hua kaagaz ka map banana: tum chahte ho ki neighborhoods saath rahen, lekin overall layout bhi samajh mein aaye.
UMAP Kyun? Motivation
PCA ki Problem: Linear projection manifold structure kho deti hai (sochiye: Swiss roll ka projection).
t-SNE ki Problem:
- Sirf local structure optimize karta hai → global distances meaningless ho jaati hain
- Computationally expensive:
- Runs ke beech non-deterministic layout
- Density information preserve nahi karta
UMAP ka Solution:
- High-D data ko fuzy topological structure (Riemannian manifold) ki tarah model karo
- Low-D mein similar structure construct karo
- Woh layout dhundo jo in dono structures ko jitna ho sake utna similar banaye
- Use clever approximations to make it fast: instead of
Key Hyperparameters:
- n_neighbors (): Local vs global structure balance control karta hai (typical: 5-50)
- min_dist: Low-D embedding mein minimum separation (typical: 0.0-0.99)
- metric: High-D space mein distance function (Euclidean, cosine, etc.)
Output: Low-dimensional embedding (typically ya )
First Principles se Derivation
Step 1: High-Dimensional Graph Banao
Har point ke liye, uske nearest neighbors dhundo. Hum points ke beech "connectedness" ko fuzy sets ki tarah model karte hain.
Distance-to-Similarity Transform:
Point aur uske neighbor ke liye, compute karo:
Yeh formula kyun?
- : Points aur ke beech distance
- : ke nearest neighbor tak ki distance (isse har point kam se kam ek neighbor ko weight ~1 ke saath "dekhta" hai)
- : Local bandwidth jo is tarah choose ki jaati hai ki (consistent local density modeling ensure karta hai)
- Exponential decay ek smooth transition banata hai: paas ke neighbors ko high weights milte hain, door wale fade ho jaate hain
kyun? Yeh "local connectivity" constraint hai. Har point ko apne immediate nearest neighbor se fully connected (weight = 1) dikhna chahiye taaki isolated points na ho. Hum subtract karte hain taaki decay pehle neighbor ke baad shuru ho.
Symmetrization (fuzy set union):
Isse ek undirected graph banta hai jahan connection strength fuzy topological membership reflect karti hai.
Step 2: Low-Dimensional Layout Define Karo
Low-D space mein hum similar fuzy connectivity chahte hain. Ek alag kernel use karo:
Yeh t-distribution jaisa kernel kyun?
- Tight clusters (chhoti distances) aur separated groups (badi distances) dono allow karta hai
- Parameters (typically fitting se) spread control karte hain
- Heavy tails "crowding problem" ko rokti hain jahan bahut saare points chhoti jagah mein compress ho jaate hain
Step 3: Cross-Entropy Loss Optimize Karo
Hum chahte hain ki low-D fuzy structure high-D wale se match kare. Cross-entropy se disagreement measure karo:
Isko breakdown karte hain:
- Pehla term: Agar high-D kehta hai "connected" ( high), toh penalize karo agar low-D disagree kare ( low)
- Doosra term: Agar high-D kehta hai "disconnected" ( low), toh penalize karo agar low-D unhe paas rakhe ( high)
Cross-entropy kyun? Yeh probability distributions ke beech difference ka natural measure hai (fuzy sets ko probabilities ki tarah model kiya jaata hai).
Optimization: Stochastic gradient descent use karo:
- Negative sampling (repulsion enforce karne ke liye unconnected pairs sample karo)
- Initial layout spectral embedding se (speed ke liye)
- Typical: 500 epochs, learning rate 1.0 se shuru hoti hai
High-D Edge Weight:
Low-D Edge Weight:
Optimization Objective:
Scenario: 1000 MNIST digits ki embeddings
n_neighbors=5 ke saath:
- Har point sirf 5 nearest neighbors dekhta hai
- High-D graph bahut sparse hai
- Low-D embedding local structure emphasize karta hai: tight, well-separated digit clusters
- Bade manifolds ko disconnected pieces mein fragment kar sakta hai
n_neighbors=50 ke saath:
- Har point 50 neighbors dekhta hai
- High-D graph denser hai, broader context capture karta hai
- Low-D embedding global structure preserve karta hai: digit groups ke beech relationships visible hain
- Clusters boundaries pe zyada blend ho sakte hain
Yeh behavior kyun? parameter adjust hota hai total weight fit karne ke liye. Chhote ke saath, sirf immediate neighbors matter karte hain → local focus. Bade ke saath, door ke connections ko nonzero weight milta hai → global awareness.
Rule of thumb:
- Chhota data ya fine clusters dhundne ke liye:
- Bada data ya global structure preserve karne ke liye:
Setup: Single-cell RNA-seq se 500 cells, 2000 genes
min_dist=0.0 ke saath:
- Points ko tightly pack hone diya jaata hai
- Calculation: Low-D kernel 1 ke paas aa sakti hai jab bhi
- Result: Dense clusters, clear separation, local density information preserve hoti hai
- Use case: Tight cell type clusters identify karna
min_dist=0.8 ke saath:
- Points ko spacing maintain karni hogi
- Calculation: Optimization ko penalize karta hai, repulsion force karta hai
- Result: Zyada "inflated" layout, individual points dekhna aasan, kuch density info kho jaati hai
- Use case: Visualization jahan point overlap structure chhupa de
Yeh step kyun? min_dist directly gradient mein attractive force modify karta hai. Zyada values chhoti range par repulsion add karti hain.
Diya gaya: Point jiske neighbors hain distances par (sorted). Nearest neighbor distance .
Goal: dhundo aise ki:
kyun? Yeh information theory se aata hai: perplexity (effective number of neighbors) se match karni chahiye. t-SNE terms mein, . UMAP numerical stability ke liye entropy = target karta hai.
Binary Search se Solution:
- Pehla neighbor: , toh
- Baaki neighbors: for
- Target:
ke liye: target
Agar bahut chhota → weights bahut fast decay karte hain → sum < target → badhao Agar bahut bada → weights bahut slowly decay karte hain → sum > target → ghatao
~20 iterations mein binary search se converge karo.
Example numbers (images par Manhattan distance):
- , pixels
- Neighbor distances: 45.2, 52.1, 58.3, 67.8, 73.2, ...
- Solved pixels
- Weights: 1.00, 0.71, 0.49, 0.28, 0.17, ... (sum≈ 3.906 ✓)
Galat Idea: UMAP aur t-SNE ek hi kaam karte hain, UMAP sirf better optimize hai.
Kyun Sahi Lagta Hai: Dono clear clusters ke saath 2D scatter plots produce karte hain, dono neighbor graphs use karte hain, dono nonlinear hain.
Sachchi Baat:
-
Mathematical foundation alag hai:
- t-SNE: Probability distributions ke beech KL-divergence
- UMAP: Fuzzy topological structures par Cross-entropy (category theory basis)
-
Global structure:
- t-SNE explicitly global distances discard karta hai (sirf local neighborhoods preserve karta hai)
- UMAP calibration aur negative sampling strategy se local + global balance karta hai
-
Inverse transform:
- t-SNE: Retraining ke bina naye points embed nahi kar sakta
- UMAP: Parametric version se inverse mapping (new point → low-D) seekh sakta hai
-
Hyperparameter meaning:
- t-SNE perplexity: "consider karne ke liye effective number of neighbors"
- UMAP n_neighbors: "local vs global balance" (graph connectivity fundamentally affect karta hai)
Fix: UMAP ko "improved clustering visualization" nahi, "manifold learning with topological constraints" socho.
Galat Idea: "Mere dataset mein 100,000 points hain, main n_neighbors=5 use karunga computation fast rakhne ke liye."
Kyun Sahi Lagta Hai: Chhota → sparser graph → faster computation.
Problem:
- 100k points aur ke saath, aapke paas 500k edges hain
- Lekin data mein natural clusters size ~10,000 ke ho sakte hain
- Har point sirf 5 neighbors "dekhta" hai → sirf local structure capture hota hai
- Result: Bade coherent structures bahut saare disconnected pieces mein fragment ho jaate hain
- "Continents" ki jagah aapko "archipelagos" milte hain
Fix:
- points ke liye, global structure ke liye minimum use karo
- 100k points ke liye: use karo
- UMAP ki approximations ise fast rakhti hain (random projection forests se nearest neighbor search)
Verification: Graph connectivity check karo:
from umap import UMAP
from scipy.sparsecsgraph import connected_components
umap_model = UMAP(n_neighbors=5)
umap_model.fit(X)
graph = umap_model.graph_
n_components, labels = connected_components(graph)
print(f"Disconnected components: {n_components}") # Should be ~1, not ~1000Recall UMAP ko ek 12-saal ke bachche ko explain karo
Socho tumhare paas ek huge crumpled ball of yarn hai jisme 1000 knots hain (tumhare data points). Yarn mein kuch structure hai — kuch knots paas paas hain aur bunches banate hain, kuch door hain. Tum is poori tangled mess ko ek table par flat rakhna chahte ho taaki pattern dikh sake, lekin tumhe do rules follow karne hain:
Rule 1: Knots jo ball mein neighbors the woh table par bhi neighbors rahen. Agar do knots touch kar rahe the, unhe table ke opposite sides par mat rakhna!
Rule 2: Agar knots ke do bunches ball mein door the, toh table par bhi unhe door rakhna. Sab kuch ek blob mein mat smosh karo.
UMAP ka kaam hai yarn ko dono rules follow karte hue sulajaana. Yaise:
-
Friendship network banao: Har knot apne 15 closest neighbors dekhta hai aur kehta hai "yeh mere dost hain" alag alag friendship levels ke saath (super close friends ko score 1.0 milta hai, acquaintances ko score 0.2).
-
Flat map banao: Shuru mein sare knots table par randomly rakho. Ab unhe move karo: friends ko paas kheencho, non-friends ko door dhakelo.
-
Adjust karte raho: Yeh 500 baar karo jab tak table par friendship network original tangled ball ki friendship network se match na kare.
Jaadu? UMAP sirf immediate neighbors ki parwaah nahi karta (jaise t-SNE karta hai). Yeh tumhare 15 closest friends dekhta hai, aur unke through, pure neighborhood structure ke baare mein seekhta hai. Isliye final flat map dono tight friend groups AUR kaise woh groups table par ek doosre se relate karte hain, dono dikhata hai.
- Unfold: Curved high-D manifold lo aur use flatten karo
- Manifolds: Data curved surfaces par hai, sirf Euclidean space mein nahi
- And: Local AND global structure dono (na ki "or")
- Preserve: Topological relationships intact rakho
Alternative: "Use My Awesome Projection" (jab tumhara t-SNE bahut slow ho)
Hyperparameter Tuning Guide
| Parameter | Embedding par Effect | Typical Range | Zyada Kab Chunna | Kam Kab Chunna |
|---|---|---|---|---|
| n_neighbors | Local↔ Global balance | 5-100 | Bada dataset, global structure chahiye | Chhota dataset, tight clusters chahiye |
| min_dist | Cluster tightness | 0.0-0.99 | Separated points chahiye, better viz | Accurate local density chahiye |
| metric | Distance definition | euclidean, cosine, etc. | Data type demand kare (text→cosine) | Euclidean kaam kare (images, tabular) |
| n_components | Output dimensions | 2-100 | Downstream ML task | Sirf visualization |
Doosre Concepts se Connections
- t-SNE: Visualization ke liye UMAP ka predecessor, sirf local structure pe focus karta hai
- PCA: Linear dimensionality reduction, fast hai lekin manifolds miss karta hai
- Autoencoders: Inverse mapping ke saath dimensionality reduction ka neural alternative
- Isomap: Geodesic distances ke zariye earlier manifold learning method
- Spectral Clustering: Initialization ke liye similar graph Laplacian concepts use karta hai
- Nearest Neighbors Algorithms: UMAP fast KNN search par heavily rely karta hai
- Cross-Entropy Loss: Distributions match karne ke liye core optimization objective
- Random Projection: Approximate nearest neighbor search ke liye internally use hota hai
- Topological Data Analysis: Fuzy simplicial sets ke liye mathematical foundation provide karta hai
UMAP vs Alternatives Kab Use Karein
UMAP use karo jab:
- Dataset size: 1k-10M points (acchi tarah scale hota hai)
- Local clusters AUR global structure dono chahiye
- Reproducible results chahiye (random_state set karo)
- Baad mein naye points embed karne hain (parametric UMAP)
- Computational constraints hain (t-SNE se faster)
t-SNE use karo jab:
- Dataset size: <10k points
- Visualization ke liye sirf local cluster separation chahiye
- UMAP try kar chuke ho aur clusters zyada blend ho rahe hain (agar aisa ho toh n_neighbors badhao)
PCA use karo jab:
- Linear interpretability chahiye (loadings)
- Data actually linear hai (rare!)
- Deterministic results chahiye
- Millions of points ke liye fast transforms chahiye
Autoencoders use karo jab:
- GPU resources hain
- Differentiable inverse mapping chahiye
- Doosre neural network losses ke saath combine karna hai
#flashcards/ai-ml
What does UMAP stand for? :: Uniform Manifold Approximation and Projection