在 Python 中为每次迭代绘制 KMeans 聚类中心

问题描述

我创建了一个包含 6 个集群的数据集并使用下面的代码对其进行可视化,并找到每次迭代的集群中心点,现在我想可视化 KMeans 算法中集群质心更新的演示。该演示应包括通过生成 2×2 轴图形的前四次迭代。 我找到了点,但我无法绘制它们,请您查看我的代码并通过查看它来帮助我编写散点图的算法吗?

这是我目前的代码

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.datasets import make_blobs
data = make_blobs(n_samples=200,n_features=8,centers=6,cluster_std=1.8,random_state=101)
data[0].shape
plt.scatter(data[0][:,0],data[0][:,1],c=data[1],cmap='brg')

plt.show()
from sklearn.cluster import KMeans

print("First iteration points:")
kmeans = KMeans(n_clusters=6,random_state=0,max_iter=1)
kmeans.fit(data[0])
centroids=kmeans.cluster_centers_
print(kmeans.cluster_centers_)
print("Second iteration points:")
kmeans = KMeans(n_clusters=6,max_iter=2)
kmeans.fit(data[0])
print(kmeans.cluster_centers_)
print("Third iteration points:")
kmeans = KMeans(n_clusters=6,max_iter=3)
kmeans.fit(data[0])
print(kmeans.cluster_centers_)
print("Forth iteration points:")
kmeans = KMeans(n_clusters=6,max_iter=4)
kmeans.fit(data[0])
print(kmeans.cluster_centers_)

解决方法

您可以使用 plt.scatter()plt.subplots() 来实现这一点,如下所示:

import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
data = make_blobs(n_samples=200,n_features=8,centers=6,cluster_std=1.8,random_state=101)

fig,ax = plt.subplots(nrows=2,ncols=2,figsize=(10,10))

from sklearn.cluster import KMeans
c=d=0
for i in range(4):
    ax[c,d].title.set_text(f"{i+1} iteration points:")
    kmeans = KMeans(n_clusters=6,random_state=0,max_iter=i+1)
    kmeans.fit(data[0])
    centroids=kmeans.cluster_centers_
    ax[c,d].scatter(data[0][:,0],data[0][:,1],c=data[1],cmap='brg')
    ax[c,d].scatter(kmeans.cluster_centers_[:,kmeans.cluster_centers_[:,s=200,c='black')
    d+=1
    if d==2:
        c+=1
        d=0

这将产生: enter image description here