ML之HierarchicalClustering:自定义HierarchicalClustering层次聚类算法

目录

输出结果

实现代码


输出结果

更新……

实现代码

# -*- encoding=utf-8 -*-from numpy import *class cluster_node:  #定义cluster_node类,类似Java中的构造函数def __init__(self,vec,left=None,right=None,distance=0.0,id=None,count=1):self.left=left  self.right=rightself.vec=vecself.id=idself.distance=distanceself.count=count #only used for weighted average def L2dist(v1,v2):  return sqrt(sum((v1-v2)**2))def L1dist(v1,v2):  return sum(abs(v1-v2))def hcluster(features,distance=L2dist): #cluster the rows of the "features" matrixdistances={}     currentclustid=-1 # clusters are initially just the individual rowsclust=[cluster_node(array(features[i]),id=i) for i in range(len(features))]while len(clust)>1:  lowestpair=(0,1) closest=distance(clust[0].vec,clust[1].vec)for i in range(len(clust)):for j in range(i+1,len(clust)):# distances is the cache of distance calculationsif (clust[i].id,clust[j].id) not in distances: distances[(clust[i].id,clust[j].id)]=distance(clust[i].vec,clust[j].vec)d=distances[(clust[i].id,clust[j].id)]  if d<closest:  closest=dlowestpair=(i,j) mergevec=[(clust[lowestpair[0]].vec[i]+clust[lowestpair[1]].vec[i])/2.0 \for i in range(len(clust[0].vec))]newcluster=cluster_node(array(mergevec),left=clust[lowestpair[0]],right=clust[lowestpair[1]],distance=closest,id=currentclustid)currentclustid-=1  del clust[lowestpair[1]]del clust[lowestpair[0]]clust.append(newcluster)return clust[0]def extract_clusters(clust,dist):  #(clust上边的树形结构,dist阈值)# extract list of sub-tree clusters from hcluster tree with distance<distclusters = {}if clust.distance<dist:# we have found a cluster subtreereturn [clust] else:# check the right and left branchescl = []   cr = []if clust.left!=None:  cl = extract_clusters(clust.left,dist=dist)if clust.right!=None: cr = extract_clusters(clust.right,dist=dist)return cl+cr  def get_cluster_elements(clust):  #用于取出算好聚类的元素# return ids for elements in a cluster sub-treeif clust.id>=0:  # positive id means that this is a leafreturn [clust.id]else:# check the right and left branchescl = []cr = []if clust.left!=None: cl = get_cluster_elements(clust.left)if clust.right!=None: cr = get_cluster_elements(clust.right)return cl+crdef printclust(clust,labels=None,n=0):  #将值打印出来# indent to make a hierarchy layoutfor i in range(n): print (' '),if clust.id<0: # negative id means that this is branchprint ('-')else:           # positive id means that this is an endpointif labels==None: print (clust.id)else: print (labels[clust.id])if clust.left!=None: printclust(clust.left,labels=labels,n=n+1)if clust.right!=None: printclust(clust.right,labels=labels,n=n+1)def getheight(clust):  #树的高度,递归方法# Is this an endpoint? Then the height is just 1if clust.left==None and clust.right==None: return 1# Otherwise the height is the same of the heights of# each branchreturn getheight(clust.left)+getheight(clust.right)def getdepth(clust):   #树的深度,递归方法if clust.left==None and clust.right==None: return 0return max(getdepth(clust.left),getdepth(clust.right))+clust.distance

相关文章
ML之H-clustering:自定义HierarchicalClustering层次聚类算法

ML之HierarchicalClustering:自定义HierarchicalClustering层次聚类算法相关推荐

  1. ML之Hierarchical clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调

    ML之Hierarchical clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调 目录 输出结果 实现代码 输出结果 实现代码 #!/usr/bin/python # co ...

  2. Crawler/ML:爬虫技术(基于urllib.request库从网页获取图片)+HierarchicalClustering层次聚类算法,实现自动从网页获取图片然后根据图片色调自动分类

    Crawler/ML:爬虫技术(基于urllib.request库从网页获取图片)+HierarchicalClustering层次聚类算法,实现自动从网页获取图片然后根据图片色调自动分类 目录 一. ...

  3. 层次聚类算法的原理及实现Hierarchical Clustering

    层次聚类算法的原理及实现Hierarchical Clustering 层次聚类(Hierarchical Clustering)是聚类算法的一种,通过计算不同类别数据点间的相似度来创建一棵有层次的嵌 ...

  4. 机器学习-Hierarchical clustering 层次聚类算法

    学习彭亮<深度学习基础介绍:机器学习>课程 假设有N个待聚类的样本,对于层次聚类来说,步骤: (初始化)把每个样本归为一类,计算每两个类之间的距离,也就是样本与样本之间的相似度: 寻找各个 ...

  5. 机器学习 改进的层次聚类算法

    http://www.cnblogs.com/wentingtu/archive/2012/01/04/2311533.html 基本工作原理 给定要聚类的N的对象以及N*N的距离矩阵(或者是相似性矩 ...

  6. 层次聚类算法原理总结

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 层次聚类(hierarchical clustering)基于簇间 ...

  7. python层次聚类法画图_Python实现简单层次聚类算法以及可视化

    本文实例为大家分享了Python实现简单层次聚类算法,以及可视化,供大家参考,具体内容如下 基本的算法思路就是:把当前组间距离最小的两组合并成一组. 算法的差异在算法如何确定组件的距离,一般有最大距离 ...

  8. 层次聚类算法 算法_聚类算法简介

    层次聚类算法 算法 Take a look at the image below. It's a collection of bugs and creepy-crawlies of different ...

  9. 【火炉炼AI】机器学习023-使用层次聚类算法构建模型

    [火炉炼AI]机器学习023-使用层次聚类算法构建模型 (本文所使用的Python库和版本号: Python 3.6, Numpy 1.14, scikit-learn 0.19, matplotli ...

最新文章

  1. VS2008 各种杂七杂八技巧
  2. 封装,多态,类的约束,super()深入了解
  3. php cannot find libz,brew安装php70出现configure: error: Cannot find libz 错误解决方法
  4. JSP简单练习-获取表单数据
  5. 酷毙了!三种风格的全屏幻灯片效果【附源码下载】
  6. 记录 Annotation processing is not supported for module cycles.
  7. 刚毕业的他仅用1年就拿下了年薪30W的阿里数据分析岗
  8. Teradata天睿公司任命Steve McMillan为总裁兼首席执行官
  9. 怎么解锁blockinput锁定的键盘_电脑键盘上的三个灯分别是什么作用?虽然很简单,但是很实用...
  10. bootstrap tab切换无效的一种情况
  11. Ansible之五:常用模块
  12. python 使用 config 文件
  13. hot-S22和X参数的原理(转)
  14. 拆解一探电路设计:小米路由器3C拆解
  15. 计算机的神奇功能华为,接上线就变PC!华为Mate 10的电脑模式究竟好用不?
  16. Java九阳神功--BP神经网络JAVA代码解析
  17. 呃,bing 桌面词典中的成熟美女
  18. Python连接MySQL数据库及其操作
  19. android l 论坛,同步更新魔趣源码Android-L编译
  20. js实现音频PCM数据合并、拼接、裁剪、调节音量等功能

热门文章

  1. Linux系统常用磁盘阵列RAID5
  2. QTP整合QC自动化测试--目录结构搭建
  3. 《深入解析Android 虚拟机》——第1章,第1.3节编译Android源码
  4. 版本控制 Git RPM打包
  5. 如此火爆的ZooKeeper,到底如何选主?
  6. 最近学习了 HTTP 连接池
  7. centos(linux) 下如何查看端口占用情况及杀死进程
  8. 程序员的时间管理哲学 —— 如何更好的利用我们的时间
  9. 不改表结构如何动态扩展字段
  10. 一篇文章带你详解 TCP/IP 协议(下)