k-means: Same clusters for every execution

◇◆丶佛笑我妖孽 提交于 2019-12-19 03:22:49

问题


Is it possible to get same kmeans clusters for every execution for a particular data set. Just like for a random value we can use a fixed seed. Is it possible to stop randomness for clustering?


回答1:


Yes. Use set.seed to set a seed for the random value before doing the clustering.

Using the example in kmeans:

set.seed(1)
x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
           matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")


set.seed(2)
XX <- kmeans(x, 2)

set.seed(2)
YY <- kmeans(x, 2)

Test for equality:

identical(XX, YY)
[1] TRUE



回答2:


Yes, calling set.seed(foo) immediately prior to running kmeans(....) will give the same random start and hence the same clustering each time. foo is a seed, like 42 or some other numeric value.



来源:https://stackoverflow.com/questions/7501035/k-means-same-clusters-for-every-execution

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