← Lesson
BitWithBite
AI & Machine Learning · Quick Reference

lesson-4.2 Cheat Sheet

AI & Machine Learning
In one line: K-Means requires you to specify k up front — choosing it well is its own problem, separate from running the algorithm itself.

Key Ideas

1Inertia (within-cluster sum of squares, WCSS). The sum of squared distances from each point to its assigned centroid. Inertia always decreases as k increases — more clusters can only fit the data at least as well —...
2The elbow method. Plot inertia against k. Early increases in k reduce inertia sharply; past some point, additional clusters give diminishing returns and the curve flattens. The "elbow" ...
3Silhouette score. A more principled per-point metric. For each point, compare its average distance to points in its own cluster (cohesion) against its average distance to points in the ...
4Silhouette plots. Go further than the average alone by showing per-point silhouette values for each cluster, sorted within each cluster. This reveals more than a single number can: a cl...

Code Examples

from sklearn.cluster import KMeans from sklearn.metrics import silhouette_score inertias, sil_scores = [], [] for k in range(2, 10): km = KMeans(n_clusters=k, random_state=42).fit(X) inertias.append(km.inertia_) sil_scores.append(silhoue...