demo中没有数据,先自己生成一些用于聚类测试的点:
class1 = 1.5 * randn(100,2)
class2 = randn(100,2) + array([5,5])
features = vstack((class1,class2)) #把类1、2合在一起
scipy.cluster.vq包进行聚类:
centroids,variance = kmeans(features,2)
code,distance = vq(features,centroids)
画出结果:
figure()
ndx = where(code==0)[0]
plot(features[ndx,0],features[ndx,1],'*')
ndx = where(code==1)[0]
plot(features[ndx,0],features[ndx,1],'r.')
plot(centroids[:,0],centroids[:,1],'go')
axis('off')
show()
参考:《python计算机视觉》