如何在知道簇数的情况下在 cutree() 中获取树的高度

How to obtain the height of tree in cutree() knowing the number of clusters

我正在使用层次聚类对我的数据进行分类。

我想定义最佳簇数。为此,我们的想法是可视化一个图形,其中 x 轴是聚类的数量,y 轴是树状图中树的高度。

为此,当指定簇数K时,我需要知道树的高度,例如如果K=4,我需要知道命令后树的高度

cutree(hclust(dist(data), method = "ward.D"), k = 4) 

有人可以帮忙吗?

高度存储在 hclust 对象中。由于您没有提供任何数据,我将以 built-in 鸢尾花数据进行说明。

HC = hclust(dist(iris[,1:4]), method="ward.D")
sort(HC$height)
<reduced output>
[133]   1.8215623   1.8787489   1.9240172   1.9508686   2.5143038   2.7244855
[139]   2.9123706   3.1111893   3.2054610   3.9028695   4.9516315   6.1980126
[145]   9.0114060  10.7530460  18.2425079  44.1751473 199.6204659

最大值为第一次分割的高度。第二大是第二次分裂,等等。 你可以看到这通过绘图给出了你需要的高度。

plot(HC)
abline(h=10.75,col="red")

可以看到第四大的高度和第四个分裂的高度相匹配。