DBSCAN (Density-Based Spatial Clustering) › Worked Example
Machine Learning from ScratchAll chapters 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:
| Point | Coordinates | Visual 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

Step 1: Calculate Distances
First, we need to find which points are neighbors. Using Euclidean distance:
Let's calculate key distances for point A:
- ✓ (within eps)
- ✓ (within eps)
- ✓ (within eps)
- ✗ (too far)
- ✗ (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:
| Point | Neighborhood | Size |
|---|---|---|
| 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:
With min_samples = 3:
| Point | Neighborhood Size | ≥ 3? | Classification |
|---|---|---|---|
| A | 4 | ✓ | Core point |
| B | 4 | ✓ | Core point |
| C | 4 | ✓ | Core point |
| D | 4 | ✓ | Core point |
| E | 4 | ✓ | Core point |
| F | 4 | ✓ | Core point |
| G | 4 | ✓ | Core point |
| H | 4 | ✓ | Core point |
| I | 1 | ✗ | 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
- Mark A as visited
- A is a core point → Create Cluster 1
- Add A to Cluster 1: {A}
- Queue A's neighbors for expansion: {B, C, D}
- Process B: visited, core point, add to Cluster 1, queue B's unvisited neighbors
- Process C: visited, core point, add to Cluster 1, queue C's unvisited neighbors
- Process D: visited, core point, add to Cluster 1
- Queue is empty → Cluster 1 complete: {A, B, C, D}
Iteration 2: Visit point E (first unvisited point)
- Mark E as visited
- E is a core point → Create Cluster 2
- Add E to Cluster 2: {E}
- Queue E's neighbors for expansion: {F, G, H}
- Process F, G, H similarly
- Cluster 2 complete: {E, F, G, H}
Iteration 3: Visit point I (last unvisited point)
- Mark I as visited
- I is NOT a core point (only 1 neighbor)
- I is not in any core point's neighborhood
- Mark I as noise
Final Result
| Cluster | Members | Description |
|---|---|---|
| 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.
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.