这个算法。我个人感觉有点鸡肋。最终的表达也不是特别清楚。

原理很简单,从所有的样本中选取Euclidean distance最近的两个样本,归为一类,取其平均值组成一个新样本,总样本数少1;不断的重复,最终样本数为1。这样的话就形成了一个树,每个节点要不有两个子节点,要不没有子节点。

这个算法也大概能分出来类,但是实用性我觉得不是很强。

源代码

 from numpy import *

 class cluster_node:
def __init__(self,vec,left=None,right=None,distance=0.0,id=None,count=1):
self.left=left
self.right = right
self.vec = vec
self.distance = distance
self.id = id
self.count = count
def L2dist(v1,v2):
return sqrt(sum(v1-v2)**2)
def L1dist(v1,v2):
return sum(abs(v1-v2)) def hcluster(features,distance=L2dist):
distances={}
currentclustid=-1 clust=[cluster_node(array(features[i],id=i) for i in range(len(features)))] while len(clust)>1:
lowstpiar=(0,1)
closest=distance(clust[0].vec,clust[1].vec) for i in range(len(clust)):
for j in range(i+1,len(clust)):
if(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=d
lowstpiar=(i,j)
mergeve=[(clust[lowstpiar[0]].vec[i]+clust[lowstpiar[1]].vec[i])/2.0 for i in range(len(clust[lowstpiar[1]].vec))]
newcluster=cluster_node(array(mergeve),left=clust[lowstpiar[0]],right=clust[lowstpiar[1]],distance=closest,id=currentclustid)
currentclustid-=1
del clust[lowstpiar[1]]
del clust[lowstpiar[0]]
clust.append(newcluster)
return clust[0] def extract_clusters(clust,dist):
clusters={}
if clust.distance<dist:
return [clust]
else:
cl=[]
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_element(clust):
if clust.id>=0:
return [clust.id]
else:
cl=[]
cr=[]
if clust.left!=None:
cl=get_cluster_element(clust.left)
if clust.right != None:
cr=get_cluster_element(clust.right)
return cl+cr
def printclust(clust,labels=None,n=0):
for i in range(n):print(' ')
if clust.id<0:
print('-')
else:
if 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):
if clust.left==None and clust.right==None:return 1
return getheight(clust.left)+getheight(clust.right)
def getdepth(clust):
if clust.left==None and clust.right==None:return 0
return max(getheight(clust.left),getheight(clust.right))+clust.distance

为了节约时间,我只写了算法部分,实际应用的没写。

这个当中的递归用的不错。还有对每个节点类的定义

菜鸟之路——机器学习之HierarchicalClustering层次分析及个人理解的更多相关文章

  1. 菜鸟之路——机器学习之决策树个人理解及Python实现

    最近开始学习机器学习,以下会记录我学习中遇到的问题以及我个人的理解 决策树算法,网上很多介绍,在这不复制粘贴.下面解释几个关键词就好. 信息熵(entropy):就是信息不确定性的多少 H(x)=-Σ ...

  2. 菜鸟之路——机器学习之BP神经网络个人理解及Python实现

    关键词: 输入层(Input layer).隐藏层(Hidden layer).输出层(Output layer) 理论上如果有足够多的隐藏层和足够大的训练集,神经网络可以模拟出任何方程.隐藏层多的时 ...

  3. 菜鸟之路——机器学习之KNN算法个人理解及Python实现

    KNN(K Nearest Neighbor) 还是先记几个关键公式 距离:一般用Euclidean distance   E(x,y)√∑(xi-yi)2 .名字这么高大上,就是初中学的两点间的距离 ...

  4. 菜鸟之路——机器学习之Kmeans聚类个人理解及Python实现

    一些概念 相关系数:衡量两组数据相关性 决定系数:(R2值)大概意思就是这个回归方程能解释百分之多少的真实值. Kmeans聚类大致就是选择K个中心点.不断遍历更新中心点的位置.离哪个中心点近就属于哪 ...

  5. 菜鸟之路——机器学习之非线性回归个人理解及python实现

    关键词: 梯度下降:就是让数据顺着梯度最大的方向,也就是函数导数最大的放下下降,使其快速的接近结果. Cost函数等公式太长,不在这打了.网上多得是. 这个非线性回归说白了就是缩小版的神经网络. py ...

  6. 菜鸟之路——机器学习之线性回归个人理解及Python实现

    这一节很简单,都是高中讲过的东西 简单线性回归:y=b0+b1x+ε.b1=(Σ(xi-x–)(yi-y–))/Σ(xi-x–)ˆ2       b0=y--b1x-    其中ε取 为均值为0的正态 ...

  7. 菜鸟之路——机器学习之SVM分类器学习理解以及Python实现

    SVM分类器里面的东西好多呀,碾压前两个.怪不得称之为深度学习出现之前表现最好的算法. 今天学到的也应该只是冰山一角,懂了SVM的一些原理.还得继续深入学习理解呢. 一些关键词: 超平面(hyper ...

  8. 菜鸟之路——Linux基础::计算机网络基础,Linux常用系统命令,Linux用户与组权限

    最近又重新安排了一下我的计划.准备跟着老男孩的教程继续学习,感觉这一套教程讲的很全面,很详细.比我上一套机器学习好的多了. 他的第一阶段是Python基础,第二阶段是高等数学基础,主要将机器学习和深度 ...

  9. 从Elo Rating System谈到层次分析法

    1. Elo Rating System Elo Rating System对于很多人来说比较陌生,根据wikipedia上的解释:Elo评分系统是一种用于计算对抗比赛(例如象棋对弈)中对手双方技能水 ...

随机推荐

  1. HDU 1011 Starship Troopers星河战队(树形dp)

    题意 有n个洞穴编号为1-n,洞穴间有通道,形成了一个n-1条边的树, 洞穴的入口即根节点是1. 每个洞穴有x只bugs,并有价值y的金子,全部消灭完一个洞穴的虫子,就可以获得这个洞穴的y个金子. 现 ...

  2. HDU2612 BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 , 一道比较简单的广搜(BFS)题目. 算法: 设置两个dist[][]数组,记录Y和M到几个K ...

  3. Java Web报错:getOutputStream() has already been called for this response解决方案

    今天做了个导出excel表的功能.大概代码如下: ouputStream = response.getOutputStream(); wb.write(ouputStream); ouputStrea ...

  4. python_48_Python3中字符编码与转码

    python3默认是Unicode,不用声明# -*- coding:utf-8 -*-,如果声明则是utf-8 unicode='你好' print('utf-8:',unicode.encode( ...

  5. AOSP常见漏洞类型简介

    Heap/Stack Overflow(CVE-2017-0541) 漏洞出现在PushcdlStack函数中,如下所示   # /external/sonivox/arm-wt-22k/lib_sr ...

  6. Framework7:不会Objective-C,也能开发iOS7应用

    摘要:Framework7是一款开源的轻量级HTML框架,用来创建混合或有着iOS7原生体验的Web应用.其包含HTML布局.所有基础界面.动画效果.视图以及简单的自定义样式,让你无需修炼Object ...

  7. vue实现tab切换功能精简版

    <template> <div> <p class="tabs" v-for="(list,index) in lists" :c ...

  8. 为啥国内互联网公司都用centos而不是ubuntu?

    一直以来都很好奇ubuntu和centos有啥区别,上学时接触的都是ubuntu,自己每次装virtual box的时候都会下个ubuntu,但是公司的服务器上装的都是centos,今天查了下知乎网友 ...

  9. web端图片文件直传

    采用JS客户端直接签名有一个很严重的安全隐患.就是OSS AccessId/AccessKey暴露在前端页面.可以随意拿到AccessId/AccessKey,这是非常不安全的做法. 本文将此例子进化 ...

  10. python 3 在工作中的应用

    Python 3在工作中的使用 安装配置Python 3 在notepad++中配置Python 3 使用sql server数据库 操作Excel 发送email python 3 使用日志   安 ...