shyamupa
shyamupa

Reputation: 1628

Reproducible kmeans in sklearn

I am using the document clustering code available here. I know that k-means is solving a non-convex problem and hence the results of optimization will differ every time I run it, but is there a way to make the clustering reproducible (maybe by fixing some random seed)?

Upvotes: 1

Views: 2213

Answers (1)

João Almeida
João Almeida

Reputation: 5067

You can fix the random_state parameter of K-means. In the following code I use 42:

km = KMeans(n_clusters=true_k, init='k-means++', max_iter=100, n_init=1, 
                               verbose=opts.verbose,
                               random_state = 42)

You can check the documentation here.

Upvotes: 2

Related Questions