微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我不明白在 fcluster 中工作的阈值的详细行为方法 ='完成'

如何解决我不明白在 fcluster 中工作的阈值的详细行为方法 ='完成'

Xi=[[0,5,10,8,3],[5,1,3,2],[10,1],[8,6],[3,2,6,0]]

Xi = 距离矩阵

  let dialog;

     const click = async item => {
     var url = `${DEXTA_BASE_URL}/dialog.html#/document/${item.id}`
     var options = { height: 50,width: 30,displayInIframe: true };
      Office.context.ui.displayDialogAsync(url,options,result => {
      dialog = result.value;
      dialog.addEventHandler(Office.EventType.DialogMessageReceived,receiveMessage);
     });

   };

在此代码中阈值 = 9

聚类结果为array([3,dtype=int32)

我不明白为什么不是数组 [2,1]

此图表示聚类后 https://drive.google.com/file/d/17806FuPuNpJiqhT12jiuFOMGNUvB1vjT/view?usp=sharing

解决方法

import numpy as np
import pandas as pd
from scipy.cluster.hierarchy import linkage,dendrogram,fcluster
from scipy.spatial.distance import pdist
import matplotlib.pyplot as plt
import seaborn as sns

你有这个距离矩阵

Xi = np.array([[0,5,10,8,3],[5,1,3,2],[10,1],[8,6],[3,2,6,0]])

我们可以想象为

df = pd.DataFrame(Xi)
# fill NaNs and mask 0s
df.fillna(0,inplace=True)
mask = np.zeros_like(df)
mask[np.triu_indices_from(mask)] = True
sns.heatmap(df,annot=True,fmt='.0f',cmap="YlGnBu",mask=mask);

enter image description here

现在,我们得到了 pdist

p = pdist(Xi)

和联动

Z = linkage(p,method='complete')

您将 9 设置为阈值,所以

dendrogram(Z)
plt.axhline(9,color='k',ls='--');

enter image description here

你有 3 个集群

fcluster(Z,9,criterion='distance')

array([3,dtype=int32)
#      0  1  2  3  4   <- elements

这是正确的,你可以用树状图验证

  • 集群 1 中的元素 241
  • 集群 3 中的元素 2
  • 集群 0 中的元素 3

如果你只想要两个集群,你必须选择12,例如,作为thershold

dendrogram(Z)
plt.axhline(12,ls='--');

enter image description here

所以你得到了预期的结果

fcluster(Z,12,criterion='distance')

array([2,dtype=int32)
#      0  1  2  3  4   <- elements

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。