DBSCAN Clustering
Learn how a computer finds groups by looking for dense areas of data. Unlike K-Means, DBSCAN does not ask you to choose the number of clusters first—and it can identify points that do not belong to any group.
The big idea
Dense areas become clusters. Isolated points can become noise.
DBSCAN looks around each data point. If enough points are close together, it can start or grow a cluster. Points in sparse areas may be marked as noise.
You control two settings: eps, the neighbourhood radius, and min_samples, the minimum number of nearby points needed to count an area as dense.
Three types of points
These roles decide how a cluster grows.
Has at least min_samples points in its eps neighbourhood, counting itself.
Has too few neighbours to be core, but lies inside a core point's neighbourhood.
Is not reached by any cluster. It may be called an outlier.
1. How does DBSCAN work?
It checks neighbourhoods and grows a cluster from core points.
The process
Pick a point that has not been checked yet.
Find all points within the chosen radius. This set is the point's neighbourhood.
If the neighbourhood is large enough, the point is a core point and can start a cluster.
Add nearby points. Any new core points can bring in their own neighbours.
If a point is not part of a cluster after the search, it stays noise.
A simple way to picture it
Imagine people standing in a park. Draw a small circle around each person. If enough people are inside a circle, that person is in a crowded area. Nearby crowded areas can join to form one group.
2. Live DBSCAN playground
Change eps and min_samples to see clusters, border points, and noise change.
Density map
Filled dots are core points, hollow dots are border points, and crosses are noise.
The dashed circle shows the eps neighbourhood of the point currently being examined. Axes show two numerical features.
3. The mathematics behind DBSCAN
DBSCAN uses distance and a simple count of nearby points.
Distance between two points
For two points in a two-dimensional space, Euclidean distance is:
A point is in the eps neighbourhood when its distance from the centre point is less than or equal to eps.
Neighbourhood and core point
The eps-neighbourhood of point p contains all points no farther than eps from p.
If the neighbourhood count reaches min_samples, p is a core point. The point p itself is included in the count.
How the two settings work together
| Setting | What it controls | If you increase it |
|---|---|---|
| eps | How far DBSCAN looks around each point. | More points can become neighbours; clusters may grow or join. |
| min_samples | How many nearby points are needed for a dense area. | It becomes harder to form a cluster; more points may be noise. |
4. Worked example: finding a busy area
Use a small set of points to understand the three point types.
Example points
Imagine these locations on a map. Let eps = 1.5 and min_samples = 3. The point itself counts as one neighbour.
| Point | X | Y | Nearby count |
|---|---|---|---|
| A | 1.0 | 1.0 | 3 |
| B | 1.8 | 1.0 | 4 |
| C | 1.2 | 1.8 | 3 |
| D | 3.0 | 1.0 | 2 |
| E | 8.5 | 8.5 | 1 |
What DBSCAN decides
A, B, and C each have at least three points in their eps neighbourhood. They are core points and form a cluster.
D has too few neighbours to be a core point, but it is close enough to a core point to join as a border point.
E is far from the group and has too few neighbours. It is marked as noise.
5. How do we choose eps and min_samples?
These settings strongly affect the clusters DBSCAN discovers.
The k-distance idea
A common way to choose eps is to measure the distance to each point's k-th nearest neighbour, where k is often related to min_samples. Sort those distances and plot them. A bend in the curve can give a useful eps starting point.
Illustrative curve only. Always check the data and the result after choosing a value.
Practical tips
Distance is affected by feature size. Standardise features when their ranges differ a lot.
A very small radius can label many points as noise. A very large radius can join separate groups.
A larger value asks for stronger local density before a cluster can grow.
Check whether the groups make sense for the real problem.
eps too small
Neighbourhoods may be too tiny. Many points may be labelled as noise, and groups may split.
eps too large
Separate groups may join together. Even distant areas may become connected through chains of points.
6. DBSCAN in Python
Scikit-learn provides DBSCAN, which performs the neighbourhood search and cluster expansion.
Clustering with scikit-learn
Copy this example into a Python notebook.
# Install the libraries if needed:
# pip install numpy scikit-learn
import numpy as np
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler
# Example data: two dense groups and a few isolated points
X = np.array([
[1.0, 1.0], [1.2, 1.1], [0.9, 1.3],
[1.3, 0.8], [1.5, 1.2],
[7.0, 7.0], [7.2, 7.1], [6.8, 7.2],
[7.3, 6.8], [7.1, 7.4],
[4.5, 1.0], [9.0, 2.0]
])
# Scale features when their ranges are very different
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Create and fit the DBSCAN model
model = DBSCAN(eps=0.7, min_samples=3)
labels = model.fit_predict(X_scaled)
# Cluster labels: -1 means noise
print("Cluster labels:", labels)
print("Clusters found:", len(set(labels) - {-1}))
print("Noise points:", np.sum(labels == -1))
# Core sample row indices
print("Core sample indices:", model.core_sample_indices_)
A label such as 0 or 1 identifies a cluster. A label of -1 means DBSCAN marked that row as noise.
Useful Python terms
eps sets the neighbourhood radius. min_samples sets the density requirement. fit_predict() fits the model and returns labels. -1 marks noise. core_sample_indices_ gives the row indices of core points.
7. Where is DBSCAN used in real life?
It is useful when groups form dense areas and some points may be unusual.
Geographic hotspots
Find busy areas from location data, such as groups of reported events or service requests.
Outlier detection
Flag unusual records that do not sit near dense groups of similar records.
Image analysis
Group nearby image features or points when their positions form dense regions.
Movement patterns
Find common gathering areas or repeated location patterns from movement data.
Strengths and limitations
Know when DBSCAN is useful—and when it may struggle.
Strengths
- You do not need to choose the number of clusters.
- Can find curved and irregularly shaped groups.
- Can identify noise and outliers.
- Does not need starting cluster centres.
Limitations
- Choosing eps can be difficult.
- One eps value may not work well for groups with very different densities.
- Distance becomes harder to interpret with many features.
- Feature scaling can strongly affect the result.
DBSCAN vs K-Means
| Question | K-Means | DBSCAN |
|---|---|---|
| Choose cluster count first? | Yes, choose K. | No. |
| Find noise points? | Not as part of its standard clustering result. | Yes, labels them as -1. |
| Cluster shape | Works best for compact groups. | Can find irregular shapes. |
| Main settings | Number of clusters K. | eps and min_samples. |
8. Knowledge check
Answer one question at a time. Your score and explanations will appear after the final question.
Mini challenge
Use the playground. First increase eps, then increase min_samples. Can you explain why the number of clusters and noise points change?