Welcome to unsupervised learning, where the data has no labels and no target to predict. The goal shifts from "predict Y from X" to "what structure is hidden in X?" The most common answer is clustering: partitioning observations into groups so that members of a group are similar to each other and different from everyone else.
Discover the natural groups in unlabeled data: K-means partitions into k round clusters (choose k with the elbow and silhouette), hierarchical builds a dendrogram tree, and DBSCAN follows density and flags outliers, then profile each group to give it business meaning.
From Labels to Groups
Supervised learning learned a mapping from features to a known label. Clustering has no label at all. We give it a table of customers, and it returns a group number for each one, groups it invented by noticing which customers resemble each other. That makes clustering ideal for discovery: customer segments, document topics, gene families, image regions, none of which come pre-labeled.
Because every clustering method measures distance between points, one preprocessing step is non-negotiable: standardization. If income runs into the tens of thousands and a spending score runs 1 to 100, raw distance is almost entirely income, and the other features are ignored. Putting each feature on a common z-score scale (mean 0, standard deviation 1) lets every feature contribute fairly. Skipping this is the single most common clustering mistake.
K-means, and Choosing k
K-means is the workhorse. You tell it how many clusters k you want; it places k centers, assigns every point to its nearest center, moves each center to the mean of its members, and repeats until nothing changes. It is fast and scales to millions of rows, but it assumes clusters are roughly round and similar in size, and it makes you pick k up front.
How do you choose k when the data has more dimensions than you can plot? Two diagnostics, and you want them to agree. The elbow plot tracks total within-cluster distance (inertia) as k grows; it always falls, but the bend marks the point where extra clusters stop helping. The silhouette score measures how much closer each point sits to its own cluster than to the next-nearest one; higher is better, and it peaks at the right k.
On the customer data both point to k = 4 (silhouette 0.55), and that is the number we trust. When the elbow and the silhouette disagree, treat it as a signal that the clusters are not clean, and look harder before committing.
Beyond K-means: Hierarchical & DBSCAN
K-means is not the only tool, and its assumptions do not always hold. Two alternatives cover its blind spots.
Hierarchical clustering starts with every point alone and repeatedly merges the two closest groups, recording the full history as a dendrogram. You read the tree and cut it at a height that leaves the number of clusters you want, the tall gaps show the natural cut. It needs no k up front and is deterministic, though it is slower on large data. On our customers it recovers exactly the same four segments K-means found, independent confirmation the groups are real.
DBSCAN takes a completely different view: it grows clusters from dense regions and leaves sparse points unassigned. A point with enough neighbors within a radius seeds a cluster; points in no dense region are labeled noise. That gives it two abilities K-means lacks: it discovers the number of clusters on its own and handles non-round shapes, and it flags outliers instead of forcing them into a group. On our data it found the four segments and isolated 5 genuine outliers as noise.
Real-World Example: Customer Segmentation
The classic business use of clustering is customer segmentation: split a shapeless customer list into a handful of actionable groups, each worth a different strategy. There is no "true" segment label, that is exactly why clustering is the right tool.
330 e-commerce customers with annual_income_k,
spending_score, age, and web_visits_mo. Note there is
no label column, the groups are hidden in the features for you to discover.
Running K-means with k = 4, then profiling each cluster by its average feature values, turns four anonymous cluster ids into a marketing plan:
| Segment | Income | Spending | Read on the group |
|---|---|---|---|
| Premium | ~89k | ~83 | high earners who spend freely, nurture with loyalty perks |
| Young Spenders | ~28k | ~76 | low income but high spend and heavy browsing, upsell carefully |
| Savers | ~90k | ~20 | high income, low spend, an untapped opportunity to win over |
| Budget-Conscious | ~27k | ~22 | low income and low spend, serve efficiently, discount-driven |
That final profiling step, describing each cluster in plain business terms, is what makes the whole exercise pay off. The algorithm finds the groups; you give them meaning and a plan.
Clustering in Machine Learning & AI
Clustering is everywhere unlabeled data needs structure, which is almost everywhere.
| Method / idea | Where it is used |
|---|---|
| K-means | customer segmentation, image color quantization, vector quantization |
| Hierarchical | gene-expression and phylogenetic trees, taxonomy building |
| DBSCAN | spatial/GPS clustering, outlier and fraud flagging, non-round shapes |
| Silhouette / elbow | validating how many groups the data really supports |
| Embeddings + clustering | grouping documents, images, and users by learned representations |
Modern AI has supercharged clustering by changing what gets clustered. Instead of raw columns, systems first map text, images, or audio into dense embedding vectors with a neural network, then cluster those, which is how a search engine groups related documents, how a recommender finds "users like you," and how a labeling pipeline mines a huge unlabeled corpus for structure. Clustering embeddings is also central to retrieval-augmented generation and semantic search behind large language models. And because it needs no labels, clustering is a workhorse of exploratory analysis: the fastest way to ask a brand-new dataset "what natural groups are in here?" before you have any idea what to predict.
Cluster customers in Python
The companion notebook segments the shoppers with K-means, chooses k with the elbow and silhouette, builds a dendrogram, runs DBSCAN to flag outliers, and profiles every segment, each cell explained.
View opens the rendered notebook instantly. Open in Colab runs it live. To run
locally, install numpy, pandas, scikit-learn, scipy,
seaborn, and openpyxl.
🎓 Key Takeaways
- ✓Clustering is unsupervised: no labels, no answer key, the goal is to discover the natural groups in the data.
- ✓Standardize first, distance-based clustering is dominated by the largest-scale feature otherwise.
- ✓K-means is fast but assumes round, equal-size clusters and needs k; choose it with the elbow and silhouette together.
- ✓Hierarchical gives a dendrogram (no k up front); DBSCAN follows density, finds k itself, and flags outliers as noise.
- ✓Profiling each cluster's feature means is what turns anonymous groups into decisions.
Practice Challenges
Five short challenges. Try them with scikit-learn and scipy before checking the solutions.
K-means with k=4
Standardize the features, fit K-means with 4 clusters, and report the cluster sizes.
StandardScaler() then KMeans(n_clusters=4, n_init=10).Choose k
Use the silhouette score across k = 2..8 to justify the number of clusters.
silhouette_score(Xs, labels); pick the k that maximizes it.Hierarchical agreement
Cut a Ward dendrogram into 4 clusters and check it agrees with K-means.
linkage(Xs,'ward') then fcluster(Z, 4, 'maxclust'); crosstab.DBSCAN outliers
Tune DBSCAN to flag the handful of genuine outliers as noise.
DBSCAN(eps=0.85, min_samples=6); count labels equal to -1.Name the premium segment
Profile the clusters and identify the high-income, high-spending "Premium" group.
df.groupby('cluster')[feat].mean().A fully-worked solutions notebook walks through all five challenges, each verified in code. Try them yourself first, then compare.
Quiz: Test Yourself
Eight quick questions on clustering. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.
Clustering found groups in the rows. Next we compress the columns. Dimensionality Reduction uses PCA to squeeze many correlated features into a few informative axes, so you can visualize, denoise, and speed up high-dimensional data.