Arbitrary-shaped clustering
Groups have irregular geometry rather than spherical structure.
Scikit-Learn · Class
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.
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.
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)| Name | Type | Description |
|---|---|---|
eps | float | Neighborhood distance threshold; default 0.5. |
min_samples | int | Required neighborhood sample count or total weight for a core point; default 5. The point itself is included. |
metric | str or callable | Distance metric for feature-array instances; default 'euclidean'. |
metric_params | dict or None | Additional keyword arguments for the metric; default None. |
algorithm | {'auto', 'ball_tree', 'kd_tree', 'brute'} | Nearest-neighbor search algorithm; default 'auto'. |
leaf_size | int | Leaf size passed to the nearest-neighbor search; default 30. |
p | float or None | Minkowski distance power parameter; default None. |
n_jobs | int or None | Number of parallel jobs for neighbor searches; default None. |
Returns the fitted DBSCAN estimator; after fitting, labels_ contains cluster labels and core_sample_indices_ identifies core samples.
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.
Groups have irregular geometry rather than spherical structure.
The dataset may contain isolated observations that should be separated from groups.
The desired number of groups is unknown before fitting.
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.
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.
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.dbscansklearn.cluster.KMeanssklearn.cluster.OPTICSsklearn.cluster.SpectralClusteringsklearn.neighbors.NearestNeighbors