Python基于聚类算法实现密度聚类(DBSCAN)计算【测试可用】
程序员文章站
2022-03-07 08:11:53
本文实例讲述了python基于聚类算法实现密度聚类(dbscan)计算。分享给大家供大家参考,具体如下:
算法思想
基于密度的聚类算法从样本密度的角度考察样本之间的可连...
本文实例讲述了python基于聚类算法实现密度聚类(dbscan)计算。分享给大家供大家参考,具体如下:
算法思想
基于密度的聚类算法从样本密度的角度考察样本之间的可连接性,并基于可连接样本不断扩展聚类簇得到最终结果。
几个必要概念:
ε-邻域:对于样本集中的xj, 它的ε-邻域为样本集中与它距离小于ε的样本所构成的集合。
核心对象:若xj的ε-邻域中至少包含minpts个样本,则xj为一个核心对象。
密度直达:若xj位于xi的ε-邻域中,且xi为核心对象,则xj由xi密度直达。
密度可达:若样本序列p1, p2, ……, pn。pi+1由pi密度直达,则p1由pn密度可达。
大致思想如下:
1. 初始化核心对象集合t为空,遍历一遍样本集d中所有的样本,计算每个样本点的ε-邻域中包含样本的个数,如果个数大于等于minpts,则将该样本点加入到核心对象集合中。初始化聚类簇数k = 0, 初始化未访问样本集和为p = d。
2. 当t集合中存在样本时执行如下步骤:
- 2.1记录当前未访问集合p_old = p
- 2.2从t中随机选一个核心对象o,初始化一个队列q = [o]
- 2.3p = p-o(从t中删除o)
- 2.4当q中存在样本时执行:
- 2.4.1取出队列中的首个样本q
- 2.4.2计算q的ε-邻域中包含样本的个数,如果大于等于minpts,则令s为q的ε-邻域与p的交集,
q = q+s, p = p-s
- 2.5 k = k + 1,生成聚类簇为ck = p_old - p
- 2.6 t = t - ck
3. 划分为c= {c1, c2, ……, ck}
python代码实现
#-*- coding:utf-8 -*- import math import numpy as np import pylab as pl #数据集:每三个是一组分别是西瓜的编号,密度,含糖量 data = """ 1,0.697,0.46,2,0.774,0.376,3,0.634,0.264,4,0.608,0.318,5,0.556,0.215, 6,0.403,0.237,7,0.481,0.149,8,0.437,0.211,9,0.666,0.091,10,0.243,0.267, 11,0.245,0.057,12,0.343,0.099,13,0.639,0.161,14,0.657,0.198,15,0.36,0.37, 16,0.593,0.042,17,0.719,0.103,18,0.359,0.188,19,0.339,0.241,20,0.282,0.257, 21,0.748,0.232,22,0.714,0.346,23,0.483,0.312,24,0.478,0.437,25,0.525,0.369, 26,0.751,0.489,27,0.532,0.472,28,0.473,0.376,29,0.725,0.445,30,0.446,0.459""" #数据处理 dataset是30个样本(密度,含糖量)的列表 a = data.split(',') dataset = [(float(a[i]), float(a[i+1])) for i in range(1, len(a)-1, 3)] #计算欧几里得距离,a,b分别为两个元组 def dist(a, b): return math.sqrt(math.pow(a[0]-b[0], 2)+math.pow(a[1]-b[1], 2)) #算法模型 def dbscan(d, e, minpts): #初始化核心对象集合t,聚类个数k,聚类集合c, 未访问集合p, t = set(); k = 0; c = []; p = set(d) for d in d: if len([ i for i in d if dist(d, i) <= e]) >= minpts: t.add(d) #开始聚类 while len(t): p_old = p o = list(t)[np.random.randint(0, len(t))] p = p - set(o) q = []; q.append(o) while len(q): q = q[0] nq = [i for i in d if dist(q, i) <= e] if len(nq) >= minpts: s = p & set(nq) q += (list(s)) p = p - s q.remove(q) k += 1 ck = list(p_old - p) t = t - set(ck) c.append(ck) return c #画图 def draw(c): colvalue = ['r', 'y', 'g', 'b', 'c', 'k', 'm'] for i in range(len(c)): coo_x = [] #x坐标列表 coo_y = [] #y坐标列表 for j in range(len(c[i])): coo_x.append(c[i][j][0]) coo_y.append(c[i][j][1]) pl.scatter(coo_x, coo_y, marker='x', color=colvalue[i%len(colvalue)], label=i) pl.legend(loc='upper right') pl.show() c = dbscan(dataset, 0.11, 5) draw(c)
本机测试运行结果图:
更多关于python相关内容感兴趣的读者可查看本站专题:《python数学运算技巧总结》、《python数据结构与算法教程》、《python函数使用技巧总结》、《python字符串操作技巧汇总》及《python入门与进阶经典教程》
希望本文所述对大家python程序设计有所帮助。