聚类顺序为(x2,x4),(x2,x3,x4),(x2,x3,x4,x5),(x1,x2,x3,x4,x5),样本距离计算选择single linkage ,和图聚类顺序一致。
x1 | x2 | x3 | x4 | x5 | |
x1 | 0 | 26 | 24 | 25 | 23 |
x2 | 26 | 0 | 12 | 3 | 25 |
x3 | 24 | 12 | 0 | 7 | 15 |
x4 | 25 | 3 | 7 | 0 | 24 |
x5 | 23 | 25 | 15 | 24 | 0 |
x1 | (x2,x4) | x3 | x5 | |
x1 | 0 | 25 | 24 | 23 |
(x2,x4) | 25 | 0 | 7 | 24 |
x3 | 24 | 7 | 0 | 15 |
x5 | 23 | 24 | 15 | 0 |
x1 | (x2,x3,x4) | x5 | |
x1 | 0 | 24 | 23 |
(x2,x3,x4) | 24 | 0 | 15 |
x5 | 23 | 15 | 0 |
x1 | (x2,x3,x4,x5) | |
x1 | 0 | 23 |
(x2,x3,x4,x5) | 23 | 0 |
import numpy as np
from scipy.cluster.hierarchy import dendrogram,linkage
from matplotlib import pyplot as plt
import math
x1=[0,1,3,1,3,4]
x2=[0,0,1,0,1,0]
x3=[2,1,0,2,2,1]
x4=[1,0,0,0,1,1]
x5=[3,3,3,1,2,1]
data=[x1,x2,x3,x4,x5]
data=np.array(data)
reslut=linkage(data,method='single')
dendrogram(reslut)
plt.show()
def juli(a: list, b: list) -> int:
al=len(a)
bl=len(b)
jl=0
for i in range(al):
jl+=math.pow(abs(a[i]-b[i]),2)
return jl
for i in range(len(data)):
for j in range(len(data)):
print("x%d-x%d的距离为%d"%(i+1,j+1,juli(data[i],data[j])))
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容