Skip to main content

Lesson 4 of 9

Non-Linear Manifold Learning: t-SNE & UMAP for High-Dimensional Visualization

Linear dimensionality reduction (PCA) cannot unroll non-linear manifolds (e.g. swiss rolls, high-dimensional word embeddings, image clusters). Non-linear manifold learning techniques preserve local geometric neighborhoods. t-SNE (t-Distributed Stochastic Neighbor Embedding) minimizes the Kullback-Leibler (KL) divergence between high-dimensional Gaussian affinities and low-dimensional Student-t affinities. UMAP (Uniform Manifold Approximation and Projection) uses Riemannian geometry to preserve both local and global structure with 10x faster execution.

25 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Benchmarking PCA vs t-SNE for High-Dimensional Cluster Separation

Benchmarking PCA vs t-SNE for High-Dimensional Cluster Separation
import numpy as np
import pandas as pd
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn.preprocessing import StandardScaler

# Ingest handwritten digits dataset (64 pixel dimensions, 10 classes)
digits = load_digits()
X_raw, y = digits.data[:1000], digits.target[:1000]
X = StandardScaler().fit_transform(X_raw)

# 1. LINEAR PCA REDUCTION (2 Components)
pca_2d = PCA(n_components=2, random_state=42)
X_pca = pca_2d.fit_transform(X)

# 2. NON-LINEAR t-SNE EMBEDDING (2 Components)
# Perplexity controls neighborhood balance (typical: 30 to 50)
tsne = TSNE(
    n_components=2,
    perplexity=30,
    learning_rate='auto',
    init='pca',
    random_state=42
)
X_tsne = tsne.fit_transform(X)

print("=== MANIFOLD REDUCTION COMPARISON ===")
print(f"Original Dimension: {X.shape[1]} pixels")
print(f"PCA 2D Projections Shape   : {X_pca.shape} (Variance preserved: {np.sum(pca_2d.explained_variance_ratio_)*100:.1f}%)")
print(f"t-SNE 2D Embeddings Shape  : {X_tsne.shape} (Preserves non-linear local neighborhood topologies!)")
print(f"t-SNE KL Divergence Score  : {tsne.kl_divergence_:.4f}")

Pro Tip: When to Use UMAP Over t-SNE

In production pipelines, use UMAP (`umap-learn`) instead of t-SNE. UMAP is mathematically grounded in Riemannian geometry, preserves global cluster layout far better than t-SNE, executes 10-20x faster on large datasets, and allows projecting new unseen test samples using `umap.transform(X_test)` (which t-SNE cannot do).

Diagram
Loading diagram…

MlTsneUmap Visual Representation

Hands-On Challenge: Tune the Perplexity Parameter in t-SNE

Run t-SNE across perplexity values of 5, 30, and 100 on the digits dataset. Observe how small perplexity creates fragmented micro-clusters, while large perplexity smooths local details into a global cloud.

Quick Quiz & Key Concept Checks

Common Pitfall: Interpreting Cluster Distances and Sizes in t-SNE

The distance between clusters in a 2D t-SNE plot is largely arbitrary and depends on perplexity and random seed. Never claim that 'Cluster A is twice as similar to Cluster B as Cluster C' based on t-SNE distance. Use PCA or UMAP for global distance comparisons.

Topics in This Module

0/9 done