kmeans是最简单的聚类算法之一,但是运用十分广泛。最近在工作中也经常遇到这个算法。kmeans一般在数据分析前期使用,选取适当的k,将数据分类后,然后分类研究不同聚类下数据的特点。
kmeans算法步骤:
1 随机选取k个中心点
2 遍历所有数据,将每个数据划分到最近的中心点中
3 计算每个聚类的平均值,并作为新的中心点
4 重复2-3,直到这k个聚类中心点不再变化(收敛了),或执行了足够多的迭代
实现代码:
from scipy.cluster.vq import *
from numpy.random import randn
from numpy import vstack
from numpy import array
from numpy import where
from matplotlib.pyplot import figure
from matplotlib.pyplot import plot
from matplotlib.pyplot import axis
from matplotlib.pyplot import show
class1=1.5*randn(100,2)
class2=randn(100,2)+array([5,5])
features=vstack((class1,class2))
centriods,variance=kmeans(features,2)
code,distance=vq(features,centriods)
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(centriods[:,0],centriods[:,1],'go')
axis('off')
show()

本文深入解析K均值聚类算法的基本原理、实现步骤,并通过具体示例展示其在数据分析领域的实际应用。从算法核心出发,逐步构建理解和实践能力。
849

被折叠的 条评论
为什么被折叠?



