機器學習-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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章