Lesson · 40 min · Free
Seeing Structure: PCA and UMAP
Seeing Structure: PCA and UMAP body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2c3e50; } pre { background-color: #ecf0f1; padding: 15px; border-radius: 5px; overflow-x: auto; } code { f
Computational Biomedicine: From Command Line to Single-Cell
Seeing Structure: PCA and UMAP
In the realm of biomedical data analysis, especially with high-dimensional datasets like gene expression profiles from single-cell RNA sequencing (scRNA-seq), understanding the underlying structure is paramount. High-dimensional data refers to datasets with many features (e.g., thousands of genes) for each sample (e.g., each cell). Visualizing such data directly is impossible, as we are limited to 2 or 3 dimensions. This is where dimensionality reduction techniques come into play, allowing us to project our complex data into a lower-dimensional space while preserving as much meaningful information as possible. Two widely used and powerful dimensionality reduction techniques are Principal Component Analysis (PCA) and Uniform Manifold Approximation and Projection (UMAP). While both aim to reduce dimensionality, they operate on different principles and are suited for different types of data structures.
Principal Component Analysis (PCA)
PCA is a linear dimensionality reduction technique. It works by identifying the directions (principal components) along which the data varies the most. The first principal component (PC1) captures the largest amount of variance in the data, the second principal component (PC2) captures the second largest amount of variance orthogonal to PC1, and so on. By projecting the data onto these principal components, we can reduce the number of dimensions while retaining the most significant variations. PCA is excellent for finding global linear relationships and is computationally efficient. However, it assumes that the data's underlying structure is linear. If the data has a complex, non-linear manifold structure (like a Swiss roll), PCA might not effectively separate distinct clusters or reveal subtle relationships. Here's a conceptual Python example using scikit-learn to perform PCA: import numpy as np from sklearn.decomposition import PCA import matplotlib.pyplot as plt # Generate some example high-dimensional data (e.g., gene expression) # 100 samples (cells), 1000 features (genes) np.random.seed(42) data = np.random.rand(100, 1000) # Add some structure: two distinct groups data[0:50, 0:100] += 5 data[50:100, 100:200] += 5 # Initialize PCA to reduce to 2 components for visualization pca = PCA(n_components=2) # Fit PCA to the data and transform it reduced_data_pca = pca.fit_transform(data) # Plot the results plt.figure(figsize=(8, 6)) plt.scatter(reduced_data_pca[:, 0], reduced_data_pca[:, 1], c=np.concatenate([np.zeros(50), np.ones(50)]), cmap='viridis') plt.title('PCA of High-Dimensional Data') plt.xlabel('Principal Component 1') plt.ylabel('Principal Component 2') plt.colorbar(label='Group') plt.show() print(f"Explained variance ratio by PC1: {pca.explained_variance_ratio_[0]:.2f}") print(f"Explained variance ratio by PC2: {pca.explained_variance_ratio_[1]:.2f}")
Uniform Manifold Approximation and Projection (UMAP)
UMAP, on the other hand, is a non-linear dimensionality reduction technique. It's designed to preserve both local and global data structure, making it particularly powerful for visualizing complex, non-linear relationships often found in biological data (e.g., developmental trajectories of cells, distinct cell types). UMAP works by constructing a high-dimensional graph representation of the data and then optimizing a low-dimensional graph to be as structurally similar as possible. UMAP is generally praised for its ability to reveal fine-grained clusters and continuous trajectories, often producing more aesthetically pleasing and interpretable visualizations than PCA or even t-SNE (another popular non-linear method). However, its non-linear nature means that distances in the UMAP plot don't always directly correspond to Euclidean distances in the original high-dimensional space, and its results can be sensitive to parameter choices (though often less so than t-SNE). Here's a conceptual Python example using UMAP to perform dimensionality reduction: import numpy as np import umap import matplotlib.pyplot as plt # Generate some example high-dimensional data with a non-linear structure # (e.g., a "swiss roll" like structure for demonstration) np.random.seed(42) n_samples = 200 t = 1.5 * np.pi * (1 + 2 * np.random.rand(n_samples)) x = t * np.cos(t) y = 8 * np.random.rand(n_samples) z = t * np.sin(t) data_swiss_roll = np.vstack((x, y, z)).T # Add some noise to make it higher dimensional for UMAP noise = np.random.rand(n_samples, 97) * 2 data_high_dim_swiss_roll = np.hstack((data_swiss_roll, noise)) # Initialize UMAP # n_neighbors: controls how UMAP balances local vs. global structure # min_dist: controls how tightly UMAP allows points to be packed together reducer = umap.UMAP(n_neighbors=15, min_dist=0.1, random_state=42) # Fit UMAP to the data and transform it reduced_data_umap = reducer.fit_transform(data_high_dim_swiss_roll) # Plot the results plt.figure(figsize=(8, 6)) plt.scatter(reduced_data_umap[:, 0], reduced_data_umap[:, 1], c=t, cmap='plasma') # Color by original 't' value plt.title('UMAP of High-Dimensional Swiss Roll Data') plt.xlabel('UMAP Component 1') plt.ylabel('UMAP Component 2') plt.colorbar(label='Original Parameter (t)') plt.show()
When to Use Which?
PCA: Good for initial exploration, finding major sources of variation, noise reduction, and when linearity is a reasonable assumption. It's often used as a preprocessing step before other analyses. UMAP: Ideal for visualizing complex, non-linear relationships, identifying distinct cell populations in scRNA-seq data, and revealing continuous trajectories. When you suspect manifold structure in your data, UMAP is often the better choice for visualization.
Practice Exercise
Imagine you have a single-cell RNA sequencing (scRNA-seq) dataset of immune cells from a patient with an autoimmune disease. This dataset contains expression levels for 20,000 genes across 10,000 cells. You want to identify distinct immune cell types and potentially observe any continuous cellular states (e.g., activation states or differentiation pathways). Which dimensionality reduction technique (PCA or UMAP) would you primarily choose for initial visualization and exploration of cell types, and why? Briefly explain your reasoning, considering the characteristics of scRNA-seq data and the strengths of each method.
Key Takeaways
Dimensionality reduction is crucial for visualizing and understanding high-dimensional biomedical data. PCA (Principal Component Analysis) is a linear method, excellent for capturing major variances and global linear structures. UMAP (Uniform Manifold Approximation and Projection) is a non-linear method, superior for preserving local and global manifold structures, revealing complex relationships and distinct clusters. The choice between PCA and UMAP depends on the underlying structure of your data and the specific insights you aim to gain. For scRNA-seq, UMAP is often preferred for visualizing cell types and trajectories.
Watch the full lesson — free
This topic is part of Computational Biomedicine: From Command Line to Single-Cell, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →