问题
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