In K-means the initial placement of centroid plays a very important role in it's convergence. Sometimes, the initial centroids are placed in a such a way that during consecutive iterations of K-means the clusters the clusters keep on changing drastically and even before the convergence condition may occur, max_iter
is reached and we are left with incorrect cluster. Hence, the clusters obtained in such may not be correct. To overcome this problem, this parameter is introduced. The value of n_iter
basically determines how many different sets of randomly chosen centroids, should the algorithm use. For each different set of points, a comparision is made about how much distance did the clusters move, i.e. if the clusters travelled small distances than it is highly likely that we are closest to ground truth/best solution. The points which provide the best performance and their respective run along with all the cluster labels are returned.
If you are interested, you can also look at k-means++ algorithm designed specifically to deal with this problem.
You can also look at this link for more details about the initial centroids matter.
n_init
states how many different sets of random points the algorithm should use. It then gives the best run in terms of inertia (how little the algo was moving at the end of the run -small steps --> closer to best solution) – Berkie