本文实例讲述了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程序设计有所帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
