2016-10-05 22 views
-5

我在R中運行分層聚類,我怎樣才能將哪個觀察屬於哪個聚類?謝謝!我如何知道哪些觀察屬於哪個簇?

### Hierarchical Clustering 
d <- dist(EMEA_2, method = "euclidean") # distance matrix 
fit <- hclust(d, method="complete") 

### Decide bet number of clusters 
library(knitr) 
library(NbClust) 

nc<-NbClust(data = EMEA_2, distance = "euclidean", min.nc=2, max.nc=15, method = "complete", index = "db", alphaBeale = 0.1) 

groups <- cutree(fit, k=2) # cut tree into 2 clusters 

### Get group means and number of frequencies within each cluster 
a2<-aggregate(EMEA_2, list(groups),mean) 
a4<-data.frame(Cluster = a2[,1], Freq = as.vector(table(groups)), a2[,-1]) 
+0

你的代碼在哪裏? – mtoto

+0

@mtoto我剛剛編輯謝謝! –

+1

看'str(nc)'''''''''''''''''''''''''''''''''''''''''stats :: kmeans'然後你可以用'nc $ clusters $'或者其他名稱來訪問它。 –

回答

1

如果您有興趣從NbClust優化結果,你會發現它在nc$Best.partition,每個數字都是在數據矩陣各行的簇號。

例如

> nc$Best.partition 
[1] 1 2 3 4 5 1 3 5 1 1 4 1 4 1 5 1 5 1 4 2 

爲20×10的數據矩陣。

相關問題