Home
Omar Hosney
LinkedIn Profile

t-SNE and UMAP Dimensionality Reduction Cheat Sheet

Introduction to Dimensionality Reduction

t-SNE Overview

UMAP Overview

t-SNE Algorithm Steps

UMAP Algorithm Steps

t-SNE Parameters

UMAP Parameters

t-SNE Code Example

import numpy as np from sklearn.manifold import TSNE import matplotlib.pyplot as plt # Generate sample data X = np.random.randn(1000, 50) y = np.random.randint(0, 5, 1000) # Apply t-SNE tsne = TSNE(n_components=2, random_state=42) X_tsne = tsne.fit_transform(X) # Plot results plt.figure(figsize=(10, 8)) scatter = plt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=y, cmap='viridis') plt.colorbar(scatter) plt.title('t-SNE visualization of random data') plt.show()

UMAP Code Example

import numpy as np import umap import matplotlib.pyplot as plt # Generate sample data X = np.random.randn(1000, 50) y = np.random.randint(0, 5, 1000) # Apply UMAP reducer = umap.UMAP(random_state=42) X_umap = reducer.fit_transform(X) # Plot results plt.figure(figsize=(10, 8)) scatter = plt.scatter(X_umap[:, 0], X_umap[:, 1], c=y, cmap='viridis') plt.colorbar(scatter) plt.title('UMAP visualization of random data') plt.show()

Comparing t-SNE and UMAP

import numpy as np from sklearn.manifold import TSNE import umap import matplotlib.pyplot as plt # Generate sample data X = np.random.randn(1000, 50) y = np.random.randint(0, 5, 1000) # Apply t-SNE tsne = TSNE(n_components=2, random_state=42) X_tsne = tsne.fit_transform(X) # Apply UMAP reducer = umap.UMAP(random_state=42) X_umap = reducer.fit_transform(X) # Plot results fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 8)) scatter1 = ax1.scatter(X_tsne[:, 0], X_tsne[:, 1], c=y, cmap='viridis') ax1.set_title('t-SNE visualization') plt.colorbar(scatter1, ax=ax1) scatter2 = ax2.scatter(X_umap[:, 0], X_umap[:, 1], c=y, cmap='viridis') ax2.set_title('UMAP visualization') plt.colorbar(scatter2, ax=ax2) plt.show()

Advantages of t-SNE

Advantages of UMAP

Limitations of t-SNE

Limitations of UMAP

When to Use t-SNE

When to Use UMAP