[英]K-means initial centers membership
我正在嘗試用r繪制k均值算法的所有步驟,但我做不到。
k均值算法以這種方式工作:
我繪制了數據集和聚類的初始中心(步驟1)。 同樣,我可以繪制新的聚類中心並顯示哪個點屬於每個聚類(步驟3和4)。 但是我不知道如何繪制第2步。在第一次迭代之前,我需要每個點的第一個初始中心成員身份 ,但是kmeans()
無法給您。 我該如何計算?
這是我的代碼:
set.seed(2009)
points1<-data.frame(x=rnorm(50,1,0.1),y=rnorm(50,5,0.1))
points2<-data.frame(x=rnorm(50,5,0.1),y=rnorm(50,5,0.1))
points3<-data.frame(x=rnorm(200,3,0.8),y=rnorm(200,3,0.8))
df<-rbind(points1,points2,points3)
p <- ggplot(df, aes(x, y))
p + geom_point(size=7, color="grey") + labs(title="Initial configuration")
y<-c(4.88871745,4.88099143,3.69713723)
x<-c(0.75606015,1.26736958,3.04961545)
kcenters<-data.frame(x,y)
p + geom_point(size=7, color="grey") + geom_point(data=kcenters, aes(x, y), size=7, color="black", shape="x") + labs(title="Initial centers")
dfCluster<-kmeans(df,centers=kcenters, iter.max = 1)
p + geom_point(size=7, aes(colour=as.factor(dfCluster$cluster))) + geom_point(data=data.frame(dfCluster$center), aes(x, y), size=7, color="black", shape="x")+ theme(legend.position="none") + labs(title="First iteration")
我的目標是在“初始中心”圖中顯示每個點的初始中心成員。
謝謝。
編輯:
我想我沒有正確解釋自己。
在此網站上有一個模擬顯示了我想要得到的東西:
http://www.onmyphd.com/?p=k-means.clustering
首次單擊“迭代”(click1)按鈕時,將放置初始中心。 再次按下(單擊2),將點分配到更接近的中心,並用不同的顏色繪制。 當您第三次單擊(click3)時,將計算新的中心,而當您第四次單擊(click4)時,將再次將點分配給更靠近的中心。
當您運行kmeans()
並在第一次迭代中將其停止時,您將獲得集群的新中心(click3), dfCluster$center
和每個點的集群成員資格(click4), dfCluster$cluster
,但您沒有獲得每個點的初始中心成員身份(單擊2),這正是我要尋找的。
我終於完成了我想要的:逐步的k均值。 抱歉,如果代碼不夠完美,我是R的新手。
#How does k-means work
library(ggplot2)
set.seed(2009)
points1<-data.frame(x=rnorm(50,1,0.1),y=rnorm(50,5,0.1))
points2<-data.frame(x=rnorm(50,5,0.1),y=rnorm(50,5,0.1))
points3<-data.frame(x=rnorm(200,3,0.8),y=rnorm(200,3,0.8))
df<-rbind(points1,points2,points3)
#plot initial points
p <- ggplot(df, aes(x, y))
p + geom_point(size=7, color="grey")
#set initial centers
kcenters<-df[c(49,26,297),]
#plot centers
p + geom_point(size=7, color="grey") + geom_point(data=kcenters, aes(x, y), size=7, color="black", shape="x")
#assignment (to calculate distances to initial centers and to allocate points to the cluster to which they are closest)
library(reshape)
distances <- melt(as.matrix(dist(df,diag=T,upper = T)), varnames = c("row", "col"))
dist_center1<-subset(distances,col==49,select = value)
dist_center2<-subset(distances,col==26,select = value)
dist_center3<-subset(distances,col==297,select = value)
dist_centers<-data.frame(dist_center1,dist_center2,dist_center3)
colnames(dist_centers)<-c("dist_center1","dist_center2","dist_center3")
dist_centers$cluster<-apply(dist_centers, 1, which.min)
df<-cbind(df,dist_centers)
#plot assignment
p + geom_point(size=7, aes(colour=as.factor(df$cluster))) + geom_point(data=kcenters, aes(x, y), size=7, color="black", shape="x") + theme(legend.position="none")
#calculate new centers
x<-tapply(df$x,df$cluster,mean)
y<-tapply(df$y,df$cluster,mean)
kcenters<-data.frame(x,y)
#plot new centers
p + geom_point(size=7, aes(colour=as.factor(df$cluster))) + geom_point(data=kcenters, aes(x, y), size=7, color="black", shape="x") + theme(legend.position="none")
然后,您可以繼續執行該過程,對上面的代碼稍作調整:
#assignment
df<-rbind(df[,1:2],kcenters)
row.names(df) <- NULL
distances <- melt(as.matrix(dist(df,diag=T,upper = T)), varnames = c("row", "col"))
dist_center1<-subset(distances,col==301,select = value)
dist_center2<-subset(distances,col==302,select = value)
dist_center3<-subset(distances,col==303,select = value)
dist_centers<-data.frame(dist_center1,dist_center2,dist_center3)
colnames(dist_centers)<-c("dist_center1","dist_center2","dist_center3")
dist_centers$cluster<-apply(dist_centers, 1, which.min)
df<-cbind(df[1:300,],dist_centers[1:300,])
#plot assignment
p + geom_point(size=7, aes(colour=as.factor(df$cluster))) + geom_point(data=kcenters, aes(x, y), size=7, color="black", shape="x") + theme(legend.position="none")
#calculate new centers
x<-tapply(df$x,df$cluster,mean)
y<-tapply(df$y,df$cluster,mean)
kcenters<-data.frame(x,y)
#plot new centers
p + geom_point(size=7, aes(colour=as.factor(df$cluster))) + geom_point(data=kcenters, aes(x, y), size=7, color="black", shape="x") + theme(legend.position="none")
如果使用相同的初始中心運行kmeans()
並在第一次迭代dfCluster<-kmeans(df,centers=kcenters, iter.max = 1)
將其dfCluster<-kmeans(df,centers=kcenters, iter.max = 1)
獲得以下中心:
> dfCluster$centers
x y
1 1.129419 4.905327
2 2.928011 2.880839
3 4.715513 4.766608
這些中心與我在過程的第一次迭代中獲得的中心不匹配(#calculate新的中心)。 我必須運行它14次(#assigment和#calculate新的中心)才能獲得它們。 我不知道kmeans()
過程中“迭代”的含義。 有人知道嗎
kcenters
是您的初始質心。
kmeans方法可能無法進行0次迭代,但是這些都是中心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.