在r中,在树状图中找到分层聚类树状图的簇数。

时间:2022-07-08 14:55:01

Here I have some problem to find number of clusters after using cutree on a dendrogram. Here is my approach:

这里我有一些问题,在使用树图上使用cutree之后,可以找到集群的数量。这是我的方法:

mat <- a huge matrix
hc <- (as.dist(mat), method = "average", members = NULL)  
#to cut the tree just 1 level below the maximum height
tree <- cutree(hc, h = hc$height[[length(hc$height)-1]])  

By printing the tree variable I can see that my dendrogram is cut into two clusters. I can also get the labels from each cluster using names(tree[tree==1]), but how can get the number of clusters without looking at the data? I want to automate this in a pipeline based on number of clusters it has in tree variable.

通过打印树变量,我可以看到我的dendrogram被分割成两个集群。我还可以使用名称来获取每个集群的标签(tree[tree==1]),但是如何在不查看数据的情况下获得集群的数量呢?我想在一个基于树变量的集群数量的流水线上实现自动化。

1 个解决方案

#1


0  

finally i made it to answer my question by running a loop over the tree object after cutting dendrogram, but this might not be an optimal solution. and feel free to suggest modifications to make it more elegant..

最后,我通过在树对象上运行一个循环来回答我的问题,但这可能不是一个最优的解决方案。并且可以随意建议修改,使其更加优雅。

clust <- c()
for (i in 1:length(tree)){
clust[i] <- tree[[i]]
}
length(unique(clust))  

This should possibly give the answer as per my knowledge..
Thank you

这可能会根据我的知识给出答案。谢谢你!

#1


0  

finally i made it to answer my question by running a loop over the tree object after cutting dendrogram, but this might not be an optimal solution. and feel free to suggest modifications to make it more elegant..

最后,我通过在树对象上运行一个循环来回答我的问题,但这可能不是一个最优的解决方案。并且可以随意建议修改,使其更加优雅。

clust <- c()
for (i in 1:length(tree)){
clust[i] <- tree[[i]]
}
length(unique(clust))  

This should possibly give the answer as per my knowledge..
Thank you

这可能会根据我的知识给出答案。谢谢你!