1
我是R的全新新手,並試圖處理kmeans對象。理想情況下,我想要做的是爲我的數據中的每個點獲取集羣標籤列表,並將標籤替換爲相應的中心。本質上,最後是一個矩陣,其中每個數據點由kmeans已經放入的簇的中心值表示。聚類標籤和聚類中心(R中的kmeans)
有沒有辦法有效地做到這一點,而不是手動通過每個條目並用集羣中心值替換集羣標籤?
謝謝!
我是R的全新新手,並試圖處理kmeans對象。理想情況下,我想要做的是爲我的數據中的每個點獲取集羣標籤列表,並將標籤替換爲相應的中心。本質上,最後是一個矩陣,其中每個數據點由kmeans已經放入的簇的中心值表示。聚類標籤和聚類中心(R中的kmeans)
有沒有辦法有效地做到這一點,而不是手動通過每個條目並用集羣中心值替換集羣標籤?
謝謝!
這是你在追求什麼?從this answer擴展:
# make some data
x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
#
# do cluster analysis
(cl <- kmeans(x, 2))
#
# put cluster labels with data
out1 <- data.frame(cbind(x, clusterNum = cl$cluster))
#
# organise center coords to be ready for merging
centers <- data.frame(cbind(data.frame(cl$center[,1]),
data.frame(cl$center[,2]),
clusterNum=rownames(cl$center)))
#
# merge cluster center coords with data
out2 <- merge(out1, centers, all.x = TRUE)
#
# check output
out2
clusterNum x y cl.center...1. cl.center...2.
1 1 0.233161364 -0.04258146 0.01064895 0.01376516
2 1 -0.356284774 -0.59135602 0.01064895 0.01376516
3 1 -0.302272796 -0.24033113 0.01064895 0.01376516
4 1 -0.369299302 -0.24997660 0.01064895 0.01376516
5 1 -0.060454427 0.19711328 0.01064895 0.01376516
...
90 2 0.609833599 0.67729922 1.05184887 1.03445718
91 2 0.943306637 1.09420588 1.05184887 1.03445718
92 2 0.545053826 1.22620571 1.05184887 1.03445718
93 2 0.706921965 1.10326091 1.05184887 1.03445718
94 2 0.837644227 1.07121784 1.05184887 1.03445718
95 2 0.550863085 1.06977250 1.05184887 1.03445718
#
# Success! We have one dataframe that includes: raw data, cluster labels
# and cluster center coords
我以前merge
把聚類中心COORDS是原始數據,但無疑有更有效的方法(例如,不需要cl$center
進行重組)。
恩,這很有幫助 - 我還想知道如何用集羣中心替換集羣中心標籤。例如,我有一個數組即聚類中心... ob < - kmeans(array,k) labels <-ob $ cluster An和一個聚類中心數組。 中心< - ob $中心 我想要做的是使用聚類中心值重新標籤標籤中的聚類標籤。因此'標籤'的每個元素都是集羣中心值。我有一點困難,雖然因爲我在3D點上做kmeans,所以每個點都由3個值的矩陣表示。 – 2012-03-17 14:45:09
爲什麼不提問另一個問題,這次包括您剛纔在評論中發現的新細節,以及一個可重複的示例,請按照以下說明進行操作:http://www.stackoverflow.com/questions/5963269/how-到化妝一個偉大-R-重複性,例如 – Ben 2012-03-17 16:21:39