Sub topic 1
What clustering does and what counts as a cluster
Prerequisites
This chapter assumes you know basic Python and NumPy, and that you understand unsupervised learning, meaning learning from data that has no labels. You should know Euclidean distance between two points. You should also be comfortable with scikit-learn's fit and predict style API.
What clustering does
Clustering groups data points so that points in the same group are more similar to each other than to points in other groups. No labels are given. The algorithm decides the groups from the features alone.
Typical uses include:
- Segmenting customers by behavior.
- Grouping documents by topic.
- Finding unusual points that fit no group.
- Compressing data by replacing many points with a group summary.
Because there are no labels, there is no single correct answer to check against. The result depends on the method, its settings, and how you measure similarity.
What counts as a cluster
"Cluster" has no single definition. Different methods rely on different ideas, and each idea finds different shapes.
| Idea | A cluster is... | Example method |
|---|---|---|
| Centroid-based | Points close to a shared center | k-means |
| Hierarchical | A node in a tree of nested groups | Agglomerative clustering |
| Density-based | A dense region separated by sparse regions | DBSCAN |
This tutorial covers the second and third ideas. Hierarchical clustering builds a tree of merges, and you choose where to cut it. DBSCAN grows clusters from dense regions and labels isolated points as noise.
What makes a good cluster
Most definitions aim for two properties:
- Cohesion: points inside a cluster are close together.
- Separation: different clusters are far apart.
These are goals, not guarantees. Two half-moon shapes interleave, so their points are not compact around a center. A centroid-based method splits them badly, while a density-based method can recover them. A good clustering depends on the shape of your data and on what you plan to do with the result.
Two practical points follow:
- Every method needs a notion of distance, so the choice of distance and the scale of your features change the clusters. The next chapters cover both.
- Some methods force every point into a cluster. DBSCAN can instead mark points as noise, which is useful when outliers are real.
A first look
This example generates three blobs and shows that clustering returns only group ids. Nothing tells you whether the ids are meaningful.
import numpy as np
from sklearn.cluster import AgglomerativeClustering
from sklearn.datasets import make_blobs
X, _ = make_blobs(n_samples=60, centers=3, random_state=0)
labels = AgglomerativeClustering(n_clusters=3).fit_predict(X)
print(np.unique(labels, return_counts=True))The output lists the cluster ids and how many points each one holds. The label numbers are arbitrary. Label 0 in one run has no special link to label 0 in another.
Exercise
A dataset has two crescent-shaped groups that wrap around each other. Which definition of a cluster fits better, centroid-based or density-based, and why?
Show answer
Density-based. Each crescent is a connected dense region, but its points are not compact around one center. A centroid-based method would cut across the crescents, while a density-based method follows the connected dense points.