python ML 笔记:Kmeans

时间:2022-06-06 08:18:50

kmeans算法的python实现:

参考与样本来源《Machine Learning in Action》

 #-*-coding:UTF-8-*-
'''
Created on 2015年8月19日
@author: Ayumi Phoenix
'''
import numpy as np def distL2(a,b):
""" 计算两个向量之间的L2距离 """
return np.sqrt(np.sum((a-b)**2)) class Kmeans():
def __init__(self, dataset,k):
self.dataset = dataset
self.k = k
self.m, self.n = dataset.shape def randcent(self):
""" 根据输入数据集获得随机生成一组簇质心 """
maxn = np.max(self.dataset, 0) # 获取每一维的最大值
minn = np.min(self.dataset, 0) # 获取每一维的最小值
centoroid = np.random.rand(self.k,self.n) * (maxn - minn) + minn # k x n
return centoroid def train(self, dist, iter = 1):
"""
# 1. 计算每个样本与所有簇心的最近匹配距离数组 m x 1:
# 计算某样本与所有簇心的距离,
# 找到最小距离所属的下标序号 0...k-1
# 2. 根据当前类标的分配,重新计算平均聚类中心
# 按照当前分配索引样本数据
# 迭代次数减一
# 3. 返回最终的质心与分配的序号
"""
centoroid = self.randcent()
while iter:
labels = np.zeros((self.m,), int)
for i in range(self.m):
d = [dist(self.dataset[i,:],centoroid[j])
for j in range(self.k)]
labels[i] = np.argmin(d)
for i in range(self.k):
x = self.dataset[labels==i]
centoroid[i] = np.mean(x, 0)
iter -= 1
return centoroid, labels

读取数据与测试函数:

 ef loadDataSet(filename):
dataMat = []
with open(filename) as f:
for line in f.readlines():
curline = line.strip().split('\t')
fltline = map(np.float, curline)
dataMat.append(fltline)
return dataMat if __name__=="__main__":
pass
datMat = np.array(loadDataSet('testSet.txt'))
km = Kmeans(datMat,4)
centoroid, labels = km.train(distL2, iter=20) # 根据当前质心显示样本分布
import matplotlib.pylab as pl
pl.figure()
c = ['ro','go','bo','yo','co','ko','wo','mo']
for i in range(datMat.shape[0]):
pl.plot(datMat[i][0],datMat[i][1],c[labels[i]])
for cen in centoroid:
pl.plot(cen[0],cen[1],'mo')
pl.show()

结果:

python ML 笔记:Kmeans