python层次聚类diana_分裂分析(DIANA)层次聚类

(这篇文章是我关于除法层次聚类算法的previous问题的延续。)

问题是如何用Python(或任何其他语言)实现这个算法。在

算法描述

除法聚类通过一系列连续的分裂进行。在步骤0,所有对象都在一个簇中。在每一步,一个集群被分割,直到在步骤n - 1所有数据对象都是分开的(形成n簇,每个簇都有一个对象)。在

每一步都将一个簇分成两个簇R和{}。最初,A等于R,B为空。在第一阶段,我们必须将一个对象从A移到{}。对于A的每个对象i,我们计算与A所有其他对象的平均差异:

上面等式达到最大值的对象i'将被移动,因此我们将

在下一个阶段中,我们寻找从A到{}的其他点。只要A仍然包含多个对象,我们就计算

对于A的每个对象i,我们考虑使这个数量最大化的对象{}。当上面等式的最大值严格为正时,我们将i''从A移到{},然后在新的A中寻找可能移动的另一个对象。另一方面,当差的最大值为负或0时,我们停止该过程并完成R到{}和{}的划分。在

在除法算法的每一步,我们还必须决定要分割哪个簇。为此,我们计算直径

对于上一步之后可用的每个簇Q,并选择直径最大的簇。在

我的起始代码粘贴在下面。实际上,脚本返回ValueError: list.remove(x): x not in list。在# Dissimilarity matrix

dm = [

[ 0, 2, 6, 10, 9],

[ 2, 0, 5, 9, 8],

[ 6, 5, 0, 4, 5],

[10, 9, 4, 0, 3],

[ 9, 8, 5, 3 ,0]]

# Create splinter and remaining group

splinter = []

remaining = range(len(dm))

# Find record ra that has the greatest average distance from the rest of the records

ra = 0

dMax = -999

for i in range(len(dm)):

dSum = 0

for j in range(len(dm)):

if i == j:

continue

dSum += dm[i][j]

if dMax < dSum:

dMax = dSum

ra = i

splinter.append(ra)

remaining.remove(ra)

# Check every record in remaining and moves the record if record is closer to splinter

bChanged = True

while bChanged:

bChanged = False

for i in range(len(remaining)):

d1 = 0.0

for j in range(len(splinter)):

d1 += dm[i][j]

d1 /= float(len(splinter))

d2 = 0.0

for k in range(len(remaining)):

if i == k:

continue

d2 += dm[i][k]

if len(remaining) > 1:

d2 /= (len(remaining) - 1.0)

if d1 < d2:

bChanged = True

splinter.append(i)

remaining.remove(i)

break

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值