from sklearn.cluster import KMeans
cluster=KMeans(n_clusters=3,random_state=0).fit(X)
y_pred=cluster.labels_
color=["red","pink","orange","gray"]
#根据聚类结果画图
fig,ax1=plt.subplots(1)
for i in range(3):
ax1.scatter(X[y_pred==i,0],X[y_pred==i,1]
,marker='o'
,s=8,
c=color[i])
ax1.scatter(cluster.cluster_centers_[:,0],cluster.cluster_centers_[:,1]
,marker="x"
,s=20,
c="black"
)
plt.show()
from sklearn.cluster import KMeans