文章目录
- sklearn版本
- sklearn.DBSCAN使用实例
- 实例1 Iris,鸢尾花数据集(UC Irvine Machine Learning Repository)
- 实例2 RandomData,随机生成的月牙形数据(非凸数据集)
- sklearn.DBSCAN解析
- DBSCAN类
- 构造函数__init__(),调用格式iris_dbscan = DBSCAN(eps = 0.3, min_samples = 5)
- iris_dbscan.fit(X)
- dbscan_inner(is_core, neighborhoods, labels)
- iris_dbscan.fit_predict(X)
- 参考
sklearn版本
- scikit-learn 0.23.2
sklearn.DBSCAN使用实例
-
实例1 Iris,鸢尾花数据集(UC Irvine Machine Learning Repository)
Iris可以从
sklearn
包内部导入,常常用作分类的训练数据集。这里为了方便展示聚类效果(二维在平面图中效果明显),选取Iris的前两个维度作为聚类依据。上一篇文章(sklearn.KMeans解析)中,我们已经用KMeans做过这个实验,这里用DBSCAN来做。代码参见Iris_DBSCAN.py。左图为全部数据点,右图为DBSCAN聚类(eps = 0.3, min_samples = 5
)结果,不同颜色代表不同簇。参数是经过调整的,以接近KMeans的结果,因为个人认为这个数据集聚类还是KMeans效果略好。
-
实例2 RandomData,随机生成的月牙形数据(非凸数据集)
这个例子体现出了DBSCAN在处理非凸数据集时的优势。代码参见RandomData_DBSCAN.py。最左图为随机生成的月牙形数据点。剩余两张图分别为KMeans聚类(
n_cluseters=2
)结果和DBSCAN聚类(eps = 0.1, min_samples = 10
)结果。
sklearn.DBSCAN解析
本节分析sklearn.DBSCAN
中的主要函数。