flowchart TD
subgraph Prep["Step 1: Scale"]
S0["Standardize Numeric X<br/>StandardScaler() -> mean 0, SD 1"]
end
subgraph Goal["Step 2: Goal"]
G1["Group Rows<br/>Clustering"]
G2["Compress Columns<br/>Dim. Reduction"]
end
subgraph Cluster["Clustering Methods"]
C1["K-Means<br/>Spherical, fast, set k<br/>Audit: Elbow + Silhouette"]
C2["Ward Hierarchical<br/>Deterministic tree<br/>Minimizes within-var"]
C3["DBSCAN<br/>Arbitrary density shapes<br/>Flags outliers as -1"]
end
subgraph Reduce["Reduction Methods"]
R1["PCA via SVD<br/>Linear, orthogonal PCs<br/>Explainable loadings + MSE"]
R2["t-SNE / UMAP<br/>2D local visualization only<br/>Do NOT cluster on axes"]
end
S0 --> G1
S0 --> G2
G1 -->|"Compact blobs"| C1
G1 -->|"Nested tree"| C2
G1 -->|"Outliers / noise"| C3
G2 -->|"Variance + audit"| R1
G2 -->|"Visual explore"| R2
25 Unsupervised Learning — Clustering (K-Means, Hierarchical, DBSCAN) and Dimensionality Reduction (PCA)
Chapter Mission: Discover hidden clinical phenotypes, district risk profiles, and low-dimensional latent biomarker axes without target labels (\(y\)). Using pure NumPy and Scikit-Learn on Bangladesh clinic triage records (
clinic_triage), you will master why feature scaling governs Euclidean geometry, implement K-Means and PCA via SVD from scratch, compare K-Means, Ward Hierarchical Clustering, and DBSCAN anomaly flagging, and guard against common nonlinear-embedding traps in t-SNE / UMAP.
Chapter Companion Notebooks (1-to-1 Mirrored in Google Colab)
| Lab Type | Interactive Colab Notebook | Scan (Print) |
|---|---|---|
| Walkthrough | ch25 / 01_walkthrough.ipynb | ![]() |
| Guided To-Dos | ch25 / 02_guided_todos.ipynb | ![]() |
| Coding Exam | ch25 / 03_coding_exam.ipynb | ![]() |
Learning Objectives
By the end of this chapter, you will be able to:
- Diagnose why raw clinical units (
age_years,height_cm,temp_c) distort Euclidean distances and prevent variance domination usingStandardScaler. - Implement iterative K-Means clustering from scratch in pure NumPy (deterministic seeding, vectorized assignment, empty-cluster-safe centroid updates, and SSE inertia) and explain why
n_init=10multi-startk-means++escapes poor local minima. - Select the number of clusters \(k\) transparently by pairing within-cluster inertia (Elbow method) with the Silhouette score (
sklearn.metrics.silhouette_score). - Compare Hierarchical Agglomerative Clustering (
linkage="ward") and DBSCAN density clustering (eps,min_samples, noise label-1) for phenotype discovery and clinical anomaly flagging. - Derive and implement Principal Component Analysis (PCA) directly via centered Singular Value Decomposition (
np.linalg.svd), verify exact agreement withsklearn.decomposition.PCA, interpret feature loadings, and audit compression viainverse_transformreconstruction MSE. - Critically evaluate t-SNE / UMAP nonlinear embeddings so you never over-interpret distorted global distances, cluster sizes, or perplexity-dependent layouts.
25.1 Stage 1 — First-Principles Intuition: Learning Structure Without Labels
In Chapters 21–24, every model learned a mapping \(\hat{y} = f(X)\) guided by ground-truth labels—whether a patient was anemic, needed urgent referral, or had elevated blood pressure. Yet in real clinical, genomic, and public health research, labels are often missing, incomplete, or not yet defined:
- Patient Phenotyping: Do 120 patients arriving at Upazila Health Complexes naturally fall into distinct clinical profiles (e.g., pediatric febrile cases vs. older hypertensive adults) before anyone assigns a diagnosis code?
- Health-System Targeting: Which Bangladesh districts share similar burdens of fever, blood pressure, anemia, and non-routine triage load so health ministries can group interventions?
- Biomarker Compression: When seven physiological measurements (
age_years,weight_kg,height_cm,temp_c,systolic_bp,hemoglobin_g_dl,fever_days) are strongly correlated, how many independent biological axes actually drive 85%–90% of the variation?
That is the domain of unsupervised learning: discovering latent geometry directly from \(X\) alone. Four first principles govern every method in this chapter:
- Distance Depends Completely on Feature Scale: Euclidean distance sums squared differences across all features, \(\|x - z\|_2^2 = \sum_j (x_j - z_j)^2\). In
clinic_triage,age_yearshas variance451.44whiletemp_chas variance0.60—a 752x variance gap! WithoutStandardScaler, clustering and PCA effectively ignore body temperature and hemoglobin. - No Universal “Accuracy” Metric: Without ground-truth \(y\), we evaluate cluster structure using geometric cohesion and separation—inertia (within-cluster sum of squared errors) and Silhouette score \(s \in [-1, +1]\)—always backed by clinical interpretability.
- Different Algorithms Assume Different Shapes: K-Means and Ward assume compact, roughly spherical clusters of comparable spread and force every point into a cluster. DBSCAN traces arbitrary dense regions and explicitly isolates sparse anomalies as label
-1. - PCA Maximizes Variance & Minimizes Reconstruction Error: Projecting standardized data onto the top \(k\) orthogonal principal components preserves the maximum possible linear variance and yields the lowest possible mean squared reconstruction error—and remarkably, on standardized data, \(\text{MSE} = 1 - \text{cumulative explained variance}\)!
Never run K-Means blindly on every unlabeled table. Ask two structural questions first:
- Are you grouping rows (patients/districts) or compressing columns (correlated features)? Row grouping calls for Clustering; column compression and noise reduction call for PCA.
- If clustering, do you expect compact spherical groups, a nested hierarchy, or dense blobs mixed with outliers? Spherical \(\rightarrow\) K-Means; nested tree \(\rightarrow\) Ward Hierarchical; irregular shapes + anomalies \(\rightarrow\) DBSCAN.
25.2 Stage 2 — Visual Mental Model: Unsupervised Method Selector
Use this decision map whenever you approach an unlabeled dataset \(X\):
- Counting DBSCAN Noise (
-1) as a Cluster:DBSCANassigns sparse outliers to label-1. Computinglen(set(labels))falsely counts noise as an extra cluster—always subtract{-1}before reportingn_clusters! - Clustering Directly on t-SNE or UMAP Coordinates: t-SNE and UMAP warp global distances and cluster diameters to preserve local neighborhoods. Never feed t-SNE coordinates into K-Means or claim two distant t-SNE islands are quantitatively “farther apart” than two close ones.
25.3 Stage 3 — Line-by-Line Code Walkthrough & Error Anatomy
Open part_05_classical_ml/ch25_unsupervised_clustering_pca/01_walkthrough.ipynb in Colab and execute every cell top-to-bottom alongside the dissections below.
25.3.1 Setup: Loading clinic_triage Feature Matrix
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans, AgglomerativeClustering, DBSCAN
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn.metrics import silhouette_score
import sharukh_tutor as tutor
clinic = tutor.load_data("clinic_triage", as_pandas=True)
feat_cols = [
"age_years", "weight_kg", "height_cm",
"temp_c", "systolic_bp", "hemoglobin_g_dl", "fever_days",
]
X_raw = clinic[feat_cols].to_numpy(dtype=float)
print("Loaded clinic_triage:", clinic.shape)
print("Feature matrix shape:", X_raw.shape)Expected Output:
We extract the 7 continuous physiological and anthropometric measurements into a clean (120, 7) float matrix X_raw, intentionally excluding patient_id, district, sex, and triage_priority so our algorithms discover structure purely from vitals.
25.3.2 Why Feature Scaling Dominates Euclidean Distance
# Inspect raw feature variances across incompatible clinical units
raw_vars = pd.Series(X_raw.var(axis=0), index=feat_cols).round(2)
print("Raw feature variances:\n" + raw_vars.to_string())
print(f"Variance ratio (age / temp): "
f"{raw_vars['age_years'] / raw_vars['temp_c']:.1f}x")
X_scaled = StandardScaler().fit_transform(X_raw)
km_raw = KMeans(n_clusters=3, n_init=10, random_state=42)
km_scl = KMeans(n_clusters=3, n_init=10, random_state=42)
lbl_raw = km_raw.fit_predict(X_raw)
lbl_scl = km_scl.fit_predict(X_scaled)
sil_raw_in_scaled = silhouette_score(X_scaled, lbl_raw)
sil_scl_in_scaled = silhouette_score(X_scaled, lbl_scl)
print(f"Unscaled K-Means silhouette (scaled space): "
f"{sil_raw_in_scaled:.4f}")
print(f"Scaled K-Means silhouette (scaled space): "
f"{sil_scl_in_scaled:.4f}")Expected Output:
Look at that dramatic 752.4x variance imbalance between age_years (451.44) and temp_c (0.60). When K-Means clusters X_raw directly, it splits patients almost exclusively by age, height, blood pressure, and weight—ignoring fever and anemia—yielding a weak silhouette of 0.1791 in standardized clinical space. Applying StandardScaler first lifts silhouette quality by +57% to 0.2811!
25.3.3 K-Means From Scratch in Pure NumPy
K-Means minimizes within-cluster inertia (sum of squared Euclidean distances to assigned centroids \(\mu_c\)):
\[J = \sum_{i=1}^{n} \left\| x_i - \mu_{c(i)} \right\|_2^2\]
It alternates two steps until centroids stop moving: (1) assign each point \(x_i\) to its nearest centroid, and (2) replace each centroid \(\mu_c\) with the mean of points assigned to cluster \(c\).
def kmeans_scratch_demo(X, k, max_iter=50, seed=42):
X_arr = np.asarray(X, dtype=float)
rng = np.random.default_rng(seed)
idx = rng.choice(len(X_arr), size=k, replace=False)
centroids = X_arr[idx].copy()
for _ in range(max_iter):
dists = ((X_arr[:, None, :] - centroids[None, :, :]) ** 2)
labels = np.argmin(dists.sum(axis=2), axis=1).astype(int)
new_c = centroids.copy()
for cid in range(k):
mask = labels == cid
if np.any(mask):
new_c[cid] = X_arr[mask].mean(axis=0)
if np.allclose(new_c, centroids):
centroids = new_c
break
centroids = new_c
dists = ((X_arr[:, None, :] - centroids[None, :, :]) ** 2)
labels = np.argmin(dists.sum(axis=2), axis=1).astype(int)
inertia = float(np.sum((X_arr - centroids[labels]) ** 2))
return labels, centroids, inertia
sc_labels, sc_centers, sc_inertia = kmeans_scratch_demo(
X_scaled, k=3, max_iter=50, seed=42
)
print(f"Scratch K-Means inertia: {sc_inertia:.4f}")
print(f"Sklearn K-Means inertia: {km_scl.inertia_:.4f}")
print("Scratch cluster sizes:",
{int(c): int(n) for c, n in zip(*np.unique(
sc_labels, return_counts=True))})Expected Output:
Pause and appreciate two key lessons in this output:
- Vectorized Broadcasting (
(n, 1, d) - (1, k, d)): One line computes every point-to-centroid squared distance in pure NumPy without a slow Python loop over patients! - Why Single-Start Inertia (
549.5317) > Sklearn Inertia (514.0598): K-Means is non-convex—a single random initialization can converge to a suboptimal local minimum. Scikit-Learn usesk-means++smart seeding acrossn_init=10independent restarts and keeps the lowest-inertia solution (514.0598).
25.3.4 Choosing \(k\): Elbow Method & Silhouette Score
For each sample \(i\), the Silhouette coefficient compares mean intra-cluster distance \(a(i)\) against mean nearest-other-cluster distance \(b(i)\):
\[s(i) = \frac{b(i) - a(i)}{\max(a(i), b(i))} \in [-1, +1]\]
Values near +1 signal tight, well-separated clusters; values near 0 indicate overlapping borders; negative values indicate misassigned points.
sweep_rows = []
for k in range(2, 7):
km = KMeans(n_clusters=k, n_init=10, random_state=42)
lbl = km.fit_predict(X_scaled)
sweep_rows.append({
"k": k,
"inertia": round(float(km.inertia_), 4),
"silhouette": round(float(silhouette_score(X_scaled, lbl)), 4),
})
k_table = pd.DataFrame(sweep_rows)
print(k_table.to_string(index=False))
best_row = k_table.loc[k_table["silhouette"].idxmax()]
print(f"Peak silhouette at k={int(best_row['k'])} "
f"({best_row['silhouette']:.4f})")Expected Output:
Notice the classic pattern: inertia drops monotonically (663.08 \(\rightarrow\) 331.97) because adding more centroids always shrinks within-cluster distance, eventually reaching 0 when \(k = n\). Pairing inertia with Silhouette reveals that \(k = 2\) achieves the cleanest geometric separation (0.3672), with \(k = 3\) (0.2811) offering a plausible finer split if clinically justified.
25.3.5 Hierarchical Agglomerative Clustering (Ward Linkage)
Agglomerative clustering starts with every patient as a singleton cluster and iteratively merges the two clusters that minimize Ward’s criterion—the exact increase in total within-cluster squared variance:
ward = AgglomerativeClustering(n_clusters=3, linkage="ward")
ward_labels = ward.fit_predict(X_scaled)
ward_sil = silhouette_score(X_scaled, ward_labels)
ward_sizes = {
int(c): int(n)
for c, n in zip(*np.unique(ward_labels, return_counts=True))
}
print(f"Ward (k=3) silhouette: {ward_sil:.4f}")
print("Ward (k=3) cluster sizes:", ward_sizes)Expected Output:
Ward clustering is 100% deterministic (zero random seed sensitivity) and achieves a slightly higher silhouette (0.2847) than K-Means (0.2811) at \(k = 3\), isolating a tight 7-patient subgroup (cluster 2) alongside two broader clinical groups (89 and 24).
25.3.6 DBSCAN: Density Clustering & Anomaly Flagging
DBSCAN (Density-Based Spatial Clustering of Applications with Noise) defines dense neighborhoods using two intuitive hyperparameters: radius eps and minimum neighbor count min_samples. Points in sparse regions are never forced into a cluster—they receive label -1 (noise / anomaly):
db = DBSCAN(eps=1.8, min_samples=5)
db_labels = db.fit_predict(X_scaled)
n_clusters = int(len(set(db_labels.tolist()) - {-1}))
n_noise = int(np.sum(db_labels == -1))
print(f"DBSCAN (eps=1.8, min_samples=5): "
f"{n_clusters} clusters, {n_noise} noise outliers")
outlier_preview = clinic.loc[
db_labels == -1, ["patient_id", "age_years", "temp_c", "systolic_bp"]
].head(3)
print("Sample flagged clinical outliers:\n",
outlier_preview.to_string(index=False))Expected Output:
Look at who DBSCAN flags automatically without any hand-coded thresholds: UHC-0008 (10-year-old with high fever 38.5 C), UHC-0021 (8-year-old with unusually elevated 129 mmHg systolic BP), and UHC-0023 (55-year-old with unusually low 102 mmHg BP and 36.1 C temperature). In clinical data quality audits, DBSCAN -1 labels serve as an automatic shortlist for chart review!
25.3.7 PCA From Scratch via NumPy SVD vs. Scikit-Learn
Given a mean-centered matrix \(X_c = X - \bar{X} \in \mathbb{R}^{n \times p}\), its thin Singular Value Decomposition (SVD) factorizes \(X_c = U \Sigma V^\top\). The rows of \(V^\top\) are the orthogonal principal component axes (loadings), the projected coordinates are \(Z_k = X_c V_k\), and the explained variance ratio of component \(j\) is simply:
\[\text{EVR}_j = \frac{\sigma_j^2}{\sum_{m=1}^{r} \sigma_m^2}\]
# 1. PCA from scratch via centered SVD
X_c = X_scaled - X_scaled.mean(axis=0)
U, S, Vt = np.linalg.svd(X_c, full_matrices=False)
Z_svd = X_c @ Vt[:2].T
evr_svd = ((S ** 2) / np.sum(S ** 2))[:2]
# 2. Compare directly against sklearn.decomposition.PCA
pca_full = PCA().fit(X_scaled)
cum_var = np.cumsum(pca_full.explained_variance_ratio_)
print("SVD EVR (top 2): ", np.round(evr_svd, 4))
print("Sklearn EVR (top 2):",
np.round(pca_full.explained_variance_ratio_[:2], 4))
print("Cumulative variance:", np.round(cum_var, 4))
k_85 = int(np.argmax(cum_var >= 0.85) + 1)
print(f"PCs required for >=85% variance: {k_85} "
f"(retains {cum_var[k_85 - 1]:.4f})")
# 3. Interpret clinical loadings on PC1 & PC2
loadings = pd.DataFrame(
pca_full.components_[:2].T,
index=feat_cols,
columns=["PC1", "PC2"],
).round(4)
print("Top-2 PC Clinical Loadings:\n", loadings)Expected Output:
SVD EVR (top 2): [0.3051 0.2519]
Sklearn EVR (top 2): [0.3051 0.2519]
Cumulative variance: [0.3051 0.557 0.7325 0.8621 0.9272 0.9692 1. ]
PCs required for >=85% variance: 4 (retains 0.8621)
Top-2 PC Clinical Loadings:
PC1 PC2
age_years 0.4951 0.0179
weight_kg 0.4826 0.3182
height_cm 0.4902 0.3448
temp_c -0.1944 0.6084
systolic_bp 0.3863 -0.1301
hemoglobin_g_dl -0.1745 0.0237
fever_days -0.2536 0.6260Read those loadings like a clinician-scientist—they tell a crystal-clear biological story:
- Exact SVD Match: Our 4-line NumPy SVD implementation reproduces Scikit-Learn’s
explained_variance_ratio_([0.3051, 0.2519]) to machine precision! - PC1 (30.5% variance — Anthropometric / Age-Maturity Axis): Driven by
age_years(+0.4951),height_cm(+0.4902),weight_kg(+0.4826), andsystolic_bp(+0.3863). - PC2 (25.2% variance — Acute Febrile Illness Axis): Driven almost exclusively by
fever_days(+0.6260) andtemp_c(+0.6084). Together, just 2 orthogonal axes explain 55.7% of all 7 vitals, and 4 PCs capture 86.21%!
25.3.8 Nonlinear Projections (t-SNE / UMAP) Caveats
When visualizing high-dimensional datasets (e.g., single-cell RNA-seq or multi-biomarker panels), researchers frequently reach for t-SNE or UMAP. Unlike PCA, these methods optimize a nonlinear objective preserving local neighbor probabilities—which comes with strict scientific caveats:
# t-SNE preserves local neighborhoods, NOT global distances or variance
for perp in [10, 30]:
emb = TSNE(
n_components=2, perplexity=perp, init="pca",
learning_rate="auto", random_state=42,
).fit_transform(X_scaled)
spread = [round(float(v), 2) for v in emb.std(axis=0)]
print(f"t-SNE (perplexity={perp:2d}) embedding shape={emb.shape}, "
f"axis SDs={spread}")Expected Output:
Changing a single hyperparameter (perplexity=10 vs. 30) shrinks the embedding coordinate spread by nearly 4x ([14.46, 16.49] vs. [3.22, 4.36])! Always remember three rules for publication figures: (1) t-SNE/UMAP axes have no feature loadings or explained variance, (2) cluster sizes and inter-cluster gaps in a 2D t-SNE plot are not faithful Euclidean distances, and (3) always perform clustering and statistical tests in the standardized original (or PCA) space, using t-SNE/UMAP strictly for visual intuition.
25.3.9 Break It On Purpose! (Unsupervised Error Anatomy)
Python’s Traceback:
Diagnosis: The silhouette formula requires a nearest other cluster distance \(b(i)\). When every sample has the same label (common with \(k = 1\), or when DBSCAN labels every point as -1 noise or collapses everything into 1 giant blob), \(b(i)\) is undefined.
Fix: Always sweep \(k \ge 2\) and guard DBSCAN silhouette calls by checking len(set(labels) - {-1}) >= 2 before scoring.
n_components Exceeds Matrix Rank)
Python’s Traceback:
Diagnosis: A matrix with \(n = 120\) samples and \(p = 7\) features has at most \(\min(n, p) = 7\) orthogonal principal components. Requesting n_components=12 exceeds the mathematical rank limit.
Fix: Cap n_components <= min(X.shape)—or pass a float in (0, 1) (like PCA(n_components=0.85)) or compute k from PCA().fit(X_scaled) dynamically.
eps)
Python’s Traceback:
Diagnosis: eps defines a Euclidean neighborhood radius, which must be strictly positive (eps > 0). In Scikit-Learn, InvalidParameterError is a subclass of ValueError raised during estimator parameter validation.
Fix: Ensure eps > 0 (typically chosen via a \(k\)-nearest-neighbor distance “knee” plot in standardized space, such as eps=1.8 on 7 standardized features).
25.4 Stage 4 — The Researcher’s Problem-Structuring Blueprint
Scenario: Your health-informatics team wants a single reusable function run_clinical_phenotyping_pipeline(raw_df, feat_cols, k=3, var_target=0.85, eps=1.8, seed=42) that takes clinic_triage (without mutating it) and outputs a complete unsupervised audit dictionary: (1) minimum PCs required to retain var_target variance plus inverse_transform reconstruction MSE, (2) side-by-side silhouette comparison of K-Means vs. Ward Hierarchical at \(k\) clusters, and (3) total DBSCAN anomaly count (labels == -1).
25.4.1 Blueprint (Completed on Paper Before Coding)
Preprocessing Layer (Scale First, Never Mutate):
- Slice
raw_df[feat_cols].to_numpy(dtype=float)and standardize viaX_sc = StandardScaler().fit_transform(X).
- Slice
Three Unsupervised Audit Modules:
- PCA Compression: Fit full
PCA().fit(X_sc), findn_pcs = int(np.argmax(cum >= var_target) + 1), refitPCA(n_components=n_pcs), computeX_hat = pca_k.inverse_transform(pca_k.fit_transform(X_sc)), and evaluaterecon_mse = np.mean((X_sc - X_hat) ** 2). - Partition Comparison: Fit
KMeans(n_clusters=k, n_init=10, random_state=seed)andAgglomerativeClustering(n_clusters=k, linkage="ward")onX_scand score both withsilhouette_score. - Anomaly Screen: Fit
DBSCAN(eps=eps, min_samples=5)onX_scand countint(np.sum(db_lbl == -1)).
- PCA Compression: Fit full
Verification Plan:
- On
clinic_triage(var_target=0.85),n_pcsmust be4,cum_variance = 0.8621, andrecon_mse = 0.13789(verifying0.86211 + 0.13789 = 1.00000!). ward_sil(0.2847) should slightly beatkmeans_sil(0.2811), anddbscan_noisemust flag14clinical outliers.
- On
25.4.2 Clean Python Implementation (Verbatim in 01_walkthrough.ipynb)
def run_clinical_phenotyping_pipeline(
raw_df, feat_cols, k=3, var_target=0.85, eps=1.8, seed=42
):
"""Scale vitals, audit PCA compression, and compare clusterers."""
X = raw_df[feat_cols].to_numpy(dtype=float)
X_sc = StandardScaler().fit_transform(X)
# 1. PCA compression & reconstruction MSE audit
full_pca = PCA().fit(X_sc)
cum = np.cumsum(full_pca.explained_variance_ratio_)
n_pcs = int(np.argmax(cum >= var_target) + 1)
pca_k = PCA(n_components=n_pcs)
X_hat = pca_k.inverse_transform(pca_k.fit_transform(X_sc))
recon_mse = round(float(np.mean((X_sc - X_hat) ** 2)), 5)
# 2. Compare K-Means vs. Ward Agglomerative at k clusters
km_lbl = KMeans(
n_clusters=k, n_init=10, random_state=seed
).fit_predict(X_sc)
ward_lbl = AgglomerativeClustering(
n_clusters=k, linkage="ward"
).fit_predict(X_sc)
# 3. Flag density anomalies via DBSCAN
db_lbl = DBSCAN(eps=eps, min_samples=5).fit_predict(X_sc)
return {
"n_pcs": n_pcs,
"cum_variance": round(float(cum[n_pcs - 1]), 4),
"recon_mse": recon_mse,
"kmeans_sil": round(float(silhouette_score(X_sc, km_lbl)), 4),
"ward_sil": round(float(silhouette_score(X_sc, ward_lbl)), 4),
"dbscan_noise": int(np.sum(db_lbl == -1)),
}
audit = run_clinical_phenotyping_pipeline(clinic, feat_cols)
for key, val in audit.items():
print(f"{key:14s}: {val}")Output:
25.5 Stage 5 — Guided “To-Do” Practice Labs
Open part_05_classical_ml/ch25_unsupervised_clustering_pca/02_guided_todos.ipynb in Google Colab, run Step 0 to activate sharukh-tutor, and implement all six reusable unsupervised learning functions below. Pass each function name (without parentheses!) to tutor.check(...).
- To-Do 25.1 (
kmeans_from_scratch(X, k, max_iter=50, seed=42)): Pure-NumPy iterative K-Means seeded vianp.random.default_rng(seed)(replace=False), alternating nearest-centroid assignment and empty-safe mean updates until convergence, returning(labels_1d_int, centroids_2d, float_inertia). - To-Do 25.2 (
evaluate_k_range(X_scaled, k_values, seed=42)): Sweepk_valueswithKMeans(n_clusters=k, n_init=10, random_state=seed)and return a DataFrame with columns["k", "inertia", "silhouette"]rounded to 4 decimals. - To-Do 25.3 (
fit_agglomerative_ward(X_scaled, n_clusters=3)): FitAgglomerativeClustering(n_clusters=n_clusters, linkage="ward")and return{"labels": labels, "silhouette": round4, "cluster_sizes": dict_int_to_int}. - To-Do 25.4 (
detect_dbscan_anomalies(X_scaled, eps=0.8, min_samples=5)): FitDBSCAN(eps=eps, min_samples=min_samples)and return{"n_clusters": int_excluding_minus1, "n_noise": int, "labels": labels}. - To-Do 25.5 (
pca_from_scratch_svd(X, n_components=2)): Mean-centerXby subtracting column means, computenp.linalg.svd(X_c, full_matrices=False), and return(X_projected, explained_variance_ratio)of shapes(n, n_components)and(n_components,). - To-Do 25.6 (
components_for_variance_target(X_scaled, target_var=0.90)): Fit fullPCA()and return the smallest Pythonintkwherenp.cumsum(explained_variance_ratio_)[k - 1] >= target_var.
25.6 Stage 6 — Concept & Execution Trace Test (No Computer Allowed!)
Work through all 10 unsupervised learning and linear-algebra reasoning questions on paper before checking Stage 8:
- Variance Domination: Feature A has standard deviation
100(variance10,000) and Feature B has standard deviation1(variance1). If you run K-Means withoutStandardScaler, roughly what percentage of the total squared Euclidean distance comes from Feature A? - K-Means Inertia Monotonicity: Why can’t you simply pick the \(k\) with the lowest K-Means inertia? What happens to inertia when \(k = n\) (one cluster per patient)?
- Silhouette Sign Interpretation: A patient has average distance
a(i) = 1.8to points in their own cluster and average distanceb(i) = 1.2to the nearest neighboring cluster. Compute their exact Silhouette score \(s(i)\) and explain what its sign means. - Local Minima &
n_init: In Section 25.3.2, our single-start pure-NumPy K-Means achieved inertia549.53while Scikit-Learn’sKMeans(n_init=10)achieved514.06. Why did both converge yet reach different inertias? - K-Means vs. Ward Determinism: If you run
AgglomerativeClustering(n_clusters=3, linkage="ward")ten times without setting anyrandom_state, will the clusters change between runs? Why or why not? - DBSCAN Label Count Trap: Suppose
DBSCAN(eps=0.5, min_samples=5).fit_predict(X)returnslabels = np.array([-1, 0, 0, -1, 1, 1, 1, -1]). What are the truen_clustersandn_noise, and what bug occurs if you computelen(set(labels))? - Mean-Centering Before SVD: What goes wrong geometrically if you run
np.linalg.svd(X)directly on a dataset whose column means are[50, 120, 37]instead of subtractingX.mean(axis=0)first? - Singular Values to Explained Variance: A centered dataset produces singular values \(S = [6, 3, 1]\). Compute the exact explained variance ratio of PC1, PC2, and PC3 by hand (as fractions or decimals).
- Standardized Reconstruction MSE Identity: After standardizing 10 clinical features, you keep \(k = 4\) principal components whose cumulative explained variance ratio is
0.8200. Without running Python, what is the exact mean squared reconstruction errornp.mean((X_scaled - X_hat) ** 2)? - t-SNE Scientific Caveat: A colleague runs 2D
TSNEon gene expression data, runs K-Means on the 2D t-SNE coordinates, and claims “Cluster A is 3x wider than Cluster B, so Cluster A is biologically more heterogeneous.” Identify the two methodological errors in that claim.
25.7 Stage 7 — Chapter 25 Coding Exam (100 Points)
Open part_05_classical_ml/ch25_unsupervised_clustering_pca/03_coding_exam.ipynb, implement all three functions, and run tutor.check_exam("ch25", exam_submissions) to earn your Chapter 25 score out of 100 points.
25.7.1 Exam Task 25.1 (30 Points): select_optimal_k_silhouette(X, k_range, seed=42)
Without mutating X:
- Standardize
XviaStandardScaler().fit_transform(X). - Sweep every
kink_rangeusingKMeans(n_clusters=k, n_init=10, random_state=seed)and buildtableas apd.DataFramewith columns["k", "inertia", "silhouette"]rounded to 4 decimals. - Return
{"best_k": int, "best_silhouette": round4, "table": table}picking thekwith the highest silhouette score.
25.7.2 Exam Task 25.2 (35 Points): district_risk_phenotyping(clinic_df, n_clusters=3, seed=42)
Without mutating clinic_df:
- Aggregate
clinic_dfperdistrict(groupby("district", as_index=False)) computingmean_temp(temp_c),mean_sbp(systolic_bp),mean_hb(hemoglobin_g_dl), andurgent_rate((triage_priority != "Routine").mean()). - Standardize those 4 aggregate indicators with
StandardScaler()and cluster the districts usingKMeans(n_clusters=n_clusters, n_init=10, random_state=seed). - Add integer
"cluster"labels to the aggregated DataFrame and return{"district_clusters": df, "silhouette": round4}.
25.7.3 Exam Task 25.3 (35 Points): pca_clinical_compression_audit(X, variance_threshold=0.85)
Without mutating X:
- Standardize
XviaStandardScaler().fit_transform(X)and fit fullPCA()to find the smallest integerkwherenp.cumsum(explained_variance_ratio_)[k - 1] >= variance_threshold. - Fit
PCA(n_components=k)onX_scaled, projectZ = pca_k.fit_transform(X_scaled), and reconstructX_hat = pca_k.inverse_transform(Z). - Return
{"n_components": int, "cumulative_variance": round4, "top_pc1_feature_idx": int, "reconstruction_mse": round5}wheretop_pc1_feature_idxusesnp.abs(pca_k.components_[0])andreconstruction_mseisnp.mean((X_scaled - X_hat) ** 2).
25.8 Stage 8 — Complete Annotated Solutions & Debugging Post-Mortem
25.8.1 A. Concept & Execution Trace Test Key
- Roughly 99.99% (\(10{,}000 / 10{,}001 \approx 99.99\%\)) of squared Euclidean distance comes from Feature A, completely drowning out Feature B until both are standardized.
- Inertia strictly decreases as \(k\) grows; at \(k = n\), every point sits at its own centroid so inertia equals
0.0—a useless overfitted partition! - \(s(i) = (1.2 - 1.8) / \max(1.8, 1.2) = -0.6 / 1.8 = \mathbf{-0.3333}\). A negative silhouette means point \(i\) is closer on average to a neighboring cluster than to its own cluster (likely misassigned).
- K-Means optimizes a non-convex objective with multiple local minima. One random draw can trap centroids in a higher-inertia local basin (
549.53), whereasn_init=10withk-means++explores 10 smart starts and selects the lowest-inertia basin (514.06). - No—Ward agglomerative clustering is 100% deterministic because it merges pairs strictly by minimum variance increase from the full pairwise distance matrix (no random centroid seeding).
- True
n_clusters = 2(labels0and1) andn_noise = 3(the three-1entries). Callinglen(set(labels))returns3, erroneously counting-1noise as a third cluster. - Without mean-centering, the first singular vector points from the origin
(0, 0, 0)straight toward the data cloud’s mean vector[50, 120, 37], inflating PC1’s variance ratio instead of capturing covariance around the center. - Squared singular values are \([36, 9, 1]\), summing to \(46\). Therefore: \(\text{EVR}_1 = 36/46 \approx \mathbf{0.7826}\), \(\text{EVR}_2 = 9/46 \approx \mathbf{0.1957}\), and \(\text{EVR}_3 = 1/46 \approx \mathbf{0.0217}\).
- On standardized features (unit variance per column), reconstruction MSE equals \(1 - 0.8200 = \mathbf{0.18000}\)!
- First, t-SNE distorts cluster sizes and global distances to equalize local densities—visual spread on a t-SNE plot does not measure biological variance. Second, running K-Means on 2D t-SNE axes clusters a distorted nonlinear visualization rather than the true standardized feature or PCA space.
25.8.2 B. Annotated Solutions — Guided To-Dos (Mirrored in 02_guided_todos_SOLUTION.ipynb)
import numpy as np
import pandas as pd
from sklearn.cluster import KMeans, AgglomerativeClustering, DBSCAN
from sklearn.decomposition import PCA
from sklearn.metrics import silhouette_score
# To-Do 25.1
def kmeans_from_scratch(X, k, max_iter=50, seed=42):
"""Run pure-NumPy K-Means and return (labels, centroids, inertia)."""
X_arr = np.asarray(X, dtype=float)
rng = np.random.default_rng(seed)
init_idx = rng.choice(len(X_arr), size=k, replace=False)
centroids = X_arr[init_idx].copy()
for _ in range(max_iter):
dists = ((X_arr[:, None, :] - centroids[None, :, :]) ** 2)
labels = np.argmin(dists.sum(axis=2), axis=1).astype(int)
new_c = centroids.copy()
for cid in range(k):
mask = labels == cid
if np.any(mask):
new_c[cid] = X_arr[mask].mean(axis=0)
if np.allclose(new_c, centroids):
centroids = new_c
break
centroids = new_c
dists = ((X_arr[:, None, :] - centroids[None, :, :]) ** 2)
labels = np.argmin(dists.sum(axis=2), axis=1).astype(int)
inertia = float(np.sum((X_arr - centroids[labels]) ** 2))
return labels, centroids, inertia
# To-Do 25.2
def evaluate_k_range(X_scaled, k_values, seed=42):
"""Sweep k_values with KMeans and return inertia/silhouette table."""
rows = []
for k in k_values:
km = KMeans(n_clusters=k, n_init=10, random_state=seed)
labels = km.fit_predict(X_scaled)
sil = silhouette_score(X_scaled, labels)
rows.append({
"k": int(k),
"inertia": round(float(km.inertia_), 4),
"silhouette": round(float(sil), 4),
})
return pd.DataFrame(rows)
# To-Do 25.3
def fit_agglomerative_ward(X_scaled, n_clusters=3):
"""Fit Ward agglomerative clustering and summarize silhouette/sizes."""
model = AgglomerativeClustering(
n_clusters=n_clusters, linkage="ward"
)
labels = model.fit_predict(X_scaled)
unique, counts = np.unique(labels, return_counts=True)
sizes = {int(c): int(n) for c, n in zip(unique, counts)}
sil = round(float(silhouette_score(X_scaled, labels)), 4)
return {
"labels": labels,
"silhouette": sil,
"cluster_sizes": sizes,
}
# To-Do 25.4
def detect_dbscan_anomalies(X_scaled, eps=0.8, min_samples=5):
"""Run DBSCAN and separate dense cluster count from -1 noise count."""
model = DBSCAN(eps=eps, min_samples=min_samples)
labels = model.fit_predict(X_scaled)
n_clusters = int(len(set(labels.tolist()) - {-1}))
n_noise = int(np.sum(labels == -1))
return {
"n_clusters": n_clusters,
"n_noise": n_noise,
"labels": labels,
}
# To-Do 25.5
def pca_from_scratch_svd(X, n_components=2):
"""Center X, run SVD, and return (X_projected, explained_var_ratio)."""
X_arr = np.asarray(X, dtype=float)
X_c = X_arr - X_arr.mean(axis=0)
_, S, Vt = np.linalg.svd(X_c, full_matrices=False)
X_proj = X_c @ Vt[:n_components].T
evr = (S ** 2) / np.sum(S ** 2)
return X_proj, evr[:n_components]
# To-Do 25.6
def components_for_variance_target(X_scaled, target_var=0.90):
"""Return smallest int k reaching target_var cumulative variance."""
pca = PCA().fit(X_scaled)
cum_var = np.cumsum(pca.explained_variance_ratio_)
return int(np.argmax(cum_var >= target_var) + 1)25.8.3 C. Annotated Solutions — Chapter 25 Coding Exam (Mirrored in 03_coding_exam_SOLUTION.ipynb)
from sklearn.preprocessing import StandardScaler
# Exam Task 25.1 (30 pts)
def select_optimal_k_silhouette(X, k_range, seed=42):
"""Standardize X, sweep k_range, and pick k maximizing silhouette."""
X_sc = StandardScaler().fit_transform(X)
rows = []
for k in k_range:
km = KMeans(n_clusters=k, n_init=10, random_state=seed)
labels = km.fit_predict(X_sc)
sil = silhouette_score(X_sc, labels)
rows.append({
"k": int(k),
"inertia": round(float(km.inertia_), 4),
"silhouette": round(float(sil), 4),
})
table = pd.DataFrame(rows)
best = table.loc[table["silhouette"].idxmax()]
return {
"best_k": int(best["k"]),
"best_silhouette": round(float(best["silhouette"]), 4),
"table": table,
}
# Exam Task 25.2 (35 pts)
def district_risk_phenotyping(clinic_df, n_clusters=3, seed=42):
"""Aggregate district triage profiles, scale, and cluster."""
agg = clinic_df.groupby("district", as_index=False).agg(
mean_temp=("temp_c", "mean"),
mean_sbp=("systolic_bp", "mean"),
mean_hb=("hemoglobin_g_dl", "mean"),
urgent_rate=(
"triage_priority", lambda s: (s != "Routine").mean()
),
)
cols = ["mean_temp", "mean_sbp", "mean_hb", "urgent_rate"]
X_sc = StandardScaler().fit_transform(agg[cols].to_numpy(dtype=float))
km = KMeans(n_clusters=n_clusters, n_init=10, random_state=seed)
labels = km.fit_predict(X_sc).astype(int)
agg["cluster"] = labels
sil = round(float(silhouette_score(X_sc, labels)), 4)
return {
"district_clusters": agg,
"silhouette": sil,
}
# Exam Task 25.3 (35 pts)
def pca_clinical_compression_audit(X, variance_threshold=0.85):
"""Scale X, pick PCs for variance_threshold, and audit recon MSE."""
X_sc = StandardScaler().fit_transform(X)
full_pca = PCA().fit(X_sc)
cum = np.cumsum(full_pca.explained_variance_ratio_)
k = int(np.argmax(cum >= variance_threshold) + 1)
pca_k = PCA(n_components=k)
Z = pca_k.fit_transform(X_sc)
X_hat = pca_k.inverse_transform(Z)
cum_var = round(float(np.sum(pca_k.explained_variance_ratio_)), 4)
top_idx = int(np.argmax(np.abs(pca_k.components_[0])))
mse = round(float(np.mean((X_sc - X_hat) ** 2)), 5)
return {
"n_components": k,
"cumulative_variance": cum_var,
"top_pc1_feature_idx": top_idx,
"reconstruction_mse": mse,
}25.8.4 D. Debugging Post-Mortem: Top 3 Chapter 25 Failure Modes
- Running Euclidean Clustering or PCA on Unscaled Units: Leaving raw features unstandardized lets large-unit variables (
age_yearsvariance451.44vs.temp_cvariance0.60) monopolize both cluster boundaries and PC1 loadings. Always applyStandardScaler()before K-Means, Ward, DBSCAN, or PCA unless all features share identical physical units. - Miscounting DBSCAN
-1Noise or Crashingsilhouette_score: Forgetting- {-1}turns DBSCAN outliers into a phantom cluster, and callingsilhouette_scorewhen only one cluster exists immediately raisesValueError. Always separate noise counts first and verify at least two clusters exist before scoring. - Forgetting Mean-Centering or
np.abs(...)in PCA Analysis: Runningnp.linalg.svdwithout subtracting column means points PC1 toward the global mean rather than maximum variance, while takingnp.argmax(pca.components_[0])withoutnp.abs(...)misses strong negative loadings because PCA eigenvector signs are arbitrary.


