gpt4 book ai didi

R聚类-带有观察标签的轮廓

转载 作者:行者123 更新时间:2023-12-01 23:43:25 24 4
gpt4 key购买 nike

我使用 cluster 进行层次聚类R 中的包。使用 silhouette函数,我可以获得树状图中任何给定高度 (h) 截止值的集群输出的轮廓图。

# run hierarchical clustering
if(!require("cluster")) { install.packages("cluster"); require("cluster") }
tmp <- matrix(c( 0, 20, 20, 20, 40, 60, 60, 60, 100, 120, 120, 120,
20, 0, 30, 50, 60, 80, 40, 80, 120, 100, 140, 120,
20, 30, 0, 40, 60, 80, 80, 80, 120, 140, 140, 80,
20, 50, 40, 0, 60, 80, 80, 80, 120, 140, 140, 140,
40, 60, 60, 60, 0, 20, 20, 20, 60, 80, 80, 80,
60, 80, 80, 80, 20, 0, 20, 20, 40, 60, 60, 60,
60, 40, 80, 80, 20, 20, 0, 20, 60, 80, 80, 80,
60, 80, 80, 80, 20, 20, 20, 0, 60, 80, 80, 80,
100, 120, 120, 120, 60, 40, 60, 60, 0, 20, 20, 20,
120, 100, 140, 140, 80, 60, 80, 80, 20, 0, 20, 20,
120, 140, 140, 140, 80, 60, 80, 80, 20, 20, 0, 20,
120, 120, 80, 140, 80, 60, 80, 80, 20, 20, 20, 0),
nr=12, dimnames=list(LETTERS[1:12], LETTERS[1:12]))

cl <- hclust(as.dist(tmp,diag = TRUE, upper = TRUE), method= 'single')
sil_cl <- silhouette(cutree(cl, h=25) ,as.dist(tmp), title=title(main = 'Good'))
plot(sil_cl)

这给出了下图,这是让我感到沮丧的一点。 如何使用观察标签 rownames(tmp)在轮廓图中,而不是数字索引(1 到 12) - 这对我来说毫无意义。

enter image description here

最佳答案

我不知道为什么,但 silhouette call 似乎删除了行名。您可以将它们添加回来

cl <- hclust(as.dist(tmp,diag = TRUE, upper = TRUE), method= 'single')
sil_cl <- silhouette(cutree(cl, h=25) ,as.dist(tmp), title=title(main = 'Good'))

rownames(sil_cl) <- rownames(tmp)

plot(sil_cl)

enter image description here

关于R聚类-带有观察标签的轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30261435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com