ML Study Hub
← Back to Study Hub
Module 05 · Clustering & PCA
← Hub
Intuition K-Means PCA Dual SVD Biomarkers Traps Past Papers WCSS Tool Quiz Checklist
Module 05 · Unsupervised Learning & Manifold Discovery

Clustering (K-Means) & Dimensionality Reduction (PCA)

Master unsupervised manifold discovery. K-Means WCSS objective minimization, the Elbow method vs Silhouette score, handling the $p \gg n$ high-dimensional regime via Dual SVD, and translating abstract principal components into actionable biological driver biomarkers.

Jan 2025 (Q7 20M) Nov 2025 (Q5 20M) Dual SVD in O(n²p) WCSS Minimization Biomarker Loadings

01. Intuition & Concept Contrast

Unsupervised learning operates without ground-truth target labels ($y$). Clustering groups similar samples together in feature space, while dimensionality reduction compresses thousands of correlated features into a few orthogonal axes.

Unsupervised Goal

Clustering vs Dimensionality Reduction

Clustering (Grouping Samples): Finds natural discrete groupings among rows ($\mathbf{x}_i \in \mathbb{R}^p$). Assigns each observation to one of $K$ distinct clusters (e.g. disease subtypes).

Dimensionality Reduction (Compressing Columns): Finds a continuous lower-dimensional subspace ($k \ll p$) that preserves maximum sample variance, eliminating redundant assay noise.

Algorithmic Trade-off

Covariance Eigendecomposition vs Dual SVD

Full Covariance Method: Computes $p \times p$ covariance matrix $\mathbf{\Sigma} = \frac{1}{n-1}\mathbf{X}^T\mathbf{X}$. Requires $O(p^3)$ time ($1.25 \times 10^{11}$ operations for $5,000$ genes) $\implies$ Computationally Intractable.

Dual SVD Method: Decomposes centered data matrix $\tilde{\mathbf{X}} \in \mathbb{R}^{n \times p}$ directly into $\mathbf{U}\mathbf{S}\mathbf{V}^T$ in $O(n^2 p)$ time ($10^8$ operations) $\implies$ Instant & Numerically Stable.

02. K-Means Clustering Mathematical Formulation

Jan 2025 (Q7) Core Theory (20 Marks). Minimizing Within-Cluster Sum of Squares (WCSS).

The K-Means Optimization Objective
$$\arg\min_{\mathcal{C}} \operatorname{WCSS}(\mathcal{C}) = \sum_{k=1}^K \sum_{\mathbf{x} \in C_k} \|\mathbf{x} - \boldsymbol{\mu}_k\|^2$$ Where $\boldsymbol{\mu}_k = \frac{1}{|C_k|}\sum_{\mathbf{x} \in C_k}\mathbf{x}$ is the centroid of cluster $C_k$.

Lloyd's 2-Step Iterative Algorithm:

  1. Step 1: Assignment Step
    Assign each data vector $\mathbf{x}_i$ to its nearest cluster centroid in Euclidean distance: $$c_i^{(t)} = \arg\min_{k \in \{1, \dots, K\}} \|\mathbf{x}_i - \boldsymbol{\mu}_k^{(t)}\|^2$$
  2. Step 2: Update Step
    Recompute the centroid of each cluster as the center of mass of all assigned points: $$\boldsymbol{\mu}_k^{(t+1)} = \frac{1}{|C_k^{(t)}|} \sum_{\mathbf{x}_i \in C_k^{(t)}} \mathbf{x}_i$$
  3. Convergence: Repeat Steps 1 and 2 until centroid coordinates stop changing ($\|\boldsymbol{\mu}^{(t+1)} - \boldsymbol{\mu}^{(t)}\| < \epsilon$) or maximum iterations are reached.

Selecting Optimal $K$: The Elbow Method vs Silhouette Coefficient

1. The Elbow Method: Plot $\operatorname{WCSS}$ against candidate cluster counts $K \in [1, 10]$. As $K$ increases, WCSS monotonically decreases. Select the "Elbow" inflection point where adding another cluster yields diminishing returns in variance reduction.

2. Silhouette Coefficient ($s(i)$): Evaluates cluster compactness vs separation:

$$s(i) = \frac{b(i) - a(i)}{\max(a(i), b(i))} \in [-1, +1]$$
  • $a(i)$: Mean distance from point $i$ to all other points in its own cluster (intra-cluster compactness).
  • $b(i)$: Mean distance from point $i$ to points in the nearest neighboring cluster (inter-cluster separation).
  • $s \approx +1 \implies$ Excellent clustering; $s \approx 0 \implies$ Borderline overlap; $s < 0 \implies$ Assigned to wrong cluster.
The K-Means Initialization Problem & K-Means++:
Standard random initialization can strand centroids in poor local minima. K-Means++ seeds the first centroid uniformly at random, and selects each subsequent centroid with probability proportional to squared distance from the closest existing centroid: $$P(\mathbf{x}) = \frac{D(\mathbf{x})^2}{\sum_{\mathbf{x}'} D(\mathbf{x}')^2}$$ This spreads initial centroids far apart across feature space, guaranteeing $O(\log K)$ competitive solutions.

03. Principal Component Analysis via Dual SVD ($p \gg n$)

Nov 2025 (Q5) Bioinformatics Exam Problem (20 Marks). Gene expression dataset with $p = 5,000$ genes and $n = 100$ patient samples.

Why Dimensionality Reduction is Mandatory in $p \gg n$:
1. Distance Metric Concentration (Curse of Dimensionality): As dimensions $p \to \infty$, the relative contrast between the nearest and farthest neighbors vanishes: $$\lim_{p \to \infty} \frac{\operatorname{dist}_{\max} - \operatorname{dist}_{\min}}{\operatorname{dist}_{\min}} \to 0$$ Every patient becomes equidistant from every other patient in $\mathbb{R}^{5000}$, rendering Euclidean clustering ineffective.
2. Rank Deficiency: Data matrix $\mathbf{X} \in \mathbb{R}^{100 \times 5000}$ has maximum rank $\min(n-1, p) = 99$. The data lies entirely within a 99-dimensional hyperplane; the remaining 4,901 dimensions represent empty assay noise.
Singular Value Decomposition (Dual SVD)
$$\tilde{\mathbf{X}} = \mathbf{U} \mathbf{S} \mathbf{V}^T$$ $\mathbf{U} \in \mathbb{R}^{100 \times 100}$, $\mathbf{S} \in \mathbb{R}^{100 \times 100}$, $\mathbf{V} \in \mathbb{R}^{5000 \times 100}$.

Mathematical Derivation of the Dual SVD Shortcut:

1. Center the data matrix: $\tilde{\mathbf{X}} = \mathbf{X} - \mathbf{1}\boldsymbol{\mu}^T \in \mathbb{R}^{100 \times 5000}$.

2. The sample covariance matrix is $\mathbf{\Sigma} = \frac{1}{n-1}\tilde{\mathbf{X}}^T\tilde{\mathbf{X}} \in \mathbb{R}^{5000 \times 5000}$. Computing its eigenvalues directly requires $O(p^3) = 5000^3 = 1.25 \times 10^{11}$ operations.

3. Instead, apply SVD directly to $\tilde{\mathbf{X}}$ or eigendecompose the smaller Gram matrix $\mathbf{K} = \frac{1}{n-1}\tilde{\mathbf{X}}\tilde{\mathbf{X}}^T \in \mathbb{R}^{100 \times 100}$ in $O(n^2 p) = 100^2 \times 5000 = 5 \times 10^7$ operations ($2500\times$ faster!).

4. The singular values $s_j$ relate directly to the covariance eigenvalues $\lambda_j$:

$$\lambda_j = \frac{s_j^2}{n - 1}$$

5. Project the 100 patient samples into the reduced $k$-dimensional principal subspace ($k \ll 100$):

$$\mathbf{Z}_k = \tilde{\mathbf{X}}\mathbf{V}_k = \mathbf{U}_k \mathbf{S}_k \in \mathbb{R}^{100 \times k}$$

We select $k$ using the cumulative explained variance ratio ($\ge 85\%$):

$$\frac{\sum_{j=1}^k \lambda_j}{\sum_{m=1}^{n-1}\lambda_m} \ge 0.85$$

04. Translating Latent Clusters to Biological Biomarkers

Nov 2025 (Q5) Part (iii) Challenge & Solution (6 Marks).

The Interpretation Barrier:

K-Means identifies patient clusters in reduced space $\mathbf{Z}_k$ (e.g. $[\text{PC}_1, \text{PC}_2]$). However, principal components are mathematical linear combinations of all 5,000 genes: $\text{PC}_1 = v_{1,1}\text{Gene}_1 + \dots + v_{5000,1}\text{Gene}_{5000}$. Oncologists cannot treat patients based on eigenvectors; they need specific mutated driver genes.

The 3-Step Methodological Solution:

  1. 1. Factor Loading Deconvolution:
    Inspect the right singular vectors (gene loadings) $\mathbf{V}_k$. Genes with the highest absolute loading coefficients $|v_{j, k}|$ contribute most to separating the patient cohorts.
  2. 2. Differential Expression Analysis (DEA):
    Map cluster labels back to the original 5,000-gene space. Conduct two-sample Welch's $t$-tests comparing cluster samples against the remaining cohort with Benjamini-Hochberg False Discovery Rate (FDR) correction: $$q_j = P_j \times \frac{p}{\operatorname{rank}(P_j)} \le 0.05$$ Construct Volcano plots ($-\log_{10}(q)$ vs Fold Change) to isolate statistically significant up- and down-regulated biomarkers.
  3. 3. Gene Ontology (GO) & Pathway Enrichment (GSEA):
    Feed identified biomarker genes into KEGG or Gene Ontology pathway databases to translate mathematical clusters into biological disease mechanisms (e.g. *"Subtype 2 shows upregulation in DNA mismatch repair pathways"*).

05. Notation Decoding Matrix

Symbol Formal Name Plain-English Meaning Bioinformatics Value
$n, p$ Samples & Features Number of patients ($n$) and genes ($p$) $n = 100$ patients, $p = 5,000$ genes
$\operatorname{WCSS}$ Within-Cluster Sum of Squares Total compactness of points around their centroid Minimized by K-Means algorithm
$s(i)$ Silhouette Coefficient Score measuring balance of cohesion vs separation $s \in [-1, +1]$ (Higher is better)
$\mathbf{U}$ Left Singular Vectors Orthonormal eigenvectors of patient Gram matrix $\mathbf{U} \in \mathbb{R}^{100 \times 100}$
$\mathbf{S}$ Singular Values Square roots of eigenvalues scaled by $n-1$ $\lambda_j = s_j^2 / (n-1)$
$\mathbf{V}$ Right Singular Vectors (Loadings) Weights of each gene in forming principal components $\mathbf{V} \in \mathbb{R}^{5000 \times 100}$

06. Repeatable Step-by-Step Solving Protocol

Step 1: Justify Dimensionality Reduction ($p \gg n$)

State distance metric concentration ($\operatorname{dist}_{\max} \approx \operatorname{dist}_{\min}$) and rank deficiency ($\text{rank} \le 99$).

Step 2: Center Data Matrix $$\tilde{\mathbf{X}} = \mathbf{X} - \mathbf{1}\boldsymbol{\mu}^T$$
Step 3: State the Dual SVD Factorization $$\tilde{\mathbf{X}} = \mathbf{U}\mathbf{S}\mathbf{V}^T, \qquad \lambda_j = \frac{s_j^2}{n - 1}$$
Step 4: Select $k$ via Cumulative Variance Ratio $$\frac{\sum_{j=1}^k \lambda_j}{\sum_{m=1}^{n-1}\lambda_m} \ge 0.85$$
Step 5: Apply K-Means in Reduced Space ($\mathbf{Z}_k$)

Iterate assignment and centroid update steps until WCSS reaches local minimum.

Step 6: Biological Deconvolution

Inspect loadings $\mathbf{V}_k$, perform Differential Expression Analysis (FDR $q \le 0.05$), and pathway enrichment.

07. 6 Deadly Exam Traps & Common Student Mistakes

Trap 01: Full Covariance on $p \gg n$

Writing $\mathbf{\Sigma} = \mathbf{X}^T\mathbf{X} \in \mathbb{R}^{5000 \times 5000}$

Eigendecomposing a $5000 \times 5000$ matrix requires $O(p^3) = 1.25 \times 10^{11}$ operations. Always specify Dual SVD or $\mathbf{X}\mathbf{X}^T \in \mathbb{R}^{100 \times 100}$ in $O(n^2 p)$ operations.

Trap 02: Forgetting to Center Data

Applying SVD to Raw Data Matrix

PCA maximizes sample variance around the mean. If $\mathbf{X}$ is not centered ($\tilde{\mathbf{X}} = \mathbf{X} - \bar{\mathbf{X}}$), the first principal component points directly toward the dataset mean rather than direction of maximum variance.

Trap 03: Assuming K-Means Finds Global Optimum

Claiming K-Means Solves Convex Minimization

WCSS has multiple local minima depending on initial seeds. K-Means is non-convex; you must mention K-Means++ initialization or multiple restarts (`n_init=10`).

Trap 04: Confusing Loadings with Scores

Mixing Up $\mathbf{V}$ and $\mathbf{Z}$

$\mathbf{Z} \in \mathbb{R}^{n \times k}$ are the Principal Component Scores (patient coordinates). $\mathbf{V} \in \mathbb{R}^{p \times k}$ are the Factor Loadings (gene weights). Biologists inspect loadings $\mathbf{V}$ to identify biomarkers.

Trap 05: Skipping Feature Scaling Before K-Means

Clustering Unstandardized Features

Features with huge numerical ranges completely dominate the Euclidean distance metric. Z-score scaling is mandatory.

Trap 06: Picking $K=N$ on WCSS

Selecting $K$ where WCSS is Zero

Setting $K = N$ makes every point its own centroid ($\text{WCSS} = 0$), which provides zero clustering value. Choose $K$ at the inflection elbow or peak Silhouette score.

08. Complete University Question Bank (All Past PYQs Solved)

Nov 2025 · Question 5 Bioinformatics Gene Expression PCA & K-Means (20 Marks)
Question Statement:
A bioinformatics dataset has 5,000 genes ($p$) and 100 samples ($n$). Goal is to discover disease subtypes.
i) Justify need for dimensionality reduction in this specific scenario. (6 Marks)
ii) Propose a suitable technique and explain its working principle. (8 Marks)
iii) You apply K-Means to identify subtypes. Explain a key challenge when interpreting clusters and how to address it. (6 Marks)

Solution:

(i) Justification for Dimensionality Reduction ($p \gg n$) (6 Marks)
  1. Distance Metric Degeneration (Loss of Contrast): Pairwise Euclidean distances converge to a uniform scalar: $\lim_{p \to \infty} \frac{\text{dist}_{\max} - \text{dist}_{\min}}{\text{dist}_{\min}} \to 0$. In 5,000 dimensions, every patient becomes equidistant from every other patient.
  2. Rank Deficiency: The $100 \times 5000$ matrix has rank at most $\min(n-1, p) = 99$. The data lies entirely on a 99-dimensional manifold; the remaining 4,901 dimensions represent noise.
  3. Multicollinearity: Co-regulated genes contain massive redundancy that destabilizes distance calculations.
(ii) Proposed Technique: PCA via Dual SVD (8 Marks)

1. Center the matrix: $\tilde{\mathbf{X}} = \mathbf{X} - \mathbf{1}\boldsymbol{\mu}^T \in \mathbb{R}^{100 \times 5000}$.

2. Avoid computing the $5000 \times 5000$ covariance matrix ($O(p^3) = 1.25 \times 10^{11}$ operations). Instead apply SVD directly to $\tilde{\mathbf{X}}$ in $O(n^2 p) = 5 \times 10^7$ operations:

$$\tilde{\mathbf{X}} = \mathbf{U} \mathbf{S} \mathbf{V}^T$$

3. Relate singular values to covariance eigenvalues: $\lambda_j = \frac{s_j^2}{n - 1}$.

4. Project samples to $k$ components based on cumulative explained variance: $\mathbf{Z}_k = \tilde{\mathbf{X}}\mathbf{V}_k \in \mathbb{R}^{100 \times k}$ with $\sum_{j=1}^k \lambda_j / \sum \lambda_m \ge 0.85$.

(iii) Cluster Interpretation Challenge & Solution (6 Marks)

The Challenge: Principal components are abstract linear mixtures of all 5,000 genes ("Latent Space Barrier"). Clinicians cannot prescribe medicine based on eigenvalues; they need specific mutated driver genes.

Methodological Solutions:

  1. Factor Loading Inspection: Identify genes with largest absolute weights $|v_{j,k}|$ in the right singular vectors $\mathbf{V}_k$.
  2. Differential Expression Analysis (DEA): Map clusters back to 5,000-gene space. Perform Welch's $t$-tests with Benjamini-Hochberg FDR correction ($q \le 0.05$) to isolate statistically significant driver biomarkers.
  3. Gene Ontology Enrichment: Match biomarkers against KEGG biological pathways to reveal treatable disease mechanisms.
Jan 2025 · Question 7 K-Means Clustering Mechanics, WCSS & Elbow Method (20 Marks)
Question Statement:
Explain the working principle of the K-Means clustering algorithm. Formulate its mathematical objective function. Describe how the optimal number of clusters ($K$) is determined using the Elbow Method and Silhouette analysis. Discuss sensitivity to initialization and feature scaling. (20 Marks)

Solution:

1. Objective Formulation (6 Marks)
$$\arg\min_{\mathcal{C}} \operatorname{WCSS}(\mathcal{C}) = \sum_{k=1}^K \sum_{\mathbf{x} \in C_k} \|\mathbf{x} - \boldsymbol{\mu}_k\|^2$$

Iterates between Assignment Step ($c_i = \arg\min_k \|\mathbf{x}_i - \boldsymbol{\mu}_k\|^2$) and Update Step ($\boldsymbol{\mu}_k = \frac{1}{|C_k|}\sum \mathbf{x}_i$).

2. Determining Optimal $K$ (6 Marks)
  • Elbow Method: Plot WCSS vs $K$. Identify the sharp inflection "elbow" point where marginal variance explained decreases.
  • Silhouette Analysis: $s(i) = \frac{b(i) - a(i)}{\max(a(i), b(i))}$. Choose $K$ that maximizes mean silhouette coefficient across all points.
3. Initialization & Scaling (8 Marks)
  • K-Means++: Prevents poor local minima by selecting initial seeds with probability $P(\mathbf{x}) \propto D(\mathbf{x})^2$, spreading centroids across data space.
  • Feature Scaling: Mandatory. Without Z-score standardization, features with larger magnitudes dominate the Euclidean distance calculation.

09. Interactive Offline WCSS Calculator

Test your understanding of Within-Cluster Sum of Squares. Calculate Euclidean distances from 1D points to candidate centroids.

Click button to compute cluster centroids and WCSS.

10. Active Recall Self-Test Quiz

Answer the questions below to evaluate progress (0/4 answered).
1. Why is full covariance matrix eigendecomposition avoided when $p \gg n$ ($5000$ genes, $100$ samples)?
Correct: (b) Dual SVD evaluates the $100 \times 100$ Gram matrix in $O(n^2 p) = 5 \times 10^7$ operations, which is $2500\times$ faster.
2. What is the mathematical relationship between SVD singular values ($s_j$) and covariance eigenvalues ($\lambda_j$)?
Correct: (c) Because $\mathbf{\Sigma} = \frac{1}{n-1}\tilde{\mathbf{X}}^T\tilde{\mathbf{X}} = \frac{1}{n-1}\mathbf{V}\mathbf{S}^2\mathbf{V}^T$, $\lambda_j = \frac{s_j^2}{n-1}$.
3. How does K-Means++ improve upon standard random centroid initialization?
Correct: (a) Spreading initial centroids far apart across feature space prevents getting trapped in poor local minima.
4. How do data scientists solve the "Latent Space Barrier" when translating PCA clusters into biological insights?
Correct: (b) Deconvolving factor loadings and running Welch's $t$-tests with FDR correction isolates real biomarker genes.

11. Exam Hall Answer Sheet Presentation Checklist

1. Cite the 3 Reasons for Dimensionality Reduction in $p \gg n$

State: (1) Distance metric concentration, (2) Rank deficiency ($\le 99$), (3) High collinearity and visual intractability.

2. Write the Centering Step Explicitly

Always write $\tilde{\mathbf{X}} = \mathbf{X} - \mathbf{1}\boldsymbol{\mu}^T$. Mention that uncentered PCA distorts variance.

3. State the Dual SVD Complexity Shortcut

Highlight: $O(n^2 p) = 5 \times 10^7$ vs $O(p^3) = 1.25 \times 10^{11}$ operations ($2500\times$ speedup). Write $\lambda_j = \frac{s_j^2}{n-1}$.

4. Write the WCSS Objective Function Boxed

Write $\arg\min \sum_{k=1}^K \sum_{\mathbf{x} \in C_k} \|\mathbf{x} - \boldsymbol{\mu}_k\|^2$. Detail assignment and update steps.

5. For Cluster Interpretation, Mention DEA & Pathway Enrichment

Cite: (1) Factor loadings $\mathbf{V}$, (2) Differential Expression Analysis with Benjamini-Hochberg FDR $q \le 0.05$, (3) Gene Ontology (GO) enrichment.