Search code examples
pythonk-meanssklearn-pandas

How to view cluster centroids for each iteration of n_init using skleans' KMeans


I am currently trying to view the created centroids(cluster centers) for each iteration of KMeans that is determined from each iteration of n_init. As of now I am able to view the final results but I would like to see these at each iteration so I am able to report the differences of KMeans when using init='random' and preset cluster centers at each iteration. The following is a brief example of what I currently have \

#Creating model for Kmeans
Model=[]

Model=KMeans(n_clusters=5,max_iter=10,n_init=10)
#Data trials below represents my data for model training
Model.fit(Data_Trials)

#Get Created Clusters
labels=Model.predict(Data_Trials)
inertia=Model.inertia_

### Gets created Cluster centroids for Final iteration of n_init
zTrial=pd.DataFrame(Model.cluster_centers_)

If anyone knows how to get this for each iteration I would greatly appreciate it.


Solution

  • You can play with number of iterations.Take this 12 point example

    X = np.array([[1, 2], [1, 4], [1, 0],[1, 3], [1, 8], [1, 9],[2, 2], [2, 4], [2, 0],[4, 2], [4, 4], [4, 0]])
    

    One iteration

    kmeans = KMeans(n_clusters=3, random_state=0,max_iter=1).fit(X)
    kmeans.predict([[0, 0], [2, 2],[6, 4]])
    

    As a result we have

     kmeans.cluster_centers_
    array([[ 2.75      ,  0.5       ],
           [ 1.83333333,  3.16666667],
           [ 1.        ,  8.5       ]])
    

    If you increase number of iterations

    kmeans = KMeans(n_clusters=3, random_state=0,max_iter=10).fit(X)
    kmeans.cluster_centers_
    array([[ 1.        ,  8.5       ],
           [ 2.75      ,  0.5       ],
           [ 1.83333333,  3.16666667]])