[英]clustering using a proximity matrix in r
I have a proximity matrix (dissimilarity) of an mahalanobis distance. 我有一个马氏距离的接近度矩阵(相异度)。
the matrix (sample): 矩阵(样本):
> dput(MD[1:5,1:5])
structure(c(0, 10.277, 8.552, 8.592, 9.059, 10.277, 0, 10.917,
9.489, 8.176, 8.552, 10.917, 0, 8.491, 8.104, 8.592, 9.489, 8.491,
0, 9.375, 9.059, 8.176, 8.104, 9.375, 0), .Dim = c(5L, 5L), .Dimnames = list(
c("2", "4", "5", "6", "9"), c("X2", "X4", "X5", "X6", "X9"
)))
the matrix has 1900 people and the row name are an Id. 矩阵有1900个人,行名是一个ID。 I need to cluster those people and the to get a number of a cluster next to the person's id. 我需要对这些人进行聚类,然后在该人的ID旁边获取一个聚类数。
I know how to cluster using k-means but I don't how to cluster when you have already a dissimilarity matrix. 我知道如何使用k均值进行聚类,但是当您已经具有不相似矩阵时,我不知道如何进行聚类。
You can use hierarchical clustering, starting with the Mahalanobis distance matrix: 您可以从马氏距离矩阵开始使用分层聚类:
MD
X2 X4 X5 X6 X9
#2 0.000 10.277 8.552 8.592 9.059
#4 10.277 0.000 10.917 9.489 8.176
#5 8.552 10.917 0.000 8.491 8.104
#6 8.592 9.489 8.491 0.000 9.375
#9 9.059 8.176 8.104 9.375 0.000
hc <- hclust(as.dist(MD))
clusters <- cutree(hc, k = 3) # obtain 3 clusters
clusters
#2 4 5 6 9
#1 2 3 1 3
plot(hc)
rect.hclust(hc, k = 3, border = "red")
If I understood you correctly, using dendextend
: 如果我正确理解您的话,请使用dendextend
:
fit<-hclust(MD)
fit %>% as.dendrogram %>%
set("branches_k_color", k = 3, value = c("purple", "orange","red")) %>%
plot
The cluster # are specified by colors ... passed on from cutree
. 簇号由从cutree
传递的颜色指定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.