平方和内的 Kmeans 总数是否会随着簇数的增加而增加?

Can Kmeans total within sum of squares increase with number of clusters?

当我使用以下 code.Is 时,我看到平方和内的总和增加了,这甚至可能还是我在代码中犯了一些错误?

v<-foreach(i = 1:30,.combine = c)  %dopar%  {
  iter <- kmeans (clustering_data,centers = i,iter.max = 1000)
  iter$tot.withinss
}

K-means 是一种随机算法。它不保证找到最佳。

所以你的随机数很差。

是的。请参阅 Anony-Mousse 的回答。

如果您使用 kmeans() 函数的 nstart = 25 参数,您将 运行 算法 25 次,让 R 从每个 运行 中收集错误度量,并在内部建立平均数。这样你就不需要构造foreach循环了。

来自 R 的文档 kmeans()

## random starts do help here with too many clusters
## (and are often recommended anyway!):
(cl <- kmeans(x, 5, nstart = 25))

必须为 nstart 选择一个合理的值。然后,不同随机初始化的错误更有可能被平均掉。 (但不能保证 tot.withinss 在 nstart 运行s 之后是最小的。)