python聚类算法之凝聚层次聚类实例分析

本文实例讲述了python聚类算法之凝聚层次聚类。分享给大家供大家参考,具体如下:

凝聚层次聚类:所谓凝聚的,指的是该算法初始时,将每个点作为一个簇,每一步合并两个最接近的簇。另外即使到最后,对于噪音点或是离群点也往往还是各占一簇的,除非过度合并。对于这里的“最接近”,有下面三种定义。我在实现是使用了min,该方法在合并时,只要依次取当前最近的点对,如果这个点对当前不在一个簇中,将所在的两个簇合并就行:

单链(min):定义簇的邻近度为不同两个簇的两个最近的点之间的距离。
全链(max):定义簇的邻近度为不同两个簇的两个最远的点之间的距离。
组平均:定义簇的邻近度为取自两个不同簇的所有点对邻近度的平均值。

# scoding=utf-8
# agglomerative hierarchical clustering(ahc)
import pylab as pl
from operator import itemgetter
from collections import ordereddict,counter
points = [[int(eachpoint.split(‘#’)[0]), int(eachpoint.split(‘#’)[1])] for eachpoint in open(“points”,”r”)]
# 初始时每个点指派为单独一簇
groups = [idx for idx in range(len(points))]
# 计算每个点对之间的距离
disp2p = {}
for idx1,point1 in enumerate(points):
for idx2,point2 in enumerate(points):
if (idx1 < idx2): distance = pow(abs(point1[0]-point2[0]),2) + pow(abs(point1[1]-point2[1]),2) disp2p[str(idx1)+"#"+str(idx2)] = distance # 按距离降序将各个点对排序 disp2p = ordereddict(sorted(disp2p.iteritems(), key=itemgetter(1), reverse=true)) # 当前有的簇个数 groupnum = len(groups) # 过分合并会带入噪音点的影响,当簇数减为finalgroupnum时,停止合并 finalgroupnum = int(groupnum*0.1) while groupnum > finalgroupnum:
# 选取下一个距离最近的点对
twopoins,distance = disp2p.popitem()
pointa = int(twopoins.split(‘#’)[0])
pointb = int(twopoins.split(‘#’)[1])
pointagroup = groups[pointa]
pointbgroup = groups[pointb]
# 当前距离最近两点若不在同一簇中,将点b所在的簇中的所有点合并到点a所在的簇中,此时当前簇数减1
if(pointagroup != pointbgroup):
for idx in range(len(groups)):
if groups[idx] == pointbgroup:
groups[idx] = pointagroup
groupnum -= 1
# 选取规模最大的3个簇,其他簇归为噪音点
wantgroupnum = 3
finalgroup = counter(groups).most_common(wantgroupnum)
finalgroup = [onecount[0] for onecount in finalgroup]
droppoints = [points[idx] for idx in range(len(points)) if groups[idx] not in finalgroup]
# 打印规模最大的3个簇中的点
group1 = [points[idx] for idx in xrange(len(points)) if groups[idx]==finalgroup[0]]
group2 = [points[idx] for idx in xrange(len(points)) if groups[idx]==finalgroup[1]]
group3 = [points[idx] for idx in xrange(len(points)) if groups[idx]==finalgroup[2]]
pl.plot([eachpoint[0] for eachpoint in group1], [eachpoint[1] for eachpoint in group1], ‘or’)
pl.plot([eachpoint[0] for eachpoint in group2], [eachpoint[1] for eachpoint in group2], ‘oy’)
pl.plot([eachpoint[0] for eachpoint in group3], [eachpoint[1] for eachpoint in group3], ‘og’)
# 打印噪音点,黑色
pl.plot([eachpoint[0] for eachpoint in droppoints], [eachpoint[1] for eachpoint in droppoints], ‘ok’)
pl.show()

运行效果截图如下:

希望本文所述对大家python程序设计有所帮助。

Posted in 未分类

发表评论