How to visualize k-means centroids for each iteration?

夙愿已清 提交于 2019-12-08 09:54:39

问题


I would like to graphically demostrate the behavior of k-means by plotting iterations of the algorithm from a starting value (at (3,5),(6,2),(8,3)) of initial cluster till the cluster centers. Each iteration may correspond to a single plot with centroids and clusters.

Given:

                x<-c(3,6,8,1,2,2,6,6,7,7,8,8)
                y<-c(5,2,3,5,4,6,1,8,3,6,1,7)


                df<-data.frame(x,y)
                dfCluster<-kmeans(df,centers=3) # with 3 centroids

I would like to use the first three tuples as my initial cluster and track the movement of the centroids.


回答1:


Try to use tryCatch to automate the the process of stopping when conversion is reached: I use the iris-data set because there kmeans needs 2 iterations (the (6,3.5)-Point switches)

set.seed(1337)
df = iris[,1:2]


dfCluster<-kmeans(df,centers=3, iter.max = 1)
  plot(df[,1], df[,2], col=dfCluster$cluster,pch=19,cex=2, main="iter 1")
  points(dfCluster$centers,col=1:5,pch=3,cex=3,lwd=3)

max_iter = 10

for (i in 2:max_iter){
  tryCatch({
    dfCluster <- kmeans(df,centers = dfCluster$centers, iter.max = 1)
    done <- TRUE
  }, 
  warning=function(w) {done <- FALSE})
  plot(df[,1], df[,2], col=dfCluster$cluster,pch=19,cex=2, main=paste("iter",i))
  points(dfCluster$centers,col=1:5,pch=3,cex=3,lwd=3)
  if(done) break
}

Result:

If you want to get the coordinates at each iteration-step see here: Getting the coordinates of every observation at each iteration of kmeans in R



来源:https://stackoverflow.com/questions/27060527/how-to-visualize-k-means-centroids-for-each-iteration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!