如何以k均值记录每次迭代的质心?

时间:2019-12-17 08:37:57

标签: python machine-learning scikit-learn data-science sklearn-pandas

通过使用“ kmeans.cluster_centers_”,我可以获得每个聚类的最终质心,但是如果我想跟踪所有迭代中的所有质心并将结果存储到列表中,该怎么办?

1 个答案:

答案 0 :(得分:2)

Scikit-learn无法为您提供中间结果,并且无法通过标准API来做到这一点。 一种获取方法是通过使用类似这样的东西:

k_means = KMeans(max_iter=1)
for i in range(300):
  k_means.fit(X)
  intermediate_centers = k_means.cluster_centers_
  k_means = KMeans(max_iter=1, init=intermediate_centers)

这不是一种快速的方法,我不建议在生产环境中运行它。