机器学习-DBSCAN算法

Section I: Brief Introduction on DBSCAN

Density-based Spatial Clustering of Applications with Noise (DBSCAN), which does not make assumptions about spherical clusters like k-means, nor does it partition the dataset into hierarchies that requires a manual cut-off point. As its name implies, density-based clustering assigns cluster labels based on dense regions of points. In DBSCAN, the notion of density is defined as the number of points within a specified radius.

According to the DBSCAN algorithm, a special label is assigned to each sample point using the following criteria:

  • A point is cinsidered a core point if at least a specified number of neighboring points fall within the specified radius
  • A border point is a point that has fewer neighbors than the specified number within a specified threshold, but lies within the threshold radius of a core point
  • All other points that are neither core nor border points are considered as noise points.

After labeling the points as core, border, or noise, the DBSCAN algorithm can be summarized in two simple steps:

  • Step 1: Form a separate cluster for each core point or connected group of core points (core points are connected if they are no farther away than threshold)
  • Step 2: Assign each border point to the cluster of its corresponding core point.

One advantage: for DBSCAN, it is different from K-Means and algorithm and hierarchical clustering in that it doesn’t necessarily assign each point to a cluster but is capable of removing noising points.

FROM
Sebastian Raschka, Vahid Mirjalili. Python机器学习第二版. 南京:东南大学出版社,2018.

第一部分:初始数据分布

代码

from sklearn import datasets
import matplotlib.pyplot as plt

plt.rcParams['figure.dpi']=200
plt.rcParams['savefig.dpi']=200
font = {'family': 'Times New Roman',
        'weight': 'light'}
plt.rc("font", **font)

#Section 1: Load data and visualize it
X,y=datasets.make_moons(n_samples=200,
                        noise=0.05,
                        random_state=0)
plt.scatter(X[:,0],X[:,1])
plt.savefig('./fig1.png')

结果
在这里插入图片描述

第二部分:K-Means++和Agglomerative聚类算法

代码

#Section 2: Construct KMeans++ and Agglomerative clusters
f,ax=plt.subplots(1,2,figsize=(8,3))

#Section 2.1: KMeans Cluster
from sklearn.cluster import KMeans

km=KMeans(n_clusters=2,random_state=0,init='k-means++')
y_km=km.fit_predict(X)
ax[0].scatter(X[y_km==0,0],X[y_km==0,1],
              c='lightblue',
              edgecolor='black',
              marker='o',
              s=40,
              label='Cluster 1')
ax[0].scatter(X[y_km==1,0],X[y_km==1,1],
              c='red',
              edgecolor='black',
              marker='s',
              s=40,
              label='Cluster 2')
ax[0].set_title("K-Means Clustering")
ax[0].legend(loc='best')

#Section 2.2: Agglomerative Cluster
from sklearn.cluster import AgglomerativeClustering

ac=AgglomerativeClustering(n_clusters=2,
                           affinity='euclidean',
                           linkage='complete')
y_ac=ac.fit_predict(X)
ax[1].scatter(X[y_ac==0,0],
              X[y_ac==0,1],
              c='lightblue',
              edgecolor='black',
              marker='o',
              s=40,
              label='Cluster 1')
ax[1].scatter(X[y_ac==1,0],
              X[y_ac==1,1],
              c='red',
              edgecolor='black',
              marker='s',
              s=40,
              label='Cluster 2')
ax[1].set_title("Agglomerative Clustering")
ax[1].legend(loc='best')
plt.savefig('./fig2.png')
plt.show()

结果
在这里插入图片描述

第三部分:DBSCAN聚类算法

代码

#Section 3: DBSCAN Cluster
from sklearn.cluster import DBSCAN

db=DBSCAN(eps=0.2,
          min_samples=5,
          metric='euclidean')
y_db=db.fit_predict(X)
plt.scatter(X[y_db==0,0],
            X[y_db==0,1],
            c='lightblue',
            edgecolor='black',
            marker='o',
            s=40,
            label='Cluster 1')
plt.scatter(X[y_db==1,0],
            X[y_db==1,1],
            c='red',
            edgecolor='black',
            marker='s',
            s=40,
            label='Cluster 2')
plt.title('Density-based Spatial Clustering of Application With Noise')
plt.legend(loc='best')
plt.savefig('./fig3.png')
plt.show()

结果
在这里插入图片描述
显然,对比上述两大类聚类算法,可以得知基于密度的DBSCAN聚类算法,聚类效果更佳。

参考文献
Sebastian Raschka, Vahid Mirjalili. Python机器学习第二版. 南京:东南大学出版社,2018.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值