python内实现k-means聚类

《Python计算机视觉编程》学习笔记

from scipy.cluster.vq import *
import numpy as np
from matplotlib import pyplot as plt
class1=1.5*np.random.randn(100,2)
##print(class1)
class2=np.random.randn(100,2)+np.array([8,8])
##print(class2)
features=np.vstack((class1,class2))
centroids,variance=kmeans(features,2)
code,distance=vq(features,centroids)
plt.figure()
ndx=np.where(code==1)[0]
plt.plot(features[ndx,0],features[ndx,1],'*')
ndx=np.where(code==0)[0]
plt.plot(features[ndx,0],features[ndx,1],'r.')
plt.plot(centroids[:,0],centroids[:,1],'go')
plt.axis('off')
plt.show()









参考资料:

Python计算机视觉编程,第6章,P137


你可能感兴趣的:(图像处理,python,python计算机视觉)