AI Hints
Home›Machine Learning Tutorial›Finding Groups
Machine Learning · Lesson 46 of 85

DBSCAN

Clusters of any shape.

⏱ 2 min read▶ Video at 1:39:09🎓 Free
Watch this lesson in the Machine Learning courseWatch on YouTube ↗Full Machine Learning course

Look at a map of a city at night, from above. You see bright, dense areas, and dark gaps between them. Those bright areas have all sorts of strange shapes, and they are not circles. You would never ask how many there are in advance. You simply see them. DBSCAN finds clusters the same way, by density.

DBSCAN
From the video: DBSCAN
  • City lights from above
  • Dense areas, strange shapes
  • Clustering by density

How DBSCAN thinks

DBSCAN has two settings. Epsilon is the size of a neighbourhood, and min samples is how many points must be inside it. A core point has plenty of neighbours. Clusters grow outward from core points, spreading to everything that they can reach. Any point that cannot be reached by any cluster is labelled as noise. So you do not choose the number of clusters, and it can reject outliers.

Epsilonthe neighbourhood size
Min sampleshow many make it dense
Core pointsgrow the cluster
Noisebelongs to no group

Two moons

Here are two interlocking crescent moons, a shape that defeats k means. We run both algorithms, and compare each with the truth. Run it. K means cuts straight across the moons, and matches poorly. DBSCAN follows the curves, and scores nearly perfectly. It found the two clusters by itself, and on data this clean, no point needed to be called noise. Same data, and a totally different result, because it looks at density instead of distance to a centre.

dbscan.pypython
from sklearn.cluster import DBSCAN, KMeans
from sklearn.datasets import make_moons
from sklearn.metrics import adjusted_rand_score

X, truth = make_moons(n_samples=500, noise=0.06, random_state=0)

km = KMeans(n_clusters=2, n_init=10, random_state=0).fit_predict(X)
db = DBSCAN(eps=0.2, min_samples=5).fit_predict(X)

print("K-Means match:", round(adjusted_rand_score(truth, km), 3))
print("DBSCAN match: ", round(adjusted_rand_score(truth, db), 3))
print("Clusters found:", len(set(db)) - (-1 in db))
print("Points called noise:", (db == -1).sum())
Output
K-Means match: 0.253
DBSCAN match:  1.0
Clusters found: 2
Points called noise: 0

Which clustering method?

Here is your guide. K means is fast and simple, for round groups on big data. Hierarchical gives a full tree, on smaller data. And DBSCAN handles odd shapes and noise, but struggles if the density varies a lot. Most projects start with k means, because it is quick, and move on if the groups do not make sense.

MethodBest forWeakness
K-MeansBig data, round groupsYou must choose k
HierarchicalSmall data, full treeSlow on big data
DBSCANOdd shapes, noiseVarying density

Summary

You now have three different ways to find structure, without any labels at all. But all of them get worse when there are too many features. High dimensions confuse distance. Next, a brilliant technique that squeezes many features into just a few, while keeping what matters.

Key takeaways
  • Density finds any shape
  • Noise points are allowed
  • Start with k-means, then move on
Next up: PCA — squeezing many features into few