2
votes

I am trying to construct clusters out of a set of data using the Kmeans algorithm from SkLearn. I want to know how one can determine whether the algorithm actually converged to a solution for one's data.

We feed in the tol parameter to define the tolerance for convergence but there is also a max_iter parameter that defines the number of iterations the algorithm will do for each run. I get that the algorithm may not always converge within the max_iter times of iterations. So is there any attribute or a function that I can access to know if the algorithm converged before the max_iter iterations ?

2

2 Answers

3
votes

You have access to the n_iter_ field of the KMeans class, it gets set after you call fit (or other routines that internally call fit.

Not your fault for overlooking that, it's not part of the documentation, I just found it by checking the source code ;)

0
votes

When I asked the question I was working with the class KMeans [http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html].This does not have any function or attribute that allows you to access the n_iter of each run of the algorithm. Instead we could use the function k_means [http://scikit-learn.org/stable/modules/generated/sklearn.cluster.k_means.html] instead of the class which has an option that enables returning the best n_iter. But this might have its own complications like having to write the predict by oneself etc.,