Clustering
Beginner 10 min read Clustering

Clustering Basics

Partition by similarity

Clustering partitions data into groups of similar rows — centroids (KMeans), density (DBSCAN) or hierarchies (agglomerative) — quantified by cohesion and separation.

clusteringpartitionscentroiddensityhierarchywithin-clustersimilarity
Before we start

What you will learn

  • Define clustering's goal and the similarity/scale prerequisites
  • Contrast centroid-, density- and hierarchy-based families
  • Choose cluster count and validate cluster quality basics
Introduction

What is this?

Clustering splits a dataset into non-overlapping groups such that rows inside a group are mutually similar and groups are mutually distinct. KMeans iterates centroid assignments (fast, convex-ish, assumes spherical-ish blobs); DBSCAN joins density-reachable points (arbitrary shapes, noise-aware); agglomerative merges a hierarchy (interpretable dendrogram). Distance choice and feature scaling decide what 'similar' means — the silent assumption under every cluster.

Motivation

Why do you need it?

  • Segments, taxonomies and exploratory structure all start from a sensible partition.
  • Clusters feed cold-start recommenders, anomaly pre-screens, and capacity planning.
  • Learning three families teaches you which geometry assumptions your data really has.
  • Every clustering output is only as good as its validation — a core senior interview topic.
Think about it

Mental model

Imagine

Organizing a crowdsourced photo album by faces

Think of

Clustering

You group photos of the same faces (similar rows); some sorting rules use 'average face' (centroid), others use 'who's near whom' (density), and you can also build a family tree (hierarchy). The grouping is only useful if the faces look right to an expert.

Mechanics

How it works

  • Scale and choose a distance (Euclidean for convex blobs; watch unit inflation).
  • Pick a family by expected shape and noise: KMeans blobs, DBSCAN arbitrary+noise, agglomerative nested groups.
  • Fit to get assignments; quantify fit with within-cluster sum of squares/silhouette.
  • Validate stability across seeds/subsamples and interpretability with domain experts.
  • Iterate feature choices — scaling and selection move the clusters more than the algorithm does.

Family shapes.

Centroid
Density
Hierarchy
Validate
Hands-on

Examples

Three families on one cloud

Three families on one cloud
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans, DBSCAN, AgglomerativeClustering
from sklearn.preprocessing import StandardScaler
X, _ = make_blobs(n_samples=600, centers=4, n_features=3, random_state=0,
cluster_std=1.4)
Xs = StandardScaler().fit_transform(X)
k = KMeans(n_clusters=4, n_init=10, random_state=0).fit(Xs)
d = DBSCAN(eps=0.6, min_samples=8).fit(Xs)
a = AgglomerativeClustering(n_clusters=4).fit(Xs)
print("kmeans sizes:", np.bincount(k.labels_))
print("dbscan sizes:", np.bincount(d.labels_).tolist()) # -1 = noise
print("agglo sizes:", np.bincount(a.labels_))

All three recover the 4 blobs, but DBSCAN must tune eps and returns labeled noise — the family choice encodes assumptions that matter on real data.

Real world

In production

  • Retail segments: KMeans on scaled spend/recency/frequency drives campaign targeting.
  • Fraud screening: DBSCAN clusters normal behavior, leaving novel activity as noise points.
  • Org diagnostics: agglomerative builds decision trees of account types for review.
Guidelines

Best practices

  • Standardize before distance clustering; always inspect cluster sizes (tiny clusters = artifacts); run 2-3 seeds and subsamples for stability; have a domain expert review cluster means.
Watch out

Common mistakes

  • Euclidean on unscaled features; assuming clusters are perfectly round (KMeans bias); forcing k where density says noise; trusting cluster labels as ground truth.
When things break

Debugging tips

  • Cluster count far from intuition? Sweep k and look at stability, not just one fit.
  • DBSCAN returning one giant cluster + noise: eps too big/small — sweep eps.
  • Re-run with features subset; clusters shouldn't crumble with modest changes.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

DBSCAN marks a point as noise when:

2

A dendrogram is the output of:

Go deeper

Official documentation