from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import KMeans
from sklearn import metrics
from sklearn import datasets

blobs, ground_truth = datasets.make_blobs(1000, centers=3,cluster_std=1.75)


#先看看数据长什么样子
f, ax = plt.subplots(figsize=(7, 5))
colors = ['r', 'g', 'b']
for i in range(3):
    p = blobs[ground_truth == i]
    ax.scatter(p[:,0], p[:,1], c=colors[i],label="Cluster {}".format(i))
ax.set_title("Cluster With Ground Truth")
ax.legend()
f.show()
f.savefig("9485OS_03-16")


#绘制聚簇中心
kmeans = KMeans(n_clusters=3)
kmeans.fit(blobs)
print(kmeans.cluster_centers_)

f, ax = plt.subplots(figsize=(7, 5))
colors = ['r', 'g', 'b']
for i in range(3):
    p = blobs[ground_truth == i]
    ax.scatter(p[:,0], p[:,1], c=colors[i],label="Cluster {}".format(i))
    ax.scatter(kmeans.cluster_centers_[:, 0],kmeans.cluster_centers_[:, 1], s=100, color='black',label='Centers')
ax.set_title("Cluster With Ground Truth")
ax.legend()
f.savefig("9485OS_03-17")
f.show()

posted on 2016-03-29 15:07  qqhfeng16  阅读(2025)  评论(0编辑  收藏  举报