基本思想
聚类是很多观察值紧密聚集在一起的区域,DBSCAN算法就是受这一点的启发而来的,它对于聚类的形状没有做任何假设。具体来说,DBSCAN算法有如下几步:
DBSCAN对象需要设置以下三个主要参数。
eps
:从一个观察值到另一个观察值得最远距离,超过这个距离将不再认为二者是邻居
min_samples
:最小限度的邻居数量,如果一个观察值在其周围小于eps距离的范围内有超过这个数量的邻居,就被认为是核心观察值
metric
:eps所用的距离度量,比如 minkowski
(闵可夫斯基距离)或者 euclidean
(欧式距离)。注意,如果使用闵可夫斯基距离,就可以用参数p设定闵可夫斯基中的幂次。
API学习
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
)
参数类型解释epsfloat, default=0.5邻域的距离阈值
min_samplesint, default=5样本点要成为核心对象所需要的邻域的样本数阈值metricstr, or callable, default=’euclidean’度量方式,默认为欧式距离,可以使用的距离度量参数有’euclidean’/’manhattan’/’chebyshev’/’minkowski’/’wminkowski’/’seuclidean’/’mahalanobis’metric_paramsdict, default=None度量函数的其他关键字参数algorithm{‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, default=’auto’近邻算法求解方式leaf_sizeint, default=30使用’ball_tree’或’kd_tree’时停止建子树的叶子节点数量的阈值pfloat, default=None只用于闵可夫斯基距离和带权重闵可夫斯基距离中p值的选择,p=1为曼哈顿距离,p=2为欧式距离。n_jobsint, default=NoneCPU并行数,值为-1时使用所有CPU运算 属性类型解释core_sample_indices_ndarray of shape(n_core_samples,)包含每个核心实例的索引components_ndarray of shape(n_core_samples, n_features)核心实例本身labels_ndarray of shape(n_samples)分类结果n_features_in_int拟合期间的特征个数feature_names_in_ndarray of shape(n_features_in_,)拟合期间的特征名称 方法说明fit(X[, y, sample_weight])Perform DBSCAN clustering from features, or distance matrix.fit_predict(X[, y, sample_weight])Compute clusters from a data or distance matrix and predict labels.get_params([deep])Get parameters for this estimator.set_params(**params)Set the parameters of this estimator.
代码示例
>>> from sklearn.cluster import DBSCAN
>>> import numpy as np
>>> X = np.array([[1, 2], [2, 2], [2, 3],
... [8, 7], [8, 8], [25, 80]])
>>> clustering = DBSCAN(eps=3, min_samples=2).fit(X)
>>> clustering.labels_
array([ 0, 0, 0, 1, 1, -1])
>>> clustering
DBSCAN(eps=3, min_samples=2)
参考文献
[1] 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
[2] 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.
Original: https://blog.csdn.net/qq_45448654/article/details/120850612
Author: GallopZhang
Title: sklearn聚类算法之DBSCAN
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/614156/
转载文章受原作者版权保护。转载请注明原作者出处!