dbscan
- sklearn.cluster.dbscan(X, eps=0.5, *, min_samples=5, metric='minkowski', metric_params=None, algorithm='auto', leaf_size=30, , ,
Perform DBSCAN clustering from vector array or distance matrix.
This function is a wrapper around DBSCAN, suitable for
quick, standalone clustering tasks. For estimator-based workflows, where
estimator attributes or pipeline integration is required, prefer
DBSCAN.
DBSCAN (Density-Based Spatial Clustering of Applications with Noise) is a density-based clustering algorithm that groups together points that are closely packed while marking points in low-density regions as outliers.
Read more in the User Guide.
A feature array, or array of distances between samples if
metric='precomputed'. When using precomputed distances, X must
be a square symmetric matrix.
The maximum distance between two samples for one to be considered as in the neighborhood of the other. This is not a maximum bound on the distances of points within a cluster. This is the most important DBSCAN parameter to choose appropriately for your data set and distance function. Smaller values result in more clusters, while larger values result in fewer, larger clusters.
The number of samples (or total weight) in a neighborhood for a point to be considered as a core point. This includes the point itself. Higher values yield fewer, denser clusters, while lower values yield more, sparser clusters.
The metric to use when calculating distance between instances in a
feature array. If metric is a string or callable, it must be one of
the options allowed by sklearn.metrics.pairwise_distances for
its metric parameter.
If metric is “precomputed”, X is assumed to be a distance matrix and
must be square during fit.
X may be a sparse graph,
in which case only “nonzero” elements may be considered neighbors.
Additional keyword arguments for the metric function.
The algorithm to be used by the NearestNeighbors module
to compute pointwise distances and find nearest neighbors.
‘auto’ will attempt to decide the most appropriate algorithm
based on the values passed to fit method.
See NearestNeighbors documentation for
details.
Leaf size passed to BallTree or cKDTree. This can affect the speed of the construction and query, as well as the memory required to store the tree. The optimal value depends on the nature of the problem. Generally, smaller leaf sizes lead to faster queries but slower construction.
Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used. This parameter is expected to be positive.
Weight of each sample, such that a sample with a weight of at least
min_samples is by itself a core sample; a sample with negative
weight may inhibit its eps-neighbor from being core.
Note that weights are absolute, and default to 1.
The number of parallel jobs to run for neighbors search. None means
1 unless in a joblib.parallel_backend context. -1 means
using all processors. See Glossary for more details.
If precomputed distances are used, parallel execution is not available
and thus n_jobs will have no effect.
Indices of core samples.
Cluster labels for each point. Noisy samples are given the label -1. Non-negative integers indicate cluster membership.
For an example, see Demo of DBSCAN clustering algorithm.
This implementation bulk-computes all neighborhood queries, which increases
the memory complexity to O(n.d) where d is the average number of neighbors,
while original DBSCAN had memory complexity O(n). It may attract a higher
memory complexity when querying these nearest neighborhoods, depending
on the algorithm.
One way to avoid the query complexity is to pre-compute sparse
neighborhoods in chunks using
NearestNeighbors.radius_neighbors_graph with
mode='distance', then using metric='precomputed' here.
Another way to reduce memory and computation time is to remove
(near-)duplicate points and use sample_weight instead.
OPTICS provides a similar clustering with lower
memory usage.
Ester, M., H. P. Kriegel, J. Sander, and X. Xu, “A Density-Based Algorithm for Discovering Clusters in Large Spatial Databases with Noise”. In: Proceedings of the 2nd International Conference on Knowledge Discovery and Data Mining, Portland, OR, AAAI Press, pp. 226-231. 1996
Schubert, E., Sander, J., Ester, M., Kriegel, H. P., & Xu, X. (2017). “DBSCAN revisited, revisited: why and how you should (still) use DBSCAN.” ACM Transactions on Database Systems (TODS), 42(3), 19.
>>> from sklearn.cluster import dbscan
>>> X = [[1, 2], [2, 2], [2, 3], [8, 7], [8, 8], [25,