**
聚类算法中 K均值聚类(KMeans)的python实现
**
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_blobs
from sklearn import cluster
from sklearn.metrics import adjusted_rand_score
from sklearn import mixture
"""""产生数据"""
def creat_data(centers,num,std):#center聚类中心数组,std,每个族中的标准差
x,labels_true=make_blobs(n_samples=num,centers=centers,cluster_std=std)
return x,labels_true #x为样本,labes为样本点的真实族分类标记
"""""先观察一下生成的样本点"""
def plot_data(*data):
x,labels_true=data
labels=np.unique(labels_true)
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
colors="rgbyckm"
for i,label in enumerate(labels):
position=labels_true==label#这儿出了问题,是什么原因
ax.scatter(x[position,0],x[position,1],label="cluster %d"%label,color=colors[i%len(colors)])#前面两个数是坐标
ax.legend(loc="best",framealpha=0.5)
ax.set_xlabel(