Search

Search papers and lessons

DBSCAN

Level 3 · 13 min read

DBSCAN groups points that sit in dense crowds, leaves sparse stragglers out as noise, and needs no preset number of clusters.

On this page

  • Colour = topic mix
  • Linear algebra
  • Learning basics
  • Discrete math
  • ReadReady to readNot yet

TL;DR

DBSCAN is a clustering method. You give it a set of points, a radius called ε\varepsilon, and a count called MinPts. It finds regions where points are packed closely enough, joins each region into one cluster, and labels points that sit alone in empty space as noise. It does not ask you for the number of clusters in advance.

It is useful because real data often has clusters of odd shapes, such as curved strips or rings, and stray points that belong to no group. Methods that assume round blobs handle these poorly.

Builds on: Euclidean Distance, Breadth-First Search · Leads to: HDBSCAN and density-based outlier detection

Where it's used

Finding places in GPS tracks. A phone that records its position every few seconds produces a long list of points. Where the person stayed at a café for an hour, many points sit within a few tens of metres of each other. DBSCAN turns that dense patch into one "stop", and the points on the walk between stops are noise. Location-analysis tools use this kind of density grouping to find places a person visits often.

Spotting unusual transactions. A bank describes each card payment by a few numbers, such as amount, time and merchant type. Ordinary payments form dense groups, and a payment far from all of them is a candidate for fraud review. DBSCAN's noise label gives a list of such candidates without anyone choosing a number of groups first.

Cleaning sensor data. A weather station network reports temperature and pressure readings. Readings that come from a faulty sensor often sit away from the main cloud of values. DBSCAN separates the main cloud from those readings, so an engineer can check the faulty ones.

Grouping astronomical objects. Surveys of the sky record positions of stars and galaxies. Galaxies gather in dense clumps with long, thin filaments between them. A method that finds dense clumps of any shape suits this better than one that expects round blobs.

How it works

Use six points on a flat plane, with ε=1.5\varepsilon = 1.5 and MinPts = 3. MinPts counts the point itself. The points are A = (0, 0), B = (1, 0), C = (0, 1), D = (1, 1), G = (2.2, 0), and E = (5, 5), plus one more far away point F = (9, 9).

  1. Choose the two settings. The radius is ε=1.5\varepsilon = 1.5 and the crowd size is MinPts = 3. A point is "in a crowd" if at least 3 points, itself included, lie within 1.5 of it. Check: the radius and the count are both chosen by the user before the run starts.
  2. For each point, count its neighbours. Measure the straight-line distance to every other point and keep those within 1.5. A is within 1.5 of B (distance 1), C (distance 1) and D (distance about 1.41). Adding A itself gives 4 points. Check: 1 and 1 and 1.41 are all at most 1.5, so A keeps 3 neighbours plus itself, which is 4.
  3. Mark the core points. A point with at least MinPts points in its neighbourhood is a core point. A, B, C and D each have 4 or more points, so they are core points. G has only itself and B, which is 2 points, so it is not core. Check: 2 is less than 3, so G fails the test.
  4. Grow a cluster from a core point. Start at A. Every core point within 1.5 of A joins its cluster, and so does every core point near those, and so on. B, C and D are all core and all lie within reach, so they join the cluster. Check: the chain A to B to D to C covers all four core points.
  5. Attach the border points. G is within 1.5 of B, a core point, so G joins the cluster as a border point. It is not core, so nothing spreads from G. Check: G is at distance 1.2 from B, which is at most 1.5.
  6. Label what is left. E and F are each alone, with no core point within 1.5. They are noise. Check: E's nearest other point is D at distance about 5.7, far beyond 1.5.

The result is one cluster, {A, B, C, D, G}, and two noise points, E and F.

flowchart TD
  P["Pick an unvisited point"] --> Q{"Has at least MinPts neighbours?"}
  Q -- "yes: core" --> C["Start or extend a cluster"]
  Q -- "no" --> R{"Within reach of a core point?"}
  R -- "yes: border" --> B["Add to that cluster, do not expand"]
  R -- "no" --> N["Label as noise"]
  C --> P
  B --> P
  N --> P

The math

Definition: ε-neighbourhood

For a point pp in a data set DD, the ε\varepsilon-neighbourhood is the set of points within distance ε\varepsilon of pp, including pp itself: Nε(p)={ q∈D:d(p,q)≤ε }N_\varepsilon(p) = \{\, q \in D : d(p, q) \le \varepsilon \,\} Here dd is the distance, usually the Euclidean distance.

Step 2 in symbols

Write ∣Nε(p)∣|N_\varepsilon(p)| for the number of points in the neighbourhood. In the example, ∣N1.5(A)∣=4|N_{1.5}(A)| = 4, counting A, B, C and D.

Step 3 in symbols

A point pp is a core point when ∣Nε(p)∣≥MinPts.|N_\varepsilon(p)| \ge \text{MinPts}. A point qq is a border point when it is not core but lies in Nε(p)N_\varepsilon(p) for some core point pp. Any other point is noise.

Step 4 in symbols

Point qq is directly density-reachable from pp when pp is core and q∈Nε(p)q \in N_\varepsilon(p). Point qq is density-reachable from pp when a chain p=p0,p1,…,pk=qp = p_0, p_1, \dots, p_k = q exists in which each pi+1p_{i+1} is directly density-reachable from pip_i. A cluster is the set of all points density-reachable from one core point. Only core points can extend the chain, which is why G, a border point, does not spread the cluster further.

The growth in step 4 is a breadth-first search over the graph whose edges join directly density-reachable pairs. Each cluster is one connected piece of that graph.

Worked examples

Example
1. A tiny one-dimensional case

Points on a line: 1, 2, 3 and 10. Use ε=1.5\varepsilon = 1.5 and MinPts = 2.

  1. Neighbourhood of 1: points within 1.5 are 1 and 2. That is 2 points, which is at least 2, so 1 is core.
  2. Neighbourhood of 2: points within 1.5 are 1, 2 and 3. That is 3 points, so 2 is core.
  3. Neighbourhood of 3: points within 1.5 are 2 and 3. That is 2 points, so 3 is core.
  4. Neighbourhood of 10: only 10 itself, since the nearest other point is 3, at distance 7. That is 1 point, so 10 is not core.
  5. Grow from 1: it reaches 2, which reaches 3. All three are core, so they form one cluster.
  6. Point 10 is not within 1.5 of any core point, so it is noise.

Result: cluster {1, 2, 3}, noise {10}. The interpretation is that the three nearby values form a group, and the isolated value stands apart.

Example
2. The standard case, with a different radius

Take the six-point set from "How it works", but change the radius to ε=1.0\varepsilon = 1.0, keeping MinPts = 3.

  1. Neighbourhood of A at radius 1.0: B (distance 1) and C (distance 1). Adding A gives 3 points, so A is core.
  2. Neighbourhood of B: A (distance 1), D (distance 1), and not G, since G is at distance 1.2. Adding B gives 3 points, so B is core.
  3. Neighbourhood of C: A and D, since C to D is distance 1, while C to B is about 1.41 and falls outside. Adding C gives 3 points, so C is core.
  4. Neighbourhood of D: B and C, since D to A is about 1.41. Adding D gives 3 points, so D is core.
  5. Neighbourhood of G: only B is at distance 1.2, which is more than 1.0, so G has just itself. It is not core and is not within reach of any core point, so G becomes noise.
  6. Growing from A reaches B, C and D. The cluster is {A, B, C, D}.
  7. Noise: E, F and G.

Result: cluster {A, B, C, D}, noise {E, F, G}. Shrinking the radius by a third moved G from the cluster into the noise. The interpretation is that the answer depends on the radius, so the radius has to be chosen with the data's scale in mind.

Example
3. A realistic check on a location trace

A phone records 12 positions. Suppose you set ε=50\varepsilon = 50 metres and MinPts = 5. One recorded position p has 7 positions within 50 metres of it, itself included.

  1. Count the neighbourhood: 7 positions, which is at least 5. So p is a core point.
  2. Look at the neighbours of p. Each one that is also core spreads the cluster further. Suppose 6 of the 7 are core, and they chain together. The whole group of 7 forms one stop.
  3. A position on the walk between two stops, 400 metres from any other recorded point, has only itself in its neighbourhood. It is noise.
  4. A position 30 metres from the stop, with only 3 positions near it, is not core, but it lies within 50 metres of a core point. It is a border point of the stop's cluster.
  5. Each stop becomes a cluster, and each stop's time span can be read from the timestamps of its member points.

Try it: a phone records 9 positions. Eight of them lie within 40 metres of one another, and the ninth lies 300 metres away. With ε=50\varepsilon = 50 and MinPts = 5, how many clusters are there, and how many noise points?

Answer: one cluster, from the eight close positions. Each of those eight has at least 5 positions in its 50-metre neighbourhood, because the group is tightly packed, so they are core. The ninth position has only itself nearby, so it is noise.

Common mistakes and tradeoffs

It is tempting to think DBSCAN finds the right number of clusters on its own. In fact it finds clusters only as well as the two settings allow, because ε\varepsilon and MinPts decide what counts as dense. Change either and the answer changes, as Example 2 showed. Trying a few values and inspecting the result is part of the work.

It is tempting to think the noise label means "outlier". In fact noise means "not dense enough for any cluster", which depends on the chosen settings. A point can be noise under one setting and in a cluster under another.

It is tempting to think DBSCAN handles clusters of very different density. In fact one fixed radius suits one density. A tight cluster and a loose cluster in the same data often cannot both be found with one setting, because the radius that catches the loose one merges the tight one with its surroundings.

It is tempting to think distances work the same in any number of features. In fact, when there are many features, most pairs of points end up at similar distances, so no neighbourhood looks dense. Scaling the features first is also necessary, because a feature measured in large units dominates the Euclidean distance; see feature scaling.

Border points can be reached from two clusters. DBSCAN gives each one to whichever cluster reaches it first, so the assignment depends on the order of the points.

When to use it: the clusters have irregular shapes, the data has a noticeable amount of noise, and the density is roughly even across the data. When not to use it: the clusters have very different densities, the data has many features, or you need each point to have a probability of belonging to each cluster. In the last case, a mixture model is a better fit. If you already know the number of groups and they are roughly round, k-means is simpler and faster. If you want a tree of nested groupings, hierarchical clustering fits better.

History

Clustering methods in the 1990s were mostly based on k-means or on hierarchical merging. Both tend to find round groups, and both need a choice that the user often cannot make in advance, such as how many groups to look for. Researchers wanted a method that found groups of any shape and also marked the points that belonged to none.

In 1996 Martin Ester, Hans-Peter Kriegel, Jörg Sander and Xiaowei Xu published the DBSCAN method at the KDD conference on knowledge discovery in data. The idea of a density threshold with a radius was simple enough to implement in a few pages, and it handled noise without extra work. It was widely used in later years, and the paper later received the SIGKDD test-of-time award, which the conference gives to work that stayed influential.

Recap

  • Points are grouped by density: a point is core when at least MinPts points lie within the radius ε\varepsilon.
  • A cluster grows from a core point through every point density-reachable from it, which is a breadth-first search over close points.
  • Border points join a cluster without spreading it, and points reached by no core point are noise.
  • The result depends on ε\varepsilon and MinPts, and it struggles with clusters of very different density.
  • Next, read the HDBSCAN paper, which removes the single-radius limit, and compare with k-nearest neighbours, which uses the same distances to predict a label.

Check yourself

QuestionRecall: what are the two settings DBSCAN needs from the user, and what does each control?Show answer

The radius ε\varepsilon sets how far apart points can be and still count as neighbours. MinPts sets how many points, itself included, a neighbourhood needs before its centre counts as a core point. Together they decide what dense means for the run.

QuestionCompute: points 0, 1, 2, 7 and 8 on a line, with ε=1.5\varepsilon = 1.5 and MinPts = 3. How many clusters, and how many noise points?Show answer

Neighbourhoods: 0 has {0, 1} (2 points, not core). 1 has {0, 1, 2} (3 points, core). 2 has {1, 2} (2 points, not core). 7 has {7, 8} (2 points, not core). 8 has {7, 8} (not core). The only core point is 1, and it reaches 0 and 2 as border points, so there is one cluster {0, 1, 2}. The points 7 and 8 are within 1.5 of each other but neither is core and no core point is within reach, so they are noise. Answer: one cluster, two noise points (7 and 8). Note that 0 and 2 are border points in the cluster, not core points, because each has only 2 points in its neighbourhood.

QuestionWhy can one radius not handle a tight cluster and a loose cluster in the same data?Show answer

A radius that is small enough to keep the tight cluster separate will leave the loose cluster's points too far apart to form core points, so the loose cluster breaks into noise. A radius large enough for the loose cluster will pull in points from its surroundings and from the tight cluster. Because the radius is one number for the whole data set, no single value fits both densities.

QuestionApplied: a biologist clusters cells from 200 measurements each. Results are poor. Name two likely causes that have nothing to do with the algorithm's idea, and what to check first.Show answer

Two likely causes are unscaled features and too many features. If one measurement has values in the thousands and another has values near 1, the large one dominates the Euclidean distance, so the neighbourhoods reflect that one measurement only. Many features also make distances similar for every pair, so no neighbourhood looks dense. The first check is to standardise each feature to a similar scale, and then to see whether a reduced set of features, for example from principal component analysis, gives clearer clusters.

Sources