πŸ€– AI/ML

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.

πŸ“– Detailed Explanation

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.

πŸ’» Code Example

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}")

πŸ—£οΈ Say It Like This in Practice

πŸ’¬ In a Meeting
"I tried UMAP for visualizing customer segments, and it was 10 times faster than t-SNE, and the cluster separation came out cleaner too. Even 1 million records take only a few minutes."
πŸ’¬ In an Interview
"We used UMAP to evaluate embedding quality. We adjusted n_neighbors to check the cluster distribution and evaluated it quantitatively together with the Silhouette Score."
πŸ’¬ In a Technical Discussion
"UMAP preserves global structure better than t-SNE. However, if min_dist is too small, it can look like overfitting, so start with around 0.1~0.3."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Common Mistakes & Cautions

❌

Fixed hyperparameters

Using only the default values fails to properly reflect the characteristics of the data. Experiment with various values of n_neighbors and min_dist.

❌

Missing normalization

If there are features with different scales, the results will be distorted. Preprocess with StandardScaler or MinMaxScaler.

βœ…

The right way

Apply UMAP after normalizing the data, visualize multiple hyperparameter combinations, and fix random_state to ensure reproducibility.

πŸ”— Related Terms

πŸ“š Learn More