Genomic Data Visualization: K-means Clustering Study Notes
Course Overview and Lesson Objectives
Course Title: EN.580.428 Genomic Data Visualization
Lesson Number: Lesson 5
Main Topic: K-means clustering
Instructor: Prof. Jean Fan
Learning Objectives
Students will learn about K-means clustering and its application.
Students will practice interpreting data visualizations that integrate:
Normalization
Dimensionality reduction
K-means clustering
Introduction to K-means Clustering
Why Cluster?
Purpose of Clustering:
Clustering aims to group data points with similar attributes together.
Example Genes:
Gene A
Gene B
K-means Clustering Algorithm
Step 1: Initialization
Randomly place k centroids within the data space.
Centroids represent the center of a cluster.
Notation:
Let k be the number of clusters.
Centroids represented as for where .
Step 2: Assignment of Observations
For each observation , find the nearest centroid using the following formula:
To find the nearest centroid:
denotes the squared Euclidean distance between observation and centroid .
Step 3: Cluster Assignment
Assign observation to the cluster of the nearest centroid identified in Step 2.
Notation:
Assign observation to cluster .
Step 4: Re-computation of Centroids
For each cluster, recompute the centroid based on the observations assigned to that cluster.
New centroid formula:
, where is the number of observations in cluster .
Step 5: Iteration Until Convergence
Repeat Steps 2 through 4 until cluster assignments no longer change.
Indicates that the algorithm has converged and clusters are stable.
Choosing the Appropriate Number of Clusters (k)
Assessment of different values for k is crucial:
Considerations:
What if ?
Visual impact on clustering represented with Gene A and Gene B.
Within-cluster sum of squares (WCSS) should be assessed for different k values to determine the best fit.
WCSS measures the compactness of clusters; smaller values are preferable.
Other evaluations can include cluster separation and overall clustering validity.
What if ?
Assess recommendation through visualizations of Gene A, Gene B, and cluster analysis.
Practical Application Exercises
Exercise 1: Gene Expression Comparison
Based on the visualizations, determine which cell-type expresses more gene Z:
Compare Cell-type B to Cell-type D.
Exercise 2: Loading Value Determination
Based on the visualizations, identify:
What is the loading value of gene Z on Principal Component 2 (PC2)?
Exercise 3: Population Expression Analysis
Compare gene Z expression between:
Cell-type A and Cell-type B.
Exercise 4: t-SNE Visualization Evaluation
Based on a t-SNE plot, determine which population expresses more gene Z:
Evaluate Cell-type A versus Cell-type B, and state additional required information for accurate assessment.
Exercise 5: Application of K-means Clustering
Discuss what types of data can be subjected to k-means clustering.
Examples include:
Gene expression data
Principal Components (PCs)
t-SNE results
Evaluate similarities of the resulting clusters from different data types.
Course Overview and Lesson Objectives
Course Title: EN.580.428 Genomic Data Visualization
Lesson Number: Lesson 5
Main Topic: K-means clustering
Instructor: Prof. Jean Fan
Learning Objectives
Understanding K-means: Comprehend the mathematical foundations and iterative nature of the K-means clustering algorithm.
Data Integration: Learn to interpret complex data visualizations that combine:
Normalization: Scaling data to ensure features contribute equally to distance calculations.
Dimensionality Reduction: Using techniques like PCA or t-SNE to simplify high-dimensional genomic data before clustering.
K-means Clustering: Identifying discrete subpopulations within the reduced feature space.
Introduction to K-means Clustering
Why Cluster?
Goal of Clustering: To partition observations into clusters in which each observation belongs to the cluster with the nearest mean (centroid).
Biological Motivation: In genomics, we often cluster to:
Identify Cell Types: Grouping cells with similar gene expression profiles.
Functional Annotation: Grouping genes that are co-expressed, suggesting they may share a biological pathway.
Pattern Recognition: Discovering structure in large datasets like single-cell RNA-seq.
K-means Clustering Algorithm
K-means is an iterative, non-deterministic algorithm that follows the Expectation-Maximization (EM) framework.
Step 1: Initialization
Seed Placement: Randomly place initial centroids within the feature space.
Notation:
= pre-defined number of clusters.
Centroids: represented as for , where (-dimensional space).
Sensitivity: Note that the final result can depend on initial centroid placement; hence, many implementations run the algorithm multiple times or use "K-means++" for better initialization.
Step 2: Assignment (Expectation Step)
For each observation , calculate the distance to all centroids and assign the observation to the nearest one.
Formula: To find the optimal cluster index for observation :
where is the squared Euclidean distance.
Step 3: Cluster Formation
Partition the data into sets based on the closest centroid assignment.
Mathematically, .
Step 4: Re-computation (Maximization Step)
Calculate the new mean (centroid) for each cluster by averaging the coordinates of all assigned points.
Formula:
represents the cardinality (number of observations) in cluster .
Step 5: Iteration and Convergence
Repeat Steps 2 and 4 until the centroids no longer shift significantly or cluster assignments remain stable.
Convergence: The algorithm is guaranteed to converge to a local minimum, though not necessarily the global optimum.
Choosing the Appropriate Number of Clusters ()
Selecting the value of is critical as the algorithm will force the data into groups regardless of the underlying structure.
The Elbow Method and WCSS
Within-Cluster Sum of Squares (WCSS): Measures the total variance within each cluster.
Elbow Rule: Plot WCSS against different values of . As increases, WCSS decreases. The "elbow" occurs at the point where the rate of decrease slows down significantly, suggesting an optimal .
Additional Considerations
Over-clustering: If is too high (e.g., when only 2 biological groups exist), the algorithm will split natural populations into arbitrary sub-groups.
Under-clustering: If is too low (e.g., when 4 groups exist), the algorithm will merge distinct biological entities.
Practical Application Exercises
Exercise 1: Gene Expression Comparison
Task: Analyze cluster-specific expression. If Cell-type B and Cell-type D are identified via K-means, which shows higher expression of marker Gene Z?
Exercise 2: Loading Value Determination
Task: In a PCA plot used for K-means input, identify the contribution (loading) of Gene Z to the variance captured by Principal Component 2 (PC2).
Exercise 3: Population Expression Analysis
Task: Quantitative comparison of Gene Z transcript levels between automated clusters (Cell-type A vs Cell-type B).
Exercise 4: t-SNE and Cluster Validation
Task: Map K-means clusters onto a t-SNE plot. If clusters overlap significantly, consider if is too large or if the dimensionality reduction parameters need adjustment.
Exercise 5: Data Input Compatibility
Input Types: K-means can be applied to:
Raw/Normalized Gene Expression (high-dimensional).
PCA coordinates (reduced dimensions, noise filtered).
t-SNE/UMAP coordinates (for spatial interpretation, though usually not recommended for primary discovery due to non-linear distortion).
Course Overview and Lesson Objectives
Course Title: EN.580.428 Genomic Data Visualization
Lesson Number: Lesson 5
Main Topic: K-means clustering
Instructor: Prof. Jean Fan
Learning Objectives
Conceptual Mastery: Understand the iterative Expectation-Maximization (EM) framework of K-means.
Data Integration Insights:
Normalization: Crucial because K-means relies on Euclidean distance; features with larger scales will disproportionately influence cluster assignments.
Dimensionality Reduction: Applying PCA before K-means can denoise data and improve computational efficiency by focusing on the most variant axes.
K-means Clustering: Partitioning complex genomic landscapes into discrete, interpretable biological entities (e.g., cell types).
Introduction to K-means Clustering
Clustering Goal: Partition observations into sets to minimize the within-cluster sum of squares.
Biological Insights:
Assumption of Sphericality: K-means inherently assumes clusters are isotropic (spherical). In genomic data, if cell types have elongated shapes (common in developmental trajectories), K-means may struggle compared to density-based or graph-based methods.
Hard Clustering: Each point belongs to exactly one cluster. This differs from "soft clustering" (like Gaussian Mixture Models) where points have probabilities of belonging to multiple groups.
K-means Clustering Algorithm (The EM Iteration)
Step 1: Initialization
Mechanism: Randomly select points as initial centroids .
Insight: The Initialization Bias: Random initialization can lead to local minima. Advanced implementations use K-means++, which spreads out initial centroids to ensure better coverage of the data space and faster convergence.
Step 2: Assignment
Mechanism: Assign each to the cluster with the closest centroid.
Formula:
Insight: Computational Complexity: The complexity of this step is , where is the number of observations and is the number of dimensions. This makes it highly scalable compared to hierarchical clustering ().
Step 3: Re-computation
Mechanism: Update centroids to be the mean of all points assigned to that cluster.
Formula:
Insight: Outlier Sensitivity: Because the centroid is a mean, a single extreme outlier can significantly shift the centroid away from the dense center of a biological population.
Step 4: Convergence
Mechanism: Iteration stops when labels no longer change or a maximum number of cycles is reached.
Mathematical Property: K-means is guaranteed to converge because the total variance (WCSS) decreases monotonically at each step.
Choosing the Optimal
Statistical Metrics
Within-Cluster Sum of Squares (WCSS): Measures compactness.
The Elbow Method: Plotting WCSS vs. . The "elbow" represents the point where adding another cluster provides diminishing returns in variance reduction.
Biological Validation (The "Manual" Insight)
If is too low (under-clustering), distinct cell types (e.g., T-cells and B-cells) might be merged.
If is too high (over-clustering), the algorithm may split a biologically uniform population based on stochastic noise or batch effects.
Practical Application Exercises
Exercise 1: Gene Expression Comparison
Evaluate if Cell-type B vs Cell-type D expresses more marker Gene Z. This validates whether the clustering algorithm successfully separated cells based on biologically relevant markers.
Exercise 2: Loading Value Determination
Identify the loading of Gene Z on PC2. This insight helps determine if the specific gene used for clustering is a primary driver of the variation captured in the reduced dimensional space.
Exercise 3: t-SNE and Visualization
Mapping K-means labels onto t-SNE or UMAP plots.
Key Insight: Clusters that appear distinct in t-SNE but are split or merged by K-means suggest a mismatch between the global structure (often captured by PCA/K-means) and the local neighborhood structure (captured by t-SNE).
Exercise 4: Input Data Types
PCA Inputs: Usually the best balance of speed and noise reduction.
t-SNE/UMAP Inputs: Risky, as these embeddings distort distances to preserve local topology; clustering on them can sometimes lead to mathematically "pretty" but biologically misleading clusters.