【发布时间】:2013-05-28 19:36:49
【问题描述】:
我对这个模块 (scipy.cluster.hierarchy) 感到困惑......但仍然有一些!
例如,我们有以下树状图:
我的问题是如何以一种很好的格式(比如 SIF 格式)提取彩色子树(每个子树代表一个簇)? 现在得到上图的代码是:
import scipy
import scipy.cluster.hierarchy as sch
import matplotlib.pylab as plt
scipy.randn(100,2)
d = sch.distance.pdist(X)
Z= sch.linkage(d,method='complete')
P =sch.dendrogram(Z)
plt.savefig('plot_dendrogram.png')
T = sch.fcluster(Z, 0.5*d.max(), 'distance')
#array([4, 5, 3, 2, 2, 3, 5, 2, 2, 5, 2, 2, 2, 3, 2, 3, 2, 5, 4, 5, 2, 5, 2,
# 3, 3, 3, 1, 3, 4, 2, 2, 4, 2, 4, 3, 3, 2, 5, 5, 5, 3, 2, 2, 2, 5, 4,
# 2, 4, 2, 2, 5, 5, 1, 2, 3, 2, 2, 5, 4, 2, 5, 4, 3, 5, 4, 4, 2, 2, 2,
# 4, 2, 5, 2, 2, 3, 3, 2, 4, 5, 3, 4, 4, 2, 1, 5, 4, 2, 2, 5, 5, 2, 2,
# 5, 5, 5, 4, 3, 3, 2, 4], dtype=int32)
sch.leaders(Z,T)
# (array([190, 191, 182, 193, 194], dtype=int32),
# array([2, 3, 1, 4,5],dtype=int32))
所以现在,fcluster() 的输出给出了节点的聚类(通过它们的 id),而 leaders() 描述的 here 应该返回 2 个数组:
第一个包含 Z 生成的集群的领导节点,这里我们可以看到我们有 5 个集群,以及在图中
第二个是这些集群的 id
所以如果这个领导者()返回相应的。 L 和 M :L[2]=182 和 M[2]=1,然后集群 1 由节点 id 182 引导,这在观察集 X 中不存在,文档说“......那么它对应于非单例集群” .但我无法得到它......
另外,我通过sch.to_tree(Z) 将 Z 转换为树,这将返回一个易于使用的树对象,我想将其可视化,但我应该使用哪个工具作为操作这些类型的图形平台树对象作为输入?
【问题讨论】:
标签: python python-2.7 numpy scipy hierarchical-clustering