import numpy as np
#调用一些下面写的子函数模拟实现kmeans的功能
def kmeans(x, k, maxIt):
numPoints, numDim = x.shape
dataSet = np.zeros((numPoints, numDim + 1))
dataSet[:, :-1] = x
#centroids = dataSet[np.random.randint(numPoints, size = k), :]
#对于中心点的选择应是以随机的方式,这里选择和上篇例子中同样的中心点为了验证结果。
centroids = dataSet[0:2, :]
centroids[:, -1] = range(1, k+1)
iterations = 0
oldCentroids = None
while not shouldStop(oldCentroids, centroids, iterations, maxIt):
print "iteration; \n", iterations
print "dataSet: \n", dataSet
print "centroids: \n", centroids
oldCentroids = np.copy(centroids)
iterations += 1
updataLabels(dataSet, centroids)
centroids = getCentroids(dataSet, k)
return dataSet
#对迭代停止时间的判断函数
def shouldStop(oldCentroids, centroids, iterations, maxIt):
if iterations > maxIt:
return True
return np.array_equal(oldCentroids, centroids)
#根据中心点修改类别标签
def updataLabels(dataSet, centroids):
numPoints, numDim = dataSet.shape
for i in range(0, numPoints):
dataSet[i,1] = getLabelFromClosestCentroid(dataSet[i,:1], centroids)
#计算得出最近的中心点将标签返回的函数
def getLabelFromClosestCentroid(dataSetRow, centroids):
label = centroids[0, -1];
minDist = np.linalg.norm(dataSetRow - centroids[0, :-1])
for i in range(1, centroids.shape[0]):
dist = np.linalg.norm(dataSetRow - centroids[i, :-1])
if dist < minDist:
minDist = dist
label = centroids[i, -1]
print "minDist: ", minDist
return label
#根据均值更新中心点
def getCentroids(dataSet, k):
result = np.zeros((k, dataSet.shape[1]))
for i in range(1, k+1):
oneCluster = dataSet[dataSet[:, -1] == i, :-1]
result[i-1, :-1] = np.mean(oneCluster, axis = 0)
result[i-1, -1] = i
return result
#下面是一个例子
x1 = np.array([1,1])
x2 = np.array([2,1])
x3 = np.array([4,3])
x4 = np.array([5,4])
testX = np.vstack((x1,x2,x3,x4))
result = kmeans(testX, 2, 10)
print "final result:"
print result
最后的例子和上篇的例子一样,计算结果也相同,是正确的结果。