python的sklearn机器学习之K-Mean 聚类算法示例
from sklearn.datasets import make_blobs
from matplotlib import pyplot as plt
from sklearn.cluster import KMeans
x, y = make_blobs(n_samples=200, n_features=2, centers=4, cluster_std=1, center_box=(-10.0, 10.0)) # 生成样本数据
plt.figure(figsize=(6, 6))
plt.scatter(x[:, 0], x[:, 1], marker='o')
# plt.show()
n_clusters = 3 # 定义3个类别
model = KMeans(n_clusters)
model.fit(x) # 训练模型
score = model.score(x) # cost按负数显示,其绝对值越大,成本越高。
print('kmean:k={} ;cost={}'.format(n_clusters, score))
labels = model.labels_
centers = model.cluster_centers_
markers = ['o', '^', '*']
colors = ['r', 'b', 'g']
# print(len(labels),'\n',centers)
##画出样本点
for c in range(n_clusters):
cluster = x[labels == c]
plt.scatter(cluster[:, 0], cluster[:, 1], marker=markers[c], c=colors[c])
##画出中心点
plt.scatter(centers[:, 0], centers[:, 1], marker='o', c='y')
plt.show()