kmeans:
缺点: 对初始值的选取敏感, 使用bikmean可以解决。
完整代码参考博客:http://blog.csdn.net/zouxy09/article/details/17590137
kmeans算法分析:
1、初始化聚类中心
def initCentroids(dataSet, k): numSamples, dim = dataSet.shape centroids = zeros((k, dim)) for i in range(k): index = int(random.uniform(0, numSamples)) centroids[i, :] = dataSet[index, :] return centroids
循环:
如果未迭代:即clusterChanged = True
1、计算各点到聚类中心的距离, 选择最近的距离, 更新clusterAssent:第一列存放该样本所在簇的类标,第二列存储该样本到对应簇中心的距离。判断是否迭代,更新
clusterChanged 。
while clusterChanged: clusterChanged = False ## for each sample for i in range(numSamples): minDist = 100000.0 minIndex = 0 ## for each centroid ## step 2: find the centroid who is closest for j in range(k): distance = euclDistance(centroids[j, :], dataSet[i, :]) if distance < minDist: minDist = distance minIndex = j #距离最小的聚类中心类标 ## step 3: update its cluster if clusterAssment[i, 0] != minIndex: clusterChanged = True clusterAssment[i, :] = minIndex, minDist ** 2
2、更新聚类中心
for j in range(k): pointsInCluster = dataSet[nonzero(clusterAssment[:, 0].A == j)[0]] centroids[j, :] = mean(pointsInCluster, axis=0)