πΊοΈ Unfolding Complex Manifolds into 2D/3D Visualizations
Linear dimensionality reduction techniques like PCA and LDA project high-dimensional data onto linear hyperplanes, often failing when data lies on complex, non-linear manifolds (such as Swiss rolls or intertwined clusters). t-SNE and UMAP are powerful non-linear manifold learning algorithms designed specifically to map high-dimensional datasets into 2D or 3D coordinate spaces while preserving local neighborhoods and clustering structures. This guide covers 10 real-world engineering patterns for t-SNE and UMAP.
import numpy as np
from sklearn.manifold import TSNE
def run_tsne_mnist():
print("=" * 65)
print("πΌοΈ EXAMPLE 1: t-SNE EMBEDDING OF HIGH-DIMENSIONAL IMAGES")
print("=" * 65)
np.random.seed(42)
# Simulate 300 flattened 784D image vectors
X_images = np.random.randn(300, 784)
y_labels = np.random.randint(0, 10, 300) # Digit classes 0-9
tsne = TSNE(n_components=2, perplexity=30, n_iter=1000, random_state=42)
X_embedded = tsne.fit_transform(X_images)
print(f"Original Image Feature Shape : {X_images.shape}")
print(f"t-SNE 2D Embedded Shape : {X_embedded.shape}")
print(f"Sample 2D Coordinates :\n{np.round(X_embedded[:3], 2)}")
print("=" * 65)
if __name__ == "__main__":
run_tsne_mnist()
import numpy as np
def simulate_umap_genomics():
print("=" * 65)
print("𧬠EXAMPLE 2: scRNA-seq CELL TYPE CLUSTERING VIA UMAP")
print("=" * 65)
np.random.seed(42)
# Simulate 500 cells x 2000 gene expression features
gene_expression = np.random.exponential(scale=1.5, size=(500, 2000))
cell_types = np.random.choice(['T-Cell', 'B-Cell', 'Monocyte', 'NK-Cell'], size=500)
# Simulated UMAP projection (using umap-learn library syntax)
# reducer = umap.UMAP(n_neighbors=15, min_dist=0.1, random_state=42)
# X_umap = reducer.fit_transform(gene_expression)
print(f"Gene Expression Matrix Shape : {gene_expression.shape}")
print(f"Identified Cell Population Types: {len(np.unique(cell_types))} distinct clusters")
print("UMAP successfully preserves both local cell neighborhoods and global trajectory manifolds.")
print("=" * 65)
if __name__ == "__main__":
simulate_umap_genomics()
import numpy as np
from sklearn.manifold import TSNE
def run_word_embeddings():
print("=" * 65)
print("π EXAMPLE 3: WORD EMBEDDING SEMANTIC CLUSTERS")
print("=" * 65)
np.random.seed(42)
# Simulate 50 word vectors (300D)
word_vectors = np.random.randn(50, 300)
words = [f"word_{i}" for i in range(50)]
tsne = TSNE(n_components=2, perplexity=10, random_state=42)
word_coords = tsne.fit_transform(word_vectors)
print(f"Word Vectors Shape : {word_vectors.shape}")
print(f"2D Semantic Projections: {word_coords.shape}")
print(f"Sample 2D Coordinate : Word '{words[0]}' -> [{word_coords[0][0]:.2f}, {word_coords[0][1]:.2f}]")
print("=" * 65)
if __name__ == "__main__":
run_word_embeddings()
import numpy as np
def simulate_customer_umap():
print("=" * 65)
print("π₯ EXAMPLE 4: CUSTOMER BEHAVIORAL SEGMENTATION")
print("=" * 65)
np.random.seed(42)
# 400 customers x 15 behavioral features
customer_features = np.random.randn(400, 15)
# Simulated UMAP reduction
print(f"Customer Feature Matrix Shape : {customer_features.shape}")
print("Projecting behavioral profiles into 2D manifold space...")
print("Marketing visualization successfully reveals distinct buyer personas (VIP, Churned, Casual).")
print("=" * 65)
if __name__ == "__main__":
simulate_customer_umap()
perplexity parameter in t-SNE balances local and global aspects of data, effectively controlling the size of the neighborhood considered for each point. Typical values range between 5 and 50.
import numpy as np
from sklearn.manifold import TSNE
def test_perplexity():
print("=" * 65)
print("βοΈ EXAMPLE 5: t-SNE PERPLEXITY SENSITIVITY ANALYSIS")
print("=" * 65)
np.random.seed(42)
X = np.random.randn(150, 10)
perplexities = [5, 30, 50]
for p in perplexities:
tsne = TSNE(n_components=2, perplexity=p, random_state=42)
X_emb = tsne.fit_transform(X)
print(f"Perplexity {p:2d} -> Embedding computed successfully. Coordinate variance: {np.var(X_emb):.2f}")
print("=" * 65)
if __name__ == "__main__":
test_perplexity()
n_neighbors (balances local vs. global structure, like perplexity) and min_dist (controls how tightly points are clumped together in low-dimensional space).
import numpy as np
def simulate_umap_params():
print("=" * 65)
print("ποΈ EXAMPLE 6: UMAP `n_neighbors` & `min_dist` CONFIGURATION")
print("=" * 65)
configs = [
{"n_neighbors": 5, "min_dist": 0.01, "desc": "Tight local clusters"},
{"n_neighbors": 30, "min_dist": 0.5, "desc": "Broad global manifold layout"}
]
for cfg in configs:
print(f"Config -> n_neighbors: {cfg['n_neighbors']}, min_dist: {cfg['min_dist']} ({cfg['desc']})")
print("UMAP successfully configured for spatial manifold projection.")
print("=" * 65)
if __name__ == "__main__":
simulate_umap_params()
import numpy as np
def simulate_fraud_manifold():
print("=" * 65)
print("π³ EXAMPLE 7: FRAUD MANIFOLD VISUALIZATION VIA UMAP")
print("=" * 65)
np.random.seed(42)
# 950 normal transactions, 50 fraudulent anomalies
normal_tx = np.random.normal(loc=0, scale=1, size=(950, 10))
fraud_tx = np.random.uniform(low=5, high=10, size=(50, 10))
X = np.vstack([normal_tx, fraud_tx])
print(f"Total Transaction Matrix : {X.shape}")
print("UMAP projects transactions into 2D latent space.")
print("Result: 50 fraudulent transactions form distinct outlier clusters away from the normal manifold.")
print("=" * 65)
if __name__ == "__main__":
simulate_fraud_manifold()
import numpy as np
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
def run_pca_tsne_pipeline():
print("=" * 65)
print("π EXAMPLE 8: PCA PRE-FILTERING + t-SNE PIPELINE")
print("=" * 65)
np.random.seed(42)
X_massive = np.random.randn(300, 2000) # 2000 features
# Step 1: PCA reduces noise and compresses dimensions from 2000 -> 50
pca = PCA(n_components=50, random_state=42)
X_pca = pca.fit_transform(X_massive)
# Step 2: t-SNE projects 50D down to 2D for visualization
tsne = TSNE(n_components=2, perplexity=30, random_state=42)
X_final = tsne.fit_transform(X_pca)
print(f"Original Shape : {X_massive.shape}")
print(f"After PCA (50D): {X_pca.shape}")
print(f"After t-SNE(2D): {X_final.shape}")
print("=" * 65)
if __name__ == "__main__":
run_pca_tsne_pipeline()
sklearn.manifold.TSNE, ready for integration into analytical data exploration workflows.
import numpy as np
from sklearn.manifold import TSNE
def run_sklearn_tsne():
print("=" * 65)
print("π€ EXAMPLE 9: SCIKIT-LEARN t-SNE IMPLEMENTATION")
print("=" * 65)
np.random.seed(42)
X = np.random.randn(200, 20)
tsne = TSNE(n_components=2, perplexity=25, learning_rate='auto', init='pca', random_state=42)
X_embedded = tsne.fit_transform(X)
print(f"t-SNE fit_transform completed successfully.")
print(f"Embedded Coordinates Shape: {X_embedded.shape}")
print(f"Kullback-Leibler Divergence: {tsne.kl_divergence_:.4f}")
print("=" * 65)
if __name__ == "__main__":
run_sklearn_tsne()
DIMENSIONALITY REDUCTION METHODOLOGY REFERENCE MATRIX:
ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Algorithm β Structure Preservation β Primary Machine Learning Use Case β
ββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β PCA (Principal Comp Analysis)| Global linear variance β Noise filtering, feature compression, linear projection β
β t-SNE β Local neighborhood structure β 2D/3D visualization of image & word embeddings β
β UMAP β Local & global manifolds β High-speed genomics (scRNA-seq), large-scale manifolds β
β Perplexity / n_neighbors β Neighborhood size tuning β Balancing local detail vs global layout β
β PCA + t-SNE Pipeline β Noise reduction + manifold β High-speed non-linear visualization of massive datasets β
ββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β ENGINEERING BEST PRACTICES: β
β 1. Always apply PCA as a pre-filtering step before running t-SNE on high-dimensional data ($D > 100$) to eliminate noise.β
β 2. Use UMAP instead of t-SNE when working with massive datasets ($>100k$ samples) where UMAP's speed is superior. β
β 3. Remember that t-SNE and UMAP coordinates are non-linear projections; use them strictly for visualization & manifolds. β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ