機器學習 聚類 密度聚類演算法

2021-08-22 08:18:16 字數 4270 閱讀 5314

一,介紹

密度聚類演算法有多種,我們這裡主要介紹一種著名的密度聚類演算法:dbscan。

首先,我們通過下圖了解幾個概念:

(1)鄰域,與中心x距離不超過ε距離,如上圖紅色虛線圈

(2)核心物件,確定聚類的初始點,如上圖的x1

(3)密度直達,在聚類核心物件鄰域內的點,如上圖x2由x1密度直達

(4)密度可達,可以通過幾次密度直達後的點,如x3有x1密度可達

(5)密度相連,兩個點可由一點通過密度可達方式分別到達,如x3和x4密度相連

演算法流程

1,確定初始引數,鄰域ε的值和核心物件密度直達最小值minpts的值;

2,根據初始引數,計算核心物件集合;

3,隨機挑選乙個核心物件作為種子,生成聚類族;

4,把包含在聚類族中的核心物件集合去除;

5,核心物件為空時,聚類結束。

二,**實現資料

0.697   0.46

0.774 0.376

0.634 0.264

0.608 0.318

0.556 0.215

0.403 0.237

0.481 0.149

0.437 0.211

0.666 0.091

0.243 0.267

0.245 0.057

0.343 0.099

0.639 0.161

0.657 0.198

0.36 0.37

0.593 0.042

0.719 0.103

0.359 0.188

0.339 0.241

0.282 0.257

0.748 0.232

0.714 0.346

0.483 0.312

0.478 0.437

0.525 0.369

0.751 0.489

0.532 0.472

0.473 0.376

0.725 0.445

0.446 0.459

python**:

import numpy as np

import math

import matplotlib.pyplot as plt

unclassified = false

noise = 0

# 讀取資料

def loaddataset(filename):

fr = open(filename)

numberoflines = len(fr.readlines())

returnmat = np.zeros((numberoflines, 2))

classlabelvector = ['密度','含糖率']

fr = open(filename)

index = 0

for line in fr.readlines():

line = line.strip().split('\t')

returnmat[index, :] = line[0:2]

index += 1

return returnmat, classlabelvector

# 計算歐氏距離

def dist(a, b):

return math.sqrt(np.power(a - b, 2).sum())

# 計算是否在鄰域內

def eps_neighbor(a, b, eps):

return dist(a, b) < eps

# 獲取鄰域內的點

def region_query(data, pointid, eps):

npoints = data.shape[1]

seeds =

for i in range(npoints):

if eps_neighbor(data[:, pointid], data[:, i], eps):

return seeds

# 分類結果

def expand_cluster(data, clusterresult, pointid, clusterid, eps, minpts):

seeds = region_query(data, pointid, eps)

if len(seeds) < minpts: # 不滿足minpts條件的為雜訊點

clusterresult[pointid] = noise

return false

else:

clusterresult[pointid] = clusterid # 劃分到該簇

for seedid in seeds:

clusterresult[seedid] = clusterid

while len(seeds) > 0: # 持續擴張

currentpoint = seeds[0]

queryresults = region_query(data, currentpoint, eps)

if len(queryresults) >= minpts:

for i in range(len(queryresults)):

resultpoint = queryresults[i]

if clusterresult[resultpoint] == unclassified:

clusterresult[resultpoint] = clusterid

elif clusterresult[resultpoint] == noise:

clusterresult[resultpoint] = clusterid

seeds = seeds[1:]

return true

# dbsanc分類,data--資料;eps--範圍;minpts--最小點

def dbscan(data, eps=0.11, minpts=5):

clusterid = 1

npoints = data.shape[1]

clusterresult = [unclassified] * npoints

for pointid in range(npoints):

point = data[:, pointid]

if clusterresult[pointid] == unclassified:

if expand_cluster(data, clusterresult, pointid, clusterid, eps, minpts):

clusterid = clusterid + 1

return clusterresult, clusterid - 1

def plotfeature(data, clusters, clusternum):

matclusters = np.mat(clusters).transpose()

fig = plt.figure()

scattercolors = ['black', 'blue', 'green', 'yellow', 'red', 'purple', 'orange', 'brown']

ax = fig.add_subplot(111)

for i in range(clusternum + 1):

colorsytle = scattercolors[i % len(scattercolors)]

subcluster = data[:, np.nonzero(matclusters[:, 0].a == i)]

ax.scatter(subcluster[0, :].flatten().a[0], subcluster[1, :].flatten().a[0], c=colorsytle, s=50)

plt.show()

if __name__=="__main__":

returnmat,classlabelvector = loaddataset('watermelon4.0.txt')

dataset = np.mat(returnmat).transpose()

clusters, clusternum = dbscan(dataset)

plotfeature(dataset, clusters, clusternum)

機器學習 密度聚類演算法 DBSCAN

1.密度聚類 基於密度的聚類演算法由於能夠發現任意形狀的聚類,識別資料集中的雜訊點,可伸縮性好等特點,在許多領域有著重要的應用。密度演算法概念 1 如果乙個資料點周圍足夠稠密,也就是以這個點為中心,給定半徑的鄰域內的資料點足夠多,密度大於密度閾值 使用者指定的引數minpts 則稱這個資料點為核心資...

聚類和EM演算法 密度聚類

模型原型 class sklearn.cluster.dbscan eps 0.5,min samples 5,metric euclidean algorithm auto leaf size 30,p none,random state none 引數 eps 引數,用於確定鄰域大小 min s...

聚類總結(中) 密度聚類

密度聚類是一種基於密度的聚類方法,基於密度的聚類方法的主要思想是尋找被低密度區域分離的高密度區域。密度度量 乙個點的區域性密度有不同的度量方式 1.畫個圈,數圈內其他點個數是一種方法。2.用圈內其他點和給點半徑的比值的高斯概率密度形式,對距離核心點距離近的給大權重。3.計算圈內距離最小的前n個點到核...