Skip to content
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

Clustering groups similar cases without labels

Clustering partitions examples according to a representation and a notion of similarity. It does not discover an objective set of natural categories. Change features, scaling, distance, or algorithm and the groups may change, so every cluster is a model output that needs evidence.

Purpose comes before distance

A grouping may propose customer segments, organize documents, expose operating states, or create downstream features. Define which decision or investigation it supports before choosing a metric. A mathematically tidy partition that changes no action is decoration.

K-Means looks for compact centroid groups

K-Means works best for roughly compact, similarly scaled clusters represented by centroids. Standardize features when units differ, use multiple initializations, and consider MiniBatchKMeans when full fitting is too expensive.

Density methods can leave noise unclustered

DBSCAN and HDBSCAN find dense regions rather than requiring K centroids. They can represent irregular shapes and leave sparse points unassigned, but distance scale, neighborhood parameters, and varying density strongly affect the result.

Agglomerative clustering exposes hierarchy

Agglomerative methods merge nearby groups from the bottom up. On modest datasets, a dendrogram can help explain how clusters relate. Linkage and distance choices determine what “nearby” means and must be part of the result.

Choose K with curves, stability, and use

Inspect inertia and silhouette over plausible K values without treating an elbow as ground truth. Repeat across seeds or resamples and connect K to operating capacity: ten statistically neat segments are useless if the team can support only three. For density methods, tie min_cluster_size to the smallest meaningful group.

Read original cases before naming a cluster

Inspect representative and boundary examples, profile groups on variables not used to create them, and check stability over time. Guard against leakage and proxies for protected attributes. Name a segment only after its members share a defensible pattern and the grouping supports a concrete action.

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.