How to seed the KMeans algorithm in Clustering.jl?

How do I make sure I get the same clusters everytime I run the following code:

using Clustering
using Random

# make a random dataset with 1000 random 5-dimensional points
X = rand(MersenneTwister(10),5, 1000)

# cluster X into 20 clusters using K-means
R = kmeans(X, 20; maxiter=200, display=:iter)

@assert nclusters(R) == 20 # verify the number of clusters

a = assignments(R) # get the assignments of points to clusters
c = counts(R) # get the cluster sizes
M = R.centers # get the cluster centers

In another package (BetaML) you can pass a RNG object to the kmeans function, e.g. kmeans(x, 20,rng= ```
MersenneTwister(10))`

1 Like

Thanks, I will check out this package.

In Clustering.jl, I came across this issue, but it doesn’t work for me. Setting Random.seed!(1234) still gave me different clusters every time.

Surprising. It works here. Just to confirm: you have to call Random.seed!(1234) again before each call to rand and kmeans.