在R中绘制k均值的迭代

时间:2018-08-28 04:03:19

标签: r k-means

我在Rentrop的一个不同k均值绘图问题的答案中找到了这段代码,但想知道为什么它只在任何给定的数据集上绘制两次迭代。有没有一种方法可以概括它在收敛时停止绘制,而不仅仅是在两次迭代后停止绘制?

IF (SELECT user_id FROM usergroup WHERE user_id = 234) IS NULL  
BEGIN
    SELECT * 
    FROM source
    WHERE source.source_id IN (SELECT DISTINCT sl.source_id FROM sourcelevel AS SL)
END
ELSE BEGIN
    SELECT * FROM source
END

1 个答案:

答案 0 :(得分:0)

如果您只想尽可能少地修改现有代码,则在tryCatch中反转TRUE / FALSE:

tryCatch({
dfCluster <- kmeans(df,centers = dfCluster$centers, iter.max = 1)
done <- FALSE #was TRUE
}, 
warning=function(w) {done <- TRUE}) #was FALSE

但是,此代码在max_iter中绘制了许多图,无论kmeans是收敛于2迭代还是20收敛。这是我的重写:

# data
set.seed(1234)
df = iris[ , 1:2]

# functions for plotting
myplot <- function(out, i) {
    plot(df[,1], df[,2], pch=20, main=paste("iter",i),
         col=scales::alpha(out$cluster, "0.5"))
}

mypoints <- function(out) {
    points(out$centers, col=1:3, pch=3, cex=2, lwd=3)
}

# number of plots = min(user-specified iters, iters until convergence)
actual_iters <- kmeans(df, centers=3)$iter
max_iter = 10
N <- min(max_iter, actual_iters)

# initial kmeans
out <- kmeans(df, centers=3, iter.max=1)
myplot(out,1)
mypoints(out)

# loop through next iters of kmeans
for (i in 2:N){
    out <- kmeans(df, centers=out$centers, iter.max=1)
    myplot(out,i)
    mypoints(out)
}