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

倾然丶 夕夏残阳落幕 提交于 2020-12-31 05:12:36

问题


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.


回答1:


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]])


来源:https://stackoverflow.com/questions/51697670/how-to-view-cluster-centroids-for-each-iteration-of-n-init-using-skleans-kmeans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!