机器学习入门之HierarchicalClustering层次分析及个人理解
小标 2018-09-29 来源 : 阅读 2115 评论 0

摘要:本文主要向大家介绍了机器学习入门之HierarchicalClustering层次分析及个人理解,通过具体的内容向大家展现,希望对大家学习机器学习入门有所帮助。

本文主要向大家介绍了机器学习入门之HierarchicalClustering层次分析及个人理解,通过具体的内容向大家展现,希望对大家学习机器学习入门有所帮助。

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

原理很简单,从所有的样本中选取Euclidean distance最近的两个样本,归为一类,取其平均值组成一个新样本,总样本数少1;不断的重复,最终样本数为1。这样的话就形成了一个树,每个节点要不有两个子节点,要不没有子节点。
这个算法也大概能分出来类,但是实用性我觉得不是很强。
源代码
 

 1 from numpy import *
 2 
 3 
 4 class cluster_node:
 5     def __init__(self,vec,left=None,right=None,distance=0.0,id=None,count=1):
 6         self.left=left
 7         self.right = right
 8         self.vec = vec
 9         self.distance = distance
10         self.id = id
11         self.count = count
12 def L2dist(v1,v2):
13     return sqrt(sum(v1-v2)**2)
14 def L1dist(v1,v2):
15     return sum(abs(v1-v2))
16 
17 def hcluster(features,distance=L2dist):
18     distances={}
19     currentclustid=-1
20 
21     clust=[cluster_node(array(features[i],id=i) for i in range(len(features)))]
22 
23     while len(clust)>1:
24         lowstpiar=(0,1)
25         closest=distance(clust[0].vec,clust[1].vec)
26 
27         for i in range(len(clust)):
28             for j in range(i+1,len(clust)):
29                 if(clust[i].id,clust[j].id) not in distances:
30                     distances[(clust[i].id,clust[j].id)]=distance(clust[i].vec,clust[j].vec)
31                 d=distances[(clust[i].id,clust[j].id)]
32                 if d<closest:
33                     closest=d
34                     lowstpiar=(i,j)
35         mergeve=[(clust[lowstpiar[0]].vec[i]+clust[lowstpiar[1]].vec[i])/2.0 for i in range(len(clust[lowstpiar[1]].vec))]
36         newcluster=cluster_node(array(mergeve),left=clust[lowstpiar[0]],right=clust[lowstpiar[1]],distance=closest,id=currentclustid)
37         currentclustid-=1
38         del clust[lowstpiar[1]]
39         del clust[lowstpiar[0]]
40         clust.append(newcluster)
41     return clust[0]
42 
43 def extract_clusters(clust,dist):
44     clusters={}
45     if clust.distance<dist:
46         return [clust]
47     else:
48         cl=[]
49         cr=[]
50         if clust.left!=None:
51             cl=extract_clusters(clust.left,dist=dist)
52         if clust.right != None:
53             cr=extract_clusters(clust.right,dist=dist)
54         return cl+cr
55 
56 def get_cluster_element(clust):
57     if clust.id>=0:
58         return [clust.id]
59     else:
60         cl=[]
61         cr=[]
62         if clust.left!=None:
63             cl=get_cluster_element(clust.left)
64         if clust.right != None:
65             cr=get_cluster_element(clust.right)
66         return cl+cr
67 def printclust(clust,labels=None,n=0):
68     for i in range(n):print(‘ ‘)
69     if clust.id<0:
70         print(‘-‘)
71     else:
72         if labels==None:print(clust.id)
73         else:print(labels[clust.id])
74 
75     if clust.left !=None:printclust(clust.left,labels=labels,n=n+1)
76     if clust.right != None: printclust(clust.right, labels=labels, n=n + 1)
77 
78 def getheight(clust):
79     if clust.left==None and clust.right==None:return 1
80     return getheight(clust.left)+getheight(clust.right)
81 def getdepth(clust):
82     if clust.left==None and clust.right==None:return 0
83     return max(getheight(clust.left),getheight(clust.right))+clust.distance

 
为了节约时间,我只写了算法部分,实际应用的没写。
这个当中的递归用的不错。还有对每个节点类的定义

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标人工智能机器学习频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程