如何在scipy创建的树状图中获得与颜色簇相对应的平坦簇 [英] How to get flat clustering corresponding to color clusters in the dendrogram created by scipy

查看:150
本文介绍了如何在scipy创建的树状图中获得与颜色簇相对应的平坦簇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用发布的代码)。但是,我看不到在哪里可以为fcluster提供与创建树状图时指定的相同的 cutoff 。看来 fcluster t 中的阈值变量必须是各种晦涩的度量(不一致距离最大clust monocrit )。有任何想法吗?

解决方案

我认为您的做法正确。让我们尝试一下:

  import scipy 
import scipy.cluster.hierarchy as sch
X = scipy。 randn(100,2)#100二维观测值
d = sch.distance.pdist(X)#(100选择2)成对距离的向量
L = sch.linkage(d,method =' complete')
ind = sch.fcluster(L,0.5 * d.max(),'distance')

ind 将为您提供100个输入观测值的聚类索引。 ind 取决于您在链接中使用的方法。尝试 method = single 完成平均值。然后注意 ind 有何不同。



示例:

 在[59]中:L = sch.linkage(d,method ='complete')

在[60]中:sch.fcluster(L,0.5 * d.max(),'distance')
Out [60]:
array([5,4,2,2,5,5,1,5,5,2,5,2, 5,5,1,1,5,5,4,2,2,5,2,5,
2,5,3,5,3,5,5,5,5,5,5,5, 2,2,5,5,4,1,4,5,2,1,4,4,
2,4,2,2,5,5,5,5,2,5,5,3,5, 5,4,5,4,5,3,5,3,5,5,5,5,
2,3,5,5,4,4,5,5,2,2,5,2,2, 4,1,2,1,5,2,5,5,5,5,1,5,
4,2,4,5,2,2,4,4,2])

在[61]中:L = sch.linkage(d,method ='single')

在[62]中:sch.fcluster(L,0.5 * d.max(),'distance' )
Out [62]:
array([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1])$ ​​b $ b

scipy.cluster.hierarchy 确实令人困惑。在您的链接中,我什至无法识别自己的代码!


Using the code posted here, I created a nice hierarchical clustering:

Let's say the the dendrogram on the left was created by doing something like

Y = sch.linkage(D, method='average') # D is a distance matrix
cutoff = 0.5*max(Y[:,2])
Z = sch.dendrogram(Y, orientation='right', color_threshold=cutoff)

Now how do I get the indices of the members of each of the colored clusters? To simplify this situation, ignore the clustering on the top, and focus only on the dendrogram on the left of the matrix.

This information should be stored in the dendrogram Z stored variable. There is a function that should do just what I want called fcluster (see documentation here). However I cannot see where I can give fcluster the same cutoff as I specified in the creation of the dendrogram. It seems that the threshold variable in fcluster, t has to be in terms of various obscure measurements (inconsistent, distance, maxclust, monocrit). Any ideas?

解决方案

I think you're on the right track. Let's try this:

import scipy
import scipy.cluster.hierarchy as sch
X = scipy.randn(100, 2)     # 100 2-dimensional observations
d = sch.distance.pdist(X)   # vector of (100 choose 2) pairwise distances
L = sch.linkage(d, method='complete')
ind = sch.fcluster(L, 0.5*d.max(), 'distance')

ind will give you cluster indices for each of the 100 input observations. ind depends on what method you used in linkage. Try method=single, complete, and average. Then note how ind differs.

Example:

In [59]: L = sch.linkage(d, method='complete')

In [60]: sch.fcluster(L, 0.5*d.max(), 'distance')
Out[60]: 
array([5, 4, 2, 2, 5, 5, 1, 5, 5, 2, 5, 2, 5, 5, 1, 1, 5, 5, 4, 2, 5, 2, 5,
       2, 5, 3, 5, 3, 5, 5, 5, 5, 5, 5, 5, 2, 2, 5, 5, 4, 1, 4, 5, 2, 1, 4,
       2, 4, 2, 2, 5, 5, 5, 2, 5, 5, 3, 5, 5, 4, 5, 4, 5, 3, 5, 3, 5, 5, 5,
       2, 3, 5, 5, 4, 5, 5, 2, 2, 5, 2, 2, 4, 1, 2, 1, 5, 2, 5, 5, 5, 1, 5,
       4, 2, 4, 5, 2, 4, 4, 2])

In [61]: L = sch.linkage(d, method='single')

In [62]: sch.fcluster(L, 0.5*d.max(), 'distance')
Out[62]: 
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1])

scipy.cluster.hierarchy sure is confusing. In your link, I don't even recognize my own code!

这篇关于如何在scipy创建的树状图中获得与颜色簇相对应的平坦簇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆