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 μ<em>i\mu<em>i for i=1,2,…,ki = 1, 2, …, k where μ</em>i∈R\mu</em>i \in \mathbb{R}.

Step 2: Assignment of Observations

  • For each observation xjx_j, find the nearest centroid using the following formula:

    • To find the nearest centroid:

    • arg min<em>i∣∣x</em>j−μi∣∣2\text{arg min}<em>{i} ||x</em>j - \mu_i||^2

    • ∣∣x<em>j−μ</em>i∣∣2||x<em>j - \mu</em>i||^2 denotes the squared Euclidean distance between observation x<em>jx<em>j and centroid μ</em>i\mu</em>i.

Step 3: Cluster Assignment

  • Assign observation xjx_j to the cluster of the nearest centroid identified in Step 2.

    • Notation:

    • Assign observation to cluster CiC_i.

Step 4: Re-computation of Centroids

  • For each cluster, recompute the centroid based on the observations assigned to that cluster.

    • New centroid formula:

    • μ<em>i=1∣C</em>i∣∑<em>x</em>j∈C<em>ix</em>j\mu<em>i = \frac{1}{|C</em>i|} \sum<em>{x</em>j \in C<em>i} x</em>j, where ∣Ci∣|C_i| is the number of observations in cluster ii.

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 k=2k = 2?

      • 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 k=4k = 4?

    • 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 nn observations into kk 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 kk initial centroids within the feature space.

  • Notation:

    • kk = pre-defined number of clusters.

    • Centroids: represented as μ<em>i\mu<em>{i} for i=1,2,…,ki = 1, 2, \dots, k, where μ</em>i∈Rd\mu</em>{i} \in \mathbb{R}^{d} (dd-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 xjx_{j}, calculate the distance to all centroids and assign the observation to the nearest one.

  • Formula: To find the optimal cluster index ii for observation xjx_{j}:

    • arg min<em>i∣x</em>j−μi∣2\text{arg min}<em>{i} |x</em>{j} - \mu_{i}|^{2}

    • where ∣x<em>j−μ</em>i∣2|x<em>{j} - \mu</em>{i}|^{2} is the squared Euclidean distance.

Step 3: Cluster Formation
  • Partition the data into sets C<em>1,C</em>2,…,CkC<em>{1}, C</em>{2}, \dots, C_{k} based on the closest centroid assignment.

  • Mathematically, C<em>i=x</em>j:∣x<em>j−μ</em>i∣2≤∣x<em>j−μ</em>l∣2 for all l=1,…,kC<em>{i} = {x</em>{j} : |x<em>{j} - \mu</em>{i}|^{2} \leq |x<em>{j} - \mu</em>{l}|^{2} \text{ for all } l = 1, \dots, k}.

Step 4: Re-computation (Maximization Step)
  • Calculate the new mean (centroid) for each cluster by averaging the coordinates of all assigned points.

  • Formula:

    • μ<em>i=1∣C</em>i∣∑<em>x</em>j∈C<em>ix</em>j\mu<em>{i} = \frac{1}{|C</em>{i}|} \sum<em>{x</em>{j} ∈ C<em>{i}} x</em>{j}

    • ∣Ci∣|C_{i}| represents the cardinality (number of observations) in cluster ii.

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 (kk)

Selecting the value of kk is critical as the algorithm will force the data into kk groups regardless of the underlying structure.

The Elbow Method and WCSS
  • Within-Cluster Sum of Squares (WCSS): Measures the total variance within each cluster.

    • WCSS=∑<em>i=1k∑</em>x<em>j∈C</em>i∣x<em>j−μ</em>i∣2WCSS = \sum<em>{i=1}^{k} \sum</em>{x<em>{j} \in C</em>{i}} |x<em>{j} - \mu</em>{i}|^{2}

  • Elbow Rule: Plot WCSS against different values of kk. As kk increases, WCSS decreases. The "elbow" occurs at the point where the rate of decrease slows down significantly, suggesting an optimal kk.

Additional Considerations
  • Over-clustering: If kk is too high (e.g., k=4k=4 when only 2 biological groups exist), the algorithm will split natural populations into arbitrary sub-groups.

  • Under-clustering: If kk is too low (e.g., k=2k=2 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 kk 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 nn observations into kk sets S=S<em>1,S</em>2,…,SkS = {S<em>1, S</em>2, \dots, S_k} 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 kk points as initial centroids μ<em>1,…,μ</em>k\mu<em>1, \dots, μ</em>k.

  • 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 x<em>jx<em>j to the cluster C</em>iC</em>i with the closest centroid.

  • Formula:

    • arg min<em>i∣∣x</em>j−μi∣∣2\text{arg min}<em>{i} ||x</em>j - \mu_i||^2

  • Insight: Computational Complexity: The complexity of this step is O(n⋅k⋅d)O(n \cdot k \cdot d), where nn is the number of observations and dd is the number of dimensions. This makes it highly scalable compared to hierarchical clustering (O(n2)O(n^2)).

Step 3: Re-computation
  • Mechanism: Update centroids to be the mean of all points assigned to that cluster.

  • Formula:

    • μ<em>i=1∣C</em>i∣∑<em>x</em>j∈C<em>ix</em>j\mu<em>i = \frac{1}{|C</em>i|} \sum<em>{x</em>j \in C<em>i} x</em>j

  • 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 kk
Statistical Metrics
  • Within-Cluster Sum of Squares (WCSS): Measures compactness.

    • WCSS=∑<em>i=1k∑</em>x<em>j∈C</em>i∣∣x<em>j−μ</em>i∣∣2WCSS = \sum<em>{i=1}^{k} \sum</em>{x<em>j \in C</em>i} ||x<em>j - \mu</em>i||^2

  • The Elbow Method: Plotting WCSS vs. kk. The "elbow" represents the point where adding another cluster provides diminishing returns in variance reduction.

Biological Validation (The "Manual" Insight)
  • If kk is too low (under-clustering), distinct cell types (e.g., T-cells and B-cells) might be merged.

  • If kk 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.