C.W.K.
Stream
Lesson 01 of 04 · published

Clustering

~28 min · clustering, kmeans, dbscan

Level 0Scout
0 XP0/48 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

What clustering is for

Clustering groups examples that look similar under some distance. Used well, it generates customer segments, anomaly candidates, or compact features for downstream models. Used badly, it produces beautiful charts that match nothing the business cares about.

Three algorithms with three intuitions

  • K-Means — assumes K convex blobs of similar size; fast, scales to millions of rows.
  • DBSCAN / HDBSCAN — density-based, finds arbitrary shapes, handles noise, leaves "unclustered" rows alone.
  • Agglomerative — bottom-up hierarchy; great for small datasets where interpretability matters.

Picking K (or letting K pick itself)

For K-Means, plot inertia vs K (elbow) and silhouette score; pick the K where the elbow bends. For HDBSCAN, set min_cluster_size based on what would matter to the business ("a real segment is at least 200 customers"). Always validate clusters by inspecting examples, not by trusting the score alone.

Code

K-Means with the elbow and silhouette diagnostics·python
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import StandardScaler

Xs = StandardScaler().fit_transform(X)
for k in range(2, 11):
    km = KMeans(n_clusters=k, n_init="auto", random_state=7).fit(Xs)
    sil = silhouette_score(Xs, km.labels_)
    print(f"k={k}  inertia={km.inertia_:,.0f}  silhouette={sil:.3f}")
HDBSCAN for density-based segmentation with noise·python
import hdbscan

clusterer = hdbscan.HDBSCAN(min_cluster_size=200, min_samples=5)
labels = clusterer.fit_predict(Xs)
print("clusters:", len(set(labels)) - (1 if -1 in labels else 0))
print("noise:", (labels == -1).mean())

External links

Exercise

Cluster your dataset with K-Means and HDBSCAN. Sample 10 members from each cluster and write a one-sentence persona. Decide whether the clusters answer a real business question or are an artifact of your features.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.