Part 3: Machine learning Lesson 8 of 20 in this part
Clustering beyond k-means
What you will learn. Choose between k-means, hierarchical clustering, DBSCAN and Gaussian mixtures for a given shape of data, and score a clustering with the silhouette score without being fooled by stray points.
Before this lesson: Unsupervised learning: groups, compression and anomalies. Already know these? Carry on.
In plain words
k-means is the clustering method most people learn first, but it makes strong assumptions: you must say how many groups there are, every group is round and about the same size, and every point must belong to a group, even a stray one. Other methods relax one assumption each. Hierarchical clustering builds a family tree of merges, so you can cut it at any number of groups. DBSCAN finds groups as dense crowds of points and labels the lonely points as noise, so you do not need to say the number of groups. A Gaussian mixture treats each group as a bell-shaped cloud and gives every point a soft share in each group. To compare clusterings you can use the silhouette score, which asks of every point whether it sits closer to its own group than to the nearest other group.
k-means
- You choose the number of groups
- Round groups of similar size
Hierarchical
- Builds a tree of merges
- Cut the tree to get any number of groups
DBSCAN
- Finds dense crowds, no number needed
- Leaves stray points as noise
Gaussian mixture
- Bell-shaped groups of different shape
- Soft membership for each point
Pick by the shape of your data and by what you must decide in advance.
- InputCount neighbours within a radiusEnough of them makes it a core point
- ProcessStart a cluster from the core pointIts neighbours join
- ModelKeep spreading through core pointsThe cluster grows to the edge of the crowd
- RiskLeave unreached points as noiseThey are not in any group
A core point is one with enough neighbours inside the radius.
- InputAverage distance to the points in my own groupCall it a
- ProcessAverage distance to the nearest other groupCall it b
- OutputScore is (b minus a) over the largerNear one means well placed
The score runs from minus one to one. Higher is better separated.
Real-world example
A shop clusters its customers by how often they buy and how much they spend. Most customers form a few clear crowds. A handful of buyers spend enormous amounts on one-off orders. k-means is forced to put these buyers in some group, which drags that group's centre towards them and spoils the segment. DBSCAN marks them as noise, so the marketing team looks at them separately and the main segments stay clean. Later the team wants a tree they can show managers, so they use hierarchical clustering to explain how the segments merge.
How it works
- Scale the features so that no one feature dominates the distances.
- Pick a method from the diagram above. For unknown group counts or stray points, try DBSCAN. For an explainable tree, use hierarchical clustering. For overlapping round groups, try a Gaussian mixture.
- Set its dial: the number of groups for k-means and mixtures, the radius and the minimum neighbours for DBSCAN.
- Score and look. Compute the silhouette score, look at group sizes and noise, and plot the groups if you can. Score alone is not enough.
Python example
Python: example run The example programme behind this lesson's quoted output ran on 2026-11-19 (Python 3.12.10). 1 code block in the lesson passed a syntax check, 1 also ran on their own.
The data is 32 points: three tight groups of 10, plus two stray points far away, made by a repeatable rule. The program runs four methods and prints cluster sizes, noise points and the silhouette score, then tries k-means with different numbers of groups.
"""Four ways to cluster the same 32 points, and the silhouette score to compare them.
Three tight groups plus two stray points. Points come from a repeatable rule, so numbers repeat.
Run: python clustering.py (needs numpy and scikit-learn)
"""
import numpy as np
from sklearn.cluster import DBSCAN, AgglomerativeClustering, KMeans
from sklearn.metrics import silhouette_score
from sklearn.mixture import GaussianMixture
pts = []
for cx, cy in ((0.0, 0.0), (5.0, 0.0), (2.5, 4.0)):
for j in range(10):
r = 0.3 + 0.12 * (j % 5)
pts.append((cx + r * np.cos(j * 2.399963), cy + r * np.sin(j * 2.399963)))
pts += [(10.0, 10.0), (-4.0, 6.0)] # two stray points
X = np.array(pts)
def report(name, labels):
found = sorted(set(labels) - {-1})
sizes = [int(np.sum(labels == c)) for c in found]
noise = int(np.sum(labels == -1))
keep = labels != -1
sil = silhouette_score(X[keep], labels[keep])
print("%-18s clusters %d sizes %s noise %d silhouette %.3f" % (name, len(found), sizes, noise, sil))
report("DBSCAN", DBSCAN(eps=1.2, min_samples=4).fit_predict(X))
report("k-means (k=3)", KMeans(n_clusters=3, n_init=10, random_state=0).fit_predict(X))
report("hierarchical (Ward)", AgglomerativeClustering(n_clusters=3, linkage="ward").fit_predict(X))
report("Gaussian mixture", GaussianMixture(n_components=3, random_state=0).fit_predict(X))
for k in (2, 3, 4, 5):
print("k-means with k=%d: silhouette %.3f" % (k, silhouette_score(X, KMeans(n_clusters=k, n_init=10, random_state=0).fit_predict(X))))DBSCAN clusters 3 sizes [10, 10, 10] noise 2 silhouette 0.831
k-means (k=3) clusters 3 sizes [11, 11, 10] noise 0 silhouette 0.695
hierarchical (Ward) clusters 3 sizes [12, 10, 10] noise 0 silhouette 0.695
Gaussian mixture clusters 3 sizes [10, 12, 10] noise 0 silhouette 0.695
k-means with k=2: silhouette 0.478
k-means with k=3: silhouette 0.695
k-means with k=4: silhouette 0.744
k-means with k=5: silhouette 0.779DBSCAN finds the three real groups of 10 and calls the two stray points noise. Its score is 0.831. The other three methods must place every point somewhere, so the two stray points join groups (sizes 11, 11, 10, or 12, 10, 10), and the score drops to 0.695. Ward hierarchical clustering and the Gaussian mixture reached the same grouping and the same score.
.NET (C#) example
C#: compiled and run The C# sample compiled on 2026-11-19 with .NET SDK 10.0.400 (no extra packages). It also ran to the end without error.
ML.NET has k-means but no DBSCAN, so this version writes DBSCAN and the silhouette score from scratch in C#. It needs no packages and prints the same DBSCAN line as the Python run: three clusters of 10, two noise points, silhouette 0.831. Hierarchical clustering and Gaussian mixtures are shown in Python only, because no .NET library for them is part of this guide's verified set.
// DBSCAN and the silhouette score written from scratch (ML.NET offers k-means but not DBSCAN).
// Same points and the same DBSCAN line as clustering.py.
var pts = new List<(double x, double y)>();
foreach (var (cx, cy) in new[] { (0.0, 0.0), (5.0, 0.0), (2.5, 4.0) })
for (int j = 0; j < 10; j++)
{
double r = 0.3 + 0.12 * (j % 5);
pts.Add((cx + r * Math.Cos(j * 2.399963), cy + r * Math.Sin(j * 2.399963)));
}
pts.Add((10.0, 10.0)); pts.Add((-4.0, 6.0)); // two stray points
int n = pts.Count;
double Dist(int a, int b) => Math.Sqrt(Math.Pow(pts[a].x - pts[b].x, 2) + Math.Pow(pts[a].y - pts[b].y, 2));
// DBSCAN: a point with at least minPts points within eps (itself included) is a core point; clusters grow from core points.
const double eps = 1.2; const int minPts = 4;
int[] label = Enumerable.Repeat(-2, n).ToArray(); // -2 = not visited, -1 = noise
List<int> Near(int p) => Enumerable.Range(0, n).Where(q => Dist(p, q) <= eps).ToList();
int cluster = 0;
for (int p = 0; p < n; p++)
{
if (label[p] != -2) continue;
var seeds = Near(p);
if (seeds.Count < minPts) { label[p] = -1; continue; }
label[p] = cluster;
var queue = new Queue<int>(seeds);
while (queue.Count > 0)
{
int q = queue.Dequeue();
if (label[q] == -1) label[q] = cluster; // a noise point next to a core point joins the cluster
if (label[q] != -2) continue;
label[q] = cluster;
var more = Near(q);
if (more.Count >= minPts) foreach (int m in more) queue.Enqueue(m);
}
cluster++;
}
// Silhouette: for each point, a = average distance inside its cluster, b = average distance to the nearest other cluster.
var keep = Enumerable.Range(0, n).Where(i => label[i] >= 0).ToList();
double silhouette = keep.Average(i =>
{
var own = keep.Where(j => label[j] == label[i] && j != i).ToList();
double a = own.Count == 0 ? 0 : own.Average(j => Dist(i, j));
double b = keep.Where(j => label[j] != label[i]).GroupBy(j => label[j]).Min(g => g.Average(j => Dist(i, j)));
return own.Count == 0 ? 0 : (b - a) / Math.Max(a, b);
});
var sizes = Enumerable.Range(0, cluster).Select(c => label.Count(l => l == c));
Console.WriteLine($"{"DBSCAN",-18} clusters {cluster} sizes [{string.Join(", ", sizes)}] noise {label.Count(l => l == -1)} silhouette {silhouette:F3}");Maths behind it
- Euclidean distance between points (geometry)
- Average distance within and between groups (statistics)
- The silhouette score formula (statistics)
- Bell-shaped (Gaussian) distributions for mixtures (probability)
Common mistakes
- Clustering without scaling the features first.
- Trusting the silhouette score alone, especially when stray points exist.
- Forcing every point into a group when some are genuinely unusual.
- Choosing the number of groups by the score alone instead of by what the groups mean for the business.
- Treating cluster numbers as meaningful. Cluster 0 is just a label.
Quick check
Your data has a few very unusual points and you do not know the number of groups. Which method do you try first?
Show answer
DBSCAN. It finds dense groups without being told how many, and it leaves the unusual points as noise.
What does a silhouette score near 0 mean for a point?
Show answer
The point is on the border between two groups. It is about as close to another group as to its own.
Why did k-means with 5 groups get a higher silhouette than with 3 on the example data?
Show answer
The two stray points formed small groups of their own, which raises the average. The extra groups are not real segments, which is why you read group sizes too.
When would you pick hierarchical clustering over k-means?
Show answer
When you want a tree you can cut at different levels and explain, or when you do not want to fix the number of groups in advance.
Completion
Evidence of completion. A table comparing at least three clustering methods on one dataset with cluster sizes, noise points and silhouette scores, plus a sentence saying which you would use and why.
Saved in this browser only. Reading and exercise are ticked separately because they are different achievements. A tick records your own work; it is not a test result. See all progress.