import numpy as npy
def kmeans(X,k,maxIteration):
numpoint,numdim=X.shape
numSet=npy.zeros((numpoint,numdim+1))
numSet[:,:-1]=X
centroids=numSet[npy.random.randint(numpoint,size=k),:]
centroids[:,-1]=range(1,k+1)
iteration=0
oldcentroids=None
while not shouldstop(oldcentroids,centroids,iteration,maxIteration):
oldcentroids=npy.copy(centroids)
iteration+=1
updatenumSet(numSet,centroids)
centroids=getcentroids(numSet,k)
return numSet
def shouldstop(oldcentroids,centroids,iteration,maxIteration):
if(iteration>maxIteration):
return True
return npy.array_equal(oldcentroids,centroids)
def updatenumSet(numSet,centroids):
numpoint,numdim=numSet.shape
for i in range(0,numpoint):
numSet[i,-1]=getlabelfromcentroids(numSet[i,:-1],centroids)
def getlabelfromcentroids(numSetRow,centroids):
label=centroids[0][-1]
mindistannce=npy.linalg.norm(numSetRow-centroids[0,:-1])
for i in range(1,centroids.shape[0]):
dist=npy.linalg.norm(numSetRow-centroids[i,:-1])
if(distmindistance=dist
label=centroids[i,-1]
return label
def getcentroids(numSet,k):
result=npy.zeros((k,numSet.shape[1]))
for i in range(1,k+1):
onCluster=numSet[numSet[:,-1]==i,:-1]
result[i-1,:-1]=npy.mean(onCluster,axis=0)#行相加取均值
result[i-1,-1]=i
return result
x1=npy.array([1,1])
x2=npy.array([2,1])
x3=npy.array([4,3])
x4=npy.array([5,4])
test_x=npy.vstack((x1,x2,x3,x4))#纵向合并
result=kmeans(test_x,2,100)
print(result)