Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

DBSCAN

DBSCAN is a density-based clustering estimator that expands clusters from dense core samples, supports arbitrary-shaped groups, and marks low-density samples as noise. It does not require the number of clusters to be specified in advance.

How it works

DBSCAN groups samples by neighborhood density, growing clusters from core samples and labeling low-density samples as noise. It accepts feature arrays or distance matrices and does not require the number of clusters beforehand.

Syntax

class sklearn.cluster.DBSCAN(eps=0.5, *, min_samples=5, metric='euclidean', metric_params=None, algorithm='auto', leaf_size=30, p=None, n_jobs=None)

Parameters

NameTypeDescription
epsfloatNeighborhood distance threshold; default 0.5.
min_samplesintRequired neighborhood sample count or total weight for a core point; default 5. The point itself is included.
metricstr or callableDistance metric for feature-array instances; default 'euclidean'.
metric_paramsdict or NoneAdditional keyword arguments for the metric; default None.
algorithm{'auto', 'ball_tree', 'kd_tree', 'brute'}Nearest-neighbor search algorithm; default 'auto'.
leaf_sizeintLeaf size passed to the nearest-neighbor search; default 30.
pfloat or NoneMinkowski distance power parameter; default None.
n_jobsint or NoneNumber of parallel jobs for neighbor searches; default None.

Returns

Returns the fitted DBSCAN estimator; after fitting, labels_ contains cluster labels and core_sample_indices_ identifies core samples.

Key points

  • The algorithm starts with sufficiently dense core samples and expands neighboring points into their clusters.
  • It is well suited to datasets whose groups have broadly comparable density.
  • Cluster geometry need not be spherical or otherwise predefined.
  • You can fit DBSCAN without supplying a target cluster count.
  • Samples that do not belong to dense regions can be identified as noise.
  • min_samples defines the neighborhood support needed for core-point status, including weighted samples.

Common mistakes

  • Do not interpret eps as the maximum pairwise distance allowed inside a completed cluster.
  • Large eps values combined with low min_samples can cause quadratic worst-case memory use.

Example: Cluster points and identify noise

from sklearn.cluster import DBSCAN
import numpy as np

X = np.array([
    [1.0, 1.0], [1.1, 1.0], [0.9, 1.1],
    [8.0, 8.0], [8.1, 8.0], [8.0, 8.1],
    [25.0, 80.0],
])

model = DBSCAN(eps=0.25, min_samples=3)
labels = model.fit_predict(X)
print(labels)  # -1 denotes noise

Fit DBSCAN to two compact groups and an isolated point; the isolated sample is labeled as noise.

When to use it

Arbitrary-shaped clustering

Groups have irregular geometry rather than spherical structure.

Outlier and noise detection

The dataset may contain isolated observations that should be separated from groups.

Unsupervised cluster discovery

The desired number of groups is unknown before fitting.

Interview questions

QHow does DBSCAN form clusters?

DBSCAN identifies dense regions by finding core samples and expanding clusters from them. Samples in low-density regions can be marked as noise, represented by label -1.

Follow-up: How does DBSCAN differ from K-means? DBSCAN does not require the number of clusters in advance and can discover clusters with arbitrary shapes.

QWhat do eps and min_samples control?

eps sets the neighborhood distance threshold, while min_samples sets the number of samples or total weight required for a point to be core. The point itself counts toward min_samples.

Follow-up: What happens when min_samples increases? DBSCAN requires denser neighborhoods, so sparse groups are less likely to qualify as clusters.

QWhat inputs and distance options does DBSCAN support?

The estimator accepts a feature array or a distance matrix. Its metric parameter determines how distances are calculated, and algorithm selects the nearest-neighbor search strategy.

Follow-up: Which resource concern should guide parameter selection? The implementation can require quadratic memory when eps is large and min_samples is low.

  • sklearn.cluster.dbscan
  • sklearn.cluster.KMeans
  • sklearn.cluster.OPTICS
  • sklearn.cluster.SpectralClustering
  • sklearn.neighbors.NearestNeighbors

Source: DBSCAN — scikit-learn 1.9.1 documentation. Checked 2026-10-15.