UMAP
Uniform Manifold Approximation and Projection
A dimensionality reduction technique faster and more scalable than t-SNE. Widely used for high-dimensional data visualization and clustering.
Uniform Manifold Approximation and Projection
A dimensionality reduction technique faster and more scalable than t-SNE. Widely used for high-dimensional data visualization and clustering.
UMAP (Uniform Manifold Approximation and Projection) is a nonlinear dimensionality reduction algorithm that projects high-dimensional data into a lower-dimensional space (mainly 2D or 3D). It has a similar purpose to t-SNE, but its mathematical foundation is different, and it has significant advantages in speed and scalability.
UMAP was published in 2018 by Leland McInnes and John Healy. Based on manifold theory from topology and fuzzy set theory, it preserves the high-dimensional structure of data in a lower-dimensional space while providing speeds 10-100 times faster than t-SNE.
UMAP's key hyperparameters are n_neighbors (precision of local structure) and min_dist (minimum distance between points). A large n_neighbors preserves global structure well, while a small one emphasizes local structure. A small min_dist makes clusters dense, while a large one makes them dispersed.
In practice, UMAP is widely used for embedding visualization, anomaly detection, cluster analysis, and data exploration. In particular, UMAP is chosen over t-SNE when visualizing LLM embeddings or when quickly reducing the dimensionality of millions of data points.
This is an example of embedding visualization using UMAP.
import umap
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.preprocessing import StandardScaler
# λ°μ΄ν° λ‘λ (MNIST μκΈμ¨ μ«μ)
digits = load_digits()
X = digits.data # (1797, 64) - 64μ°¨μ
y = digits.target
# λ°μ΄ν° μ κ·ν (κΆμ₯)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# UMAP μ°¨μ μΆμ
reducer = umap.UMAP(
n_neighbors=15, # μ§μ ꡬ쑰 μ λ°λ (5-50)
min_dist=0.1, # μ κ° μ΅μ 거리 (0.0-1.0)
n_components=2, # μΆλ ₯ μ°¨μ
metric='euclidean', # 거리 λ©νΈλ¦
random_state=42 # μ¬νμ±
)
# 64μ°¨μ -> 2μ°¨μ ν¬μ
X_embedded = reducer.fit_transform(X_scaled)
print(f"μλ³Έ: {X.shape} -> UMAP κ²°κ³Ό: {X_embedded.shape}")
# μκ°ν
plt.figure(figsize=(12, 8))
scatter = plt.scatter(
X_embedded[:, 0],
X_embedded[:, 1],
c=y,
cmap='Spectral',
s=5,
alpha=0.7
)
plt.colorbar(scatter, label='μ«μ λ μ΄λΈ')
plt.title('UMAP: μκΈμ¨ μ«μ 64μ°¨μ β 2μ°¨μ μκ°ν')
plt.xlabel('UMAP 1')
plt.ylabel('UMAP 2')
plt.tight_layout()
plt.savefig('umap_digits.png', dpi=150)
plt.show()
# μλ‘μ΄ λ°μ΄ν°μ μ μ© (νμ΅λ reducer μ¬μ©)
new_data = np.random.randn(10, 64) # μ λ°μ΄ν°
new_data_scaled = scaler.transform(new_data)
new_embedded = reducer.transform(new_data_scaled)
print(f"μ λ°μ΄ν° ν¬μ κ²°κ³Ό: {new_embedded.shape}")
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free
Using only the default values fails to properly reflect the characteristics of the data. Experiment with various values of n_neighbors and min_dist.
If there are features with different scales, the results will be distorted. Preprocess with StandardScaler or MinMaxScaler.
Apply UMAP after normalizing the data, visualize multiple hyperparameter combinations, and fix random_state to ensure reproducibility.