cur*_*ous 2 python matplotlib scipy
我有以下代码来执行数据的分层clutering:
Z = linkage(data,method='weighted')
  plt.subplot(2,1,1)
  dendro = dendrogram(Z)
  leaves = dendro['leaves']
  print leaves
  plt.show()
在树形图中,所有簇都具有相同的颜色(蓝色).有没有办法在群集之间使用不同颜色的相似性?
查看文档,看起来您可以传递link_color_func关键字或color_threshold关键字以获得不同的颜色.
编辑:
树形图着色方案的默认行为是,如果第一个节点低于切割阈值,则为color_threshold = 0.7*max(Z[:,2])群集节点下方的所有后代链路着色k相同的颜色k; 否则,连接距离大于或等于阈值的节点的所有链接都会以蓝色[来自文档].
这到底是什么意思?好吧,如果你看一个树形图,不同的簇链接在一起.两个簇之间的"距离"是它们之间链接的高度.的color_threshold是高度低于该新集群将是不同的颜色.如果你的所有星团都是蓝色的,那么你需要提高你的星团color_threshold.例如,
In [48]: mat = np.random.rand(10, 10)
In [49]: z = linkage(mat, method="weighted")
In [52]: d = dendrogram(z)
In [53]: d['color_list']
Out[53]: ['g', 'g', 'b', 'r', 'c', 'c', 'c', 'b', 'b']
In [54]: plt.show()

我可以查看默认值color_threshold是什么
In [56]: 0.7*np.max(z[:,2])
Out[56]: 1.0278719020096947
如果我降低color_threshold,我会变得更蓝,因为更多链接的距离大于新的color_threshold.您可以直观地看到这一点,因为0.9以上的所有链接现在都是蓝色的:
In [64]: d = dendrogram(z, color_threshold=.9)
In [65]: d['color_list']
Out[65]: ['g', 'b', 'b', 'r', 'b', 'b', 'b', 'b', 'b']
In [66]: plt.show()

如果我增加color_thresholdto 1.2,则下面的链接1.2将不再是蓝色.此外,青色和红色链接将合并为单一颜色,因为它们的父链接位于以下位置1.2:

| 归档时间: | 
 | 
| 查看次数: | 1330 次 | 
| 最近记录: |