DBSCAN (Density-Based Spatial Clustering) › Worked Example

Michael BrenndoerferUpdated December 18, 20257 min read
Machine Learning from ScratchAll chapters
Part 7 of 9Unsupervised Learning
Chapter 55DBSCAN (Density-Based Spatial Clustering) › Worked Example
Reading section 4 of 8 · Worked Example · 7 minSection 4 of 8 · 7 min

Book contents

Section 4 of 8 · 7 min read

DBSCAN (Density-Based Spatial Clustering) › Worked ExampleOne section · Sections in this chapter4 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's work through a concrete numerical example to see how DBSCAN's mathematical concepts translate into actual clustering decisions. We'll use a small dataset where we can trace every calculation by hand, building intuition for how the algorithm discovers clusters.

The Dataset

Consider nine points in 2D space, arranged to form two distinct groups with one isolated point:

PointCoordinatesVisual Location
A(1, 1)Lower-left cluster
B(1, 2)Lower-left cluster
C(2, 1)Lower-left cluster
D(2, 2)Lower-left cluster
E(8, 8)Upper-right cluster
F(8, 9)Upper-right cluster
G(9, 8)Upper-right cluster
H(9, 9)Upper-right cluster
I(5, 5)Isolated in center

We'll use parameters: eps = 1.5 and min_samples = 3

Out[7]:
Visualization
Scatter plot of nine labeled points A through I showing two clusters in opposite corners and one isolated point in the center, with epsilon neighborhoods drawn as dashed circles.
The worked example dataset with nine points arranged in two dense clusters and one isolated point. Points A-D form the lower-left cluster, points E-H form the upper-right cluster, and point I sits isolated in the center. The dashed circles show the ε-neighborhood (radius = 1.5) around each point, illustrating which points can 'see' each other as neighbors.

Step 1: Calculate Distances

First, we need to find which points are neighbors. Using Euclidean distance:

d(p,q)=(x2−x1)2+(y2−y1)2d(p, q) = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}

Let's calculate key distances for point A:

  • d(A,B)=(1−1)2+(2−1)2=0+1=1.0d(A, B) = \sqrt{(1-1)^2 + (2-1)^2} = \sqrt{0 + 1} = 1.0 ✓ (within eps)
  • d(A,C)=(2−1)2+(1−1)2=1+0=1.0d(A, C) = \sqrt{(2-1)^2 + (1-1)^2} = \sqrt{1 + 0} = 1.0 ✓ (within eps)
  • d(A,D)=(2−1)2+(2−1)2=1+1=1.414d(A, D) = \sqrt{(2-1)^2 + (2-1)^2} = \sqrt{1 + 1} = 1.414 ✓ (within eps)
  • d(A,E)=(8−1)2+(8−1)2=49+49=9.899d(A, E) = \sqrt{(8-1)^2 + (8-1)^2} = \sqrt{49 + 49} = 9.899 ✗ (too far)
  • d(A,I)=(5−1)2+(5−1)2=16+16=5.657d(A, I) = \sqrt{(5-1)^2 + (5-1)^2} = \sqrt{16 + 16} = 5.657 ✗ (too far)

Since the lower-left and upper-right clusters are symmetric, similar calculations apply.

Step 2: Build Neighborhoods

For each point, we identify all neighbors within eps = 1.5:

PointNeighborhood Neps(p)N_{eps}(p)Size ∥Neps(p)∥\|N_{eps}(p)\|
A{A, B, C, D}4
B{A, B, C, D}4
C{A, B, C, D}4
D{A, B, C, D}4
E{E, F, G, H}4
F{E, F, G, H}4
G{E, F, G, H}4
H{E, F, G, H}4
I{I}1

Notice that points A, B, C, D form a tightly connected group where everyone is neighbors with everyone else. The same is true for E, F, G, H. Point I stands alone.

Step 3: Identify Core Points

Now we apply the density test: ∣Neps(p)∣≥min_samples|N_{eps}(p)| \geq min\_samples

With min_samples = 3:

PointNeighborhood Size≥ 3?Classification
A4✓Core point
B4✓Core point
C4✓Core point
D4✓Core point
E4✓Core point
F4✓Core point
G4✓Core point
H4✓Core point
I1✗Not core

All points in the two dense groups qualify as core points. Point I, with only itself in its neighborhood, fails the density test.

Step 4: Expand Clusters

Now we execute the DBSCAN algorithm, visiting points and expanding clusters:

Iteration 1: Visit point A

  1. Mark A as visited
  2. A is a core point → Create Cluster 1
  3. Add A to Cluster 1: {A}
  4. Queue A's neighbors for expansion: {B, C, D}
  5. Process B: visited, core point, add to Cluster 1, queue B's unvisited neighbors
  6. Process C: visited, core point, add to Cluster 1, queue C's unvisited neighbors
  7. Process D: visited, core point, add to Cluster 1
  8. Queue is empty → Cluster 1 complete: {A, B, C, D}

Iteration 2: Visit point E (first unvisited point)

  1. Mark E as visited
  2. E is a core point → Create Cluster 2
  3. Add E to Cluster 2: {E}
  4. Queue E's neighbors for expansion: {F, G, H}
  5. Process F, G, H similarly
  6. Cluster 2 complete: {E, F, G, H}

Iteration 3: Visit point I (last unvisited point)

  1. Mark I as visited
  2. I is NOT a core point (only 1 neighbor)
  3. I is not in any core point's neighborhood
  4. Mark I as noise

Final Result

ClusterMembersDescription
Cluster 1{A, B, C, D}Lower-left dense region
Cluster 2{E, F, G, H}Upper-right dense region
Noise{I}Isolated point

DBSCAN successfully identified two distinct clusters based on density while correctly classifying the isolated point I as noise. The algorithm never required us to specify the number of clusters; it discovered them naturally by following density connections.

Key Insight

Notice that every point in both clusters is a core point. In more complex scenarios, clusters often contain a mix of core points (in dense interiors) and border points (on cluster edges). Border points are reachable from core points but don't have enough neighbors themselves to be core points.