Clustering Metrics and Model Selection
This demo takes a few seconds to load and runs entirely in your browser (NumPy, SciPy and matplotlib via Pyodide; no
tgmm or PyTorch involved).
Three Gaussian blobs, a k-means clustering with the \(K\) you choose, and every index of
ClusteringMetrics recomputed live with the same formulas. Move the
separation slider to watch the indices degrade as the blobs overlap, change \(K\) to see which indices are fooled, and
switch on noise (uniform background points). tgmm treats noise by dropping the points labelled as noise and scaling
the index by \(f=(n-n_{\text{noise}})/n\) (multiplied for higher-is-better, divided for lower-is-better indexes); the second
column below does that, the first lets k-means swallow the noise into clusters.
import%20marimo%20as%20mo%0Aimport%20numpy%20as%20np%0Aimport%20matplotlib.pyplot%20as%20plt%0A%0Adef%20blobs(sep%2C%20noise)%3A%0A%20%20%20%20rng%20%3D%20np.random.default_rng(0)%0A%20%20%20%20X%20%3D%20np.vstack(%5Brng.normal(c%2C%201.0%2C%20(80%2C%202))%20for%20c%20in%20np.array(%5B%5B0%2C%200%5D%2C%20%5B1%2C%200%5D%2C%20%5B0.5%2C%200.87%5D%5D)%20*%20sep%5D)%3B%20y%20%3D%20np.repeat(%5B0%2C%201%2C%202%5D%2C%2080)%0A%20%20%20%20if%20noise%3A%20%20%23%2030%20uniform%20background%20points%2C%20label%20-1%0A%20%20%20%20%20%20%20%20X%20%3D%20np.vstack(%5BX%2C%20rng.uniform(X.min(0)%20-%203%2C%20X.max(0)%20%2B%203%2C%20(30%2C%202))%5D)%3B%20y%20%3D%20np.r_%5By%2C%20-np.ones(30%2C%20int)%5D%0A%20%20%20%20return%20X%2C%20y%0A%0Adef%20kmeans(X%2C%20k)%3A%0A%20%20%20%20C%20%3D%20X%5Bnp.random.default_rng(1).choice(len(X)%2C%20k%2C%20replace%3DFalse)%5D%0A%20%20%20%20for%20_%20in%20range(50)%3A%0A%20%20%20%20%20%20%20%20lab%20%3D%20((X%5B%3A%2C%20None%5D%20-%20C%5BNone%5D)%20**%202).sum(-1).argmin(1)%0A%20%20%20%20%20%20%20%20C%20%3D%20np.stack(%5BX%5Blab%20%3D%3D%20j%5D.mean(0)%20if%20(lab%20%3D%3D%20j).any()%20else%20C%5Bj%5D%20for%20j%20in%20range(k)%5D)%0A%20%20%20%20return%20lab%0A%0Adef%20indices(X%2C%20lab%2C%20noise_label%3D-1)%3A%0A%20%20%20%20keep%20%3D%20lab%20!%3D%20noise_label%3B%20Xk%2C%20l%20%3D%20X%5Bkeep%5D%2C%20np.unique(lab%5Bkeep%5D%2C%20return_inverse%3DTrue)%5B1%5D%3B%20f%20%3D%20keep.mean()%3B%20K%20%3D%20l.max()%20%2B%201%0A%20%20%20%20D%20%3D%20np.linalg.norm(Xk%5B%3A%2C%20None%5D%20-%20Xk%5BNone%5D%2C%20axis%3D-1)%3B%20cnt%20%3D%20np.bincount(l%2C%20minlength%3DK)%3B%20i%20%3D%20np.arange(len(l))%0A%20%20%20%20sums%20%3D%20np.stack(%5BD%5B%3A%2C%20l%20%3D%3D%20j%5D.sum(1)%20for%20j%20in%20range(K)%5D%2C%201)%3B%20a%20%3D%20sums%5Bi%2C%20l%5D%20%2F%20np.maximum(cnt%5Bl%5D%20-%201%2C%201)%0A%20%20%20%20m%20%3D%20sums%20%2F%20cnt%3B%20m%5Bi%2C%20l%5D%20%3D%20np.inf%3B%20b%20%3D%20m.min(1)%0A%20%20%20%20sil%20%3D%20np.where(cnt%5Bl%5D%20%3C%3D%201%2C%200%2C%20(b%20-%20a)%20%2F%20np.maximum(a%2C%20b)).mean()%0A%20%20%20%20C%20%3D%20np.stack(%5BXk%5Bl%20%3D%3D%20j%5D.mean(0)%20for%20j%20in%20range(K)%5D)%3B%20own%20%3D%20np.linalg.norm(Xk%20-%20C%5Bl%5D%2C%20axis%3D1)%0A%20%20%20%20ssw%2C%20ssb%20%3D%20(own%20**%202).sum()%2C%20(cnt%20*%20((C%20-%20Xk.mean(0))%20**%202).sum(1)).sum()%0A%20%20%20%20M%20%3D%20np.linalg.norm(C%5B%3A%2C%20None%5D%20-%20C%5BNone%5D%2C%20axis%3D-1)%3B%20R%20%3D%20(np.bincount(l%2C%20own%2C%20K)%20%2F%20cnt)%5B%3A%2C%20None%5D%20%2B%20(np.bincount(l%2C%20own%2C%20K)%20%2F%20cnt)%5BNone%5D%0A%20%20%20%20R%20%3D%20R%20%2F%20np.where(M%20%3D%3D%200%2C%20np.inf%2C%20M)%3B%20np.fill_diagonal(R%2C%20-np.inf)%0A%20%20%20%20same%20%3D%20l%5B%3A%2C%20None%5D%20%3D%3D%20l%5BNone%5D%0A%20%20%20%20up%20%3D%20%7B%22Silhouette%22%3A%20sil%2C%20%22Calinski-Harabasz%22%3A%20ssb%20%2F%20(K%20-%201)%20%2F%20(ssw%20%2F%20(len(l)%20-%20K))%2C%20%22Dunn%22%3A%20D%5B~same%5D.min()%20%2F%20D%5Bsame%5D.max()%7D%0A%20%20%20%20down%20%3D%20%7B%22Davies-Bouldin%22%3A%20R.max(1).mean()%2C%20%22WB-index%22%3A%20K%20*%20ssw%20%2F%20ssb%7D%0A%20%20%20%20return%20%7B**%7Bk%3A%20v%20*%20f%20for%20k%2C%20v%20in%20up.items()%7D%2C%20**%7Bk%3A%20v%20%2F%20f%20for%20k%2C%20v%20in%20down.items()%7D%7D%0A%0Adef%20bic_icl(X%2C%20k)%3A%0A%20%20%20%20%22%22%22EM%20for%20a%20k-component%20full-covariance%20GMM%20(k-means%20init)%3B%20returns%20BIC%20and%20hard-ICL%20(lower%20is%20better).%22%22%22%0A%20%20%20%20n%2C%20d%20%3D%20X.shape%3B%20r%20%3D%20np.eye(k)%5Bkmeans(X%2C%20k)%5D%3B%20prev%20%3D%20-np.inf%0A%20%20%20%20for%20_%20in%20range(300)%3A%0A%20%20%20%20%20%20%20%20nk%20%3D%20r.sum(0)%20%2B%201e-9%3B%20pi%2C%20mu%20%3D%20nk%20%2F%20n%2C%20(r.T%20%40%20X)%20%2F%20nk%5B%3A%2C%20None%5D%0A%20%20%20%20%20%20%20%20S%20%3D%20%5B(r%5B%3A%2C%20j%2C%20None%5D%20*%20(X%20-%20mu%5Bj%5D)).T%20%40%20(X%20-%20mu%5Bj%5D)%20%2F%20nk%5Bj%5D%20%2B%201e-6%20*%20np.eye(d)%20for%20j%20in%20range(k)%5D%0A%20%20%20%20%20%20%20%20lp%20%3D%20np.stack(%5Bnp.log(pi%5Bj%5D)%20-%200.5%20*%20np.linalg.slogdet(S%5Bj%5D)%5B1%5D%20-%20np.log(2%20*%20np.pi)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-%200.5%20*%20np.einsum(%22ni%2Cij%2Cnj-%3En%22%2C%20X%20-%20mu%5Bj%5D%2C%20np.linalg.inv(S%5Bj%5D)%2C%20X%20-%20mu%5Bj%5D)%20for%20j%20in%20range(k)%5D%2C%201)%0A%20%20%20%20%20%20%20%20ll%20%3D%20np.logaddexp.reduce(lp%2C%201)%3B%20r%20%3D%20np.exp(lp%20-%20ll%5B%3A%2C%20None%5D)%0A%20%20%20%20%20%20%20%20if%20abs(ll.sum()%20-%20prev)%20%3C%201e-8%20*%20abs(ll.sum())%3A%20break%0A%20%20%20%20%20%20%20%20prev%20%3D%20ll.sum()%0A%20%20%20%20bic%20%3D%20-2%20*%20ll.sum()%20%2B%20(k%20*%20d%20*%20(d%20%2B%201)%20%2F%202%20%2B%20k%20*%20d%20%2B%20k%20-%201)%20*%20np.log(n)%20%20%20%23%20free%20parameters%20of%20a%20full-covariance%20GMM%0A%20%20%20%20return%20bic%2C%20bic%20-%202%20*%20np.log(r.max(1)).sum()%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20ICL%20%3D%20BIC%20%2B%202%20*%20entropy%20of%20the%20hard%20assignment
sep%20%3D%20mo.ui.slider(1.0%2C%208.0%2C%20step%3D0.5%2C%20value%3D4.0%2C%20label%3D%22cluster%20separation%22%2C%20debounce%3DTrue)%0Ak%20%3D%20mo.ui.slider(2%2C%208%2C%20step%3D1%2C%20value%3D3%2C%20label%3D%22K%20for%20k-means%22%2C%20debounce%3DTrue)%0Anoise%20%3D%20mo.ui.switch(label%3D%22add%20noise%20points%22)%0Amo.hstack(%5Bsep%2C%20k%2C%20noise%5D%2C%20justify%3D%22start%22)
X%2C%20y%20%3D%20blobs(sep.value%2C%20noise.value)%0Alab%20%3D%20kmeans(X%2C%20k.value)%0Alab_noise%20%3D%20np.where(y%20%3D%3D%20-1%2C%20-1%2C%20lab)%20%20%20%23%20noise%20known%20(e.g.%20from%20HDBSCAN)%3A%20label%20-1%0Am_plain%20%3D%20indices(X%2C%20lab%2C%20noise_label%3D-1)%0Am_noise%20%3D%20indices(X%2C%20lab_noise%2C%20noise_label%3D-1)%0Acrit%20%3D%20np.array(%5Bbic_icl(X%2C%20kk)%20for%20kk%20in%20range(1%2C%207)%5D)
fig%2C%20axs%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(12%2C%204.4))%0Aaxs%5B0%5D.scatter(*X.T%2C%20c%3Dlab_noise%2C%20cmap%3D%22viridis%22%2C%20s%3D12)%3B%20axs%5B0%5D.set_aspect(%22equal%22)%0Aaxs%5B0%5D.set_title(f%22k-means%2C%20K%20%3D%20%7Bk.value%7D%22%20%2B%20(%22%20(noise%20in%20grey-purple%3A%20label%20%E2%88%921)%22%20if%20noise.value%20else%20%22%22))%0Afor%20j%2C%20name%20in%20enumerate((%22BIC%22%2C%20%22ICL%22))%3A%0A%20%20%20%20axs%5B1%5D.plot(range(1%2C%207)%2C%20crit%5B%3A%2C%20j%5D%2C%20marker%3D%22o%22%2C%20label%3Dname)%3B%20axs%5B1%5D.plot(1%20%2B%20crit%5B%3A%2C%20j%5D.argmin()%2C%20crit%5B%3A%2C%20j%5D.min()%2C%20%22k*%22%2C%20ms%3D14)%0Aaxs%5B1%5D.set_xlabel(%22number%20of%20GMM%20components%20K%22)%3B%20axs%5B1%5D.set_ylabel(%22criterion%20(lower%20is%20better)%22)%3B%20axs%5B1%5D.legend()%0Aaxs%5B1%5D.set_title(f%22BIC%20picks%20K%20%3D%20%7B1%20%2B%20crit%5B%3A%2C%200%5D.argmin()%7D%2C%20ICL%20picks%20K%20%3D%20%7B1%20%2B%20crit%5B%3A%2C%201%5D.argmin()%7D%22)%0Afig
_arrow%20%3D%20%7B%22Silhouette%22%3A%20%22%E2%86%91%22%2C%20%22Calinski-Harabasz%22%3A%20%22%E2%86%91%22%2C%20%22Dunn%22%3A%20%22%E2%86%91%22%2C%20%22Davies-Bouldin%22%3A%20%22%E2%86%93%22%2C%20%22WB-index%22%3A%20%22%E2%86%93%22%7D%0Amo.md(%22%7C%20index%20%7C%20better%20%7C%20noise%20swallowed%20by%20clusters%20%7C%20noise%20labelled%20%E2%88%921%20(dropped%2C%20scaled)%20%7C%5Cn%7C---%7C---%7C---%7C---%7C%5Cn%22%20%2B%0A%20%20%20%20%20%20%22%5Cn%22.join(f%22%7C%20%7Bn%7D%20%7C%20%7B_arrow%5Bn%5D%7D%20%7C%20%7Bm_plain%5Bn%5D%3A.3f%7D%20%7C%20%7Bm_noise%5Bn%5D%3A.3f%7D%20%7C%22%20for%20n%20in%20m_plain)%20%2B%0A%20%20%20%20%20%20f%22%5Cn%5CnICL%20%E2%88%92%20BIC%20at%20K%20%3D%203%3A%20%7Bcrit%5B2%2C%201%5D%20-%20crit%5B2%2C%200%5D%3A.1f%7D%20(twice%20the%20assignment%20entropy%3B%20it%20grows%20with%20overlap).%22)
What to look for
- Separation: as the blobs overlap, silhouette and Calinski–Harabasz fall and Davies–Bouldin and WB rise. Dunn is the
most erratic (it rests on one closest pair and one widest cluster), which is why
tgmm's documentation ranks it behind the
others; at low separation its best \(K\) jumps around while silhouette and CH keep pointing at \(K=3\).
- K: at high separation almost every index peaks at the true \(K=3\); when the blobs overlap, WB-index and Davies–Bouldin
(and Dunn) are the ones that wander.
- Noise: the noise points are known here, so the second column drops them (label \(-1\)) and scales by
\((n-n_{\text{noise}})/n\). Calinski–Harabasz, WB and Dunn improve a lot; silhouette and Davies–Bouldin barely move.
The GMM panel cannot drop noise: it tends to spend an extra component on the background, so BIC and ICL often pick
\(K=4\) or \(5\) when noise is switched on.
- BIC vs. ICL: both are \(-2\log L+p\log n\); ICL adds \(-2\sum_i\log\max_k\tau_{ik}\), which is near 0 for well-separated
components and large when they overlap. Around separation \(\approx3\) ICL already prefers fewer components than BIC,
because it penalises solutions whose clusters are not clearly distinguishable (see the
Model Selection tutorial).