k-means: Same clusters for every execution

前端 未结 2 2122
灰色年华
灰色年华 2021-01-03 02:05

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 random

2条回答
  •  滥情空心
    2021-01-03 03:03

    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
    

提交回复
热议问题