K-means: same clusters for each run

Is it possible to get the same kmeans clusters for each run for a specific dataset. As for a random value, we can use a fixed seed. Can randomness be stopped for clustering?

+5
source share
2 answers

Yes. Use set.seedto set the seed for a random value before 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)

Equality Check:

identical(XX, YY)
[1] TRUE
+13
source

, set.seed(foo) kmeans(....) , , . foo - , 42 .

+4

All Articles