Unsupervised Learning I: Clustering (K-Means++, Hierarchical, DBSCAN & GMM)
Lloyd's algorithm & K-Means++ init, Elbow & Silhouette evaluation, Agglomerative dendrograms, density-based DBSCAN/HDBSCAN, and soft Gaussian Mixture Models (EM).
Clustering algorithms (K-Means++, Hierarchical, DBSCAN, GMM) automatically group unlabeled data points into natural segments based on similarity or density.
When you have no ground-truth labels, clustering uncovers customer personas, document topics, and irregular spatial patterns without human annotation.
When you have ZERO labels — customer segmentation, embedding topic discovery, and fraud rings — clustering reveals hidden structure!
K-Means powers vector quantization (IVF-PQ in FAISS) for billion-scale RAG search, while HDBSCAN on sentence embeddings is the backbone of BERTopic document clustering!
Knowing why raw K-Means destroys unscaled features and concentric shapes — and how Silhouette Score & BIC defend your choice of K to executives — is essential applied DS.
Classic interview prompt: 'Compare K-Means, DBSCAN, and GMM — when does each break, and how is K-Means a special case of EM for GMM?'
If you run K-Means on a dataset and plot Inertia (Within-Cluster Sum of Squares) for K = 1, 2, ..., N, what happens as K keeps increasing toward N?
- Trace Lloyd's K-Means Assign/Update steps and explain why K-Means++ (P ∝ D(x)²) avoids bad local minima
- Evaluate clusters without ground-truth labels using Inertia (Elbow), Silhouette Score, and BIC/AIC
- Explain why K-Means fails on non-spherical shapes & outliers — and when DBSCAN / HDBSCAN wins
- Contrast hard spherical K-Means with soft elliptical Gaussian Mixture Models (GMM) trained via EM
Clustering asks: 'If nobody gave us labels, how should we group these points?' K-Means drops K magnets and pulls spherical Voronoi bubbles around them; DBSCAN walks through dense crowds and ignores stragglers as noise; GMM fits soft overlapping elliptical bell curves using Expectation–Maximization!
1. K-Means, K-Means++ Initialization & Silhouette Evaluation
Randomly dropping initial centroids can trap K-Means in terrible local minima where two centroids share one cluster. K-Means++ fixes this cleanly: pick the first centroid randomly, then pick each subsequent centroid with probability proportional to squared distance from the nearest existing centroid!
Evaluating Clusters Without Labels
- Silhouette Score: , where is mean distance to your own cluster and is mean distance to the nearest rival cluster.
- Scale first! K-Means uses Euclidean distance, so always apply
StandardScaler(and remember K-Means is sensitive to extreme outliers because centroids are means!).
2. Density-Based Clustering (DBSCAN / HDBSCAN) vs. K-Means
K-Means assumes every cluster is a roughly equal-sized convex sphere. Give it two concentric rings or crescent moons and K-Means fails completely! Toggle Rings and switch between K-Means and DBSCAN below.
DBSCAN & Hierarchical Agglomerative Clustering
- DBSCAN: Discovers arbitrary shapes without specifying and labels isolated outliers as
-1. When clusters have varying densities, upgrade to HDBSCAN! - Hierarchical Agglomerative: Starts with singleton clusters and merges the closest pair (Single, Complete, Average, or Ward linkage) into a visual Dendrogram ( — great for ).
Compare K-Means vs. DBSCAN on Blobs & Concentric Rings
K-Means assumes convex spherical Voronoi cells — it brutally slices BOTH rings in half! Click Algo to switch to DBSCAN.
3. Gaussian Mixture Models (GMM) & Expectation–Maximization (EM)
What if clusters are stretched ellipses that overlap? GMM models data as a weighted sum of multivariate Gaussians , giving every point soft cluster probabilities!
K-Means as a Special Case of GMM (Interview Classic!)
Run EM for GMM with equal mixing weights , shared spherical covariance , and take the limit : soft responsibilities collapse to hard 0/1 nearest-centroid assignments — recovering Lloyd's K-Means identically! Pick GMM's using BIC (Bayesian Information Criterion).
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
Because K-Means' objective is non-convex and initialization is still randomized; running 10 restarts and keeping the lowest-inertia solution guarantees a much cleaner optimum.
Watch Centroids Glide to Cluster Means & Inertia Drop Monotonically
Alternating E-Step (Assign nearest centroid) → M-Step (Move centroid to mean)
RAG & Embedding Topic Discovery
UMAP + HDBSCAN (BERTopic) clusters millions of support tickets or documents without guessing K.
FAISS IVF Vector Quantization
K-Means partitions billion-vector embedding spaces into Voronoi cells for millisecond ANN lookup.
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans, DBSCAN
from sklearn.mixture import GaussianMixture
from sklearn.metrics import silhouette_score
X_sc = StandardScaler().fit_transform(X)
km = KMeans(n_clusters=4, init="k-means++", n_init="auto", random_state=42).fit(X_sc)
print("Silhouette:", round(silhouette_score(X_sc, km.labels_), 3))
# Soft elliptical clusters + BIC model selection
gmm = GaussianMixture(n_components=4, covariance_type="full", random_state=42).fit(X_sc)
print("GMM BIC:", round(gmm.bic(X_sc), 1))