Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 41

DBSCAN

Find dense neighborhoods and mark isolated points as noise, without choosing the number of clusters first. One example is on this page.

Intermediate12 min
DBSCAN MahiDev

What You Will Learn

  • Idea
  • Example
  • When to use it

Idea

DBSCAN looks at a radius, eps. A point with enough neighbors is a core point. Nearby core points join the same cluster. A point with no dense neighborhood is noise, labelled -1. The number of clusters is a result, not an input.

Example

python
import numpy as np
from sklearn.cluster import DBSCAN

X = np.array([[1, 1], [1.2, 0.8], [0.8, 1.1], [8, 8], [8.2, 7.9], [20, 20]], dtype=float)
labels = DBSCAN(eps=0.5, min_samples=2).fit_predict(X)
print(labels.tolist())

Output:

text
[0, 0, 0, 1, 1, -1]

When to use it

  • Use it when clusters have irregular shapes and some points should stay ungrouped.
  • Scale the inputs. eps is a distance.
  • A label of -1 means noise, not a third cluster you must keep.

Loading playground…