#!/usr/bin/python
def biKmeans(dataSet, k, distMeas=distEclud):
m = shape(dataSet)[0]
clusterAssment = mat(zeros((m,2)))#创建一个矩阵存储每个点的簇分配结果及平方误差
centroid0 = mean(dataSet, axis=0).tolist()[0]#计算整个数据集的质心
centList =[centroid0] #create a list with one centroid#使用一个列表来保留所有的质心
for j in range(m):#calc initial Error 遍历数据集中所有点
clusterAssment[j,1] = distMeas(mat(centroid0), dataSet[j,:])**2#计算每个点到质心的误差值
while (len(centList) < k):#该循环会不停对簇进行划分,直到得到想要的簇数目为止,为此需要比较划分前后的sse
lowestSSE = inf#开始将最小SSE设为无穷大
for i in range(len(centList)):#遍历簇列表centList中的每个簇来决定最佳的簇进行划分
ptsInCurrCluster = dataSet[nonzero(clusterAssment[:,0].A==i)[0],:]#get the data points currently in cluster i 对每个簇,对该簇中的所有点看成一个小的数据集ptsInCurrCluster
centroidMat, splitClustAss = kMeans(ptsInCurrCluster, 2, distMeas)#将ptsInCurrCluster输入到函数kmeans()(k=2)中进行处理生成2个质心簇,并给出每个簇的误差值
#误差与剩余数据集的误差之和将作为本次划分的误差
sseSplit = sum(splitClustAss[:,1])#compare the SSE to the currrent minimum
sseNotSplit = sum(clusterAssment[nonzero(clusterAssment[:,0].A!=i)[0],1])
print "sseSplit, and notSplit: ",sseSplit,sseNotSplit
if (sseSplit + sseNotSplit) < lowestSSE:#如果该划分的sse值最小,则本次划分保存
...
一旦决定了要划分的簇,就要执行实际划分操作,即将要划分的簇中所有点的簇分配结果进行修改即可。当使用KMEANS()函数并簇数为2时,得到两个编号0与1的结果簇,需要将这些簇编号修改改为划分簇与新加簇的编号,该过程通过2个数组过滤器完成
...
bestCentToSplit = i
bestNewCents = centroidMat
bestClustAss = splitClustAss.copy()
lowestSSE = sseSplit + sseNotSplit#
bestClustAss[nonzero(bestClustAss[:,0].A == 1)[0],0] = len(centList) #change 1 to 3,4, or whatever
bestClustAss[nonzero(bestClustAss[:,0].A == 0)[0],0] = bestCentToSplit
print 'the bestCentToSplit is: ',bestCentToSplit
print 'the len of bestClustAss is: ', len(bestClustAss)
centList[bestCentToSplit] = bestNewCents[0,:].tolist()[0]#replace a centroid with two best centroids #新的簇分配结果被更新
centList.append(bestNewCents[1,:].tolist()[0])#新的质心添加到centlist中
clusterAssment[nonzero(clusterAssment[:,0].A == bestCentToSplit)[0],:]= bestClustAss #reassign new clusters, and SSE
return mat(centList), clusterAssment #while循环结束后,同kmeans()函数一样,函数返回质心列表与簇分配结果