![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
k=2 data = np.random.randn(100,2) data[:50]+=2.0 center_index = np.random.choice(np.arange(len(data)), k) center = data[center_index] iters = 20 def dispatch(data,center): # n*2 # k*2 data = data[None] center = center[:,None] distance = ((data-center)**2).sum(axis= 2) return distance.argmin(axis=0) threshold = 0.01 finsh = True while finsh: classfity = dispatch(data,center) unique_classfity = np.unique(classfity) new_center = [] for i in range(len(unique_classfity)): if np.any(new_center): data_vs = data[classfity==i].mean(axis = 0,keepdims = True) new_center = np.vstack((new_center,data_vs)) else: new_center = data[classfity==i].mean(axis = 0,keepdims = True) diff = ((new_center - center) ** 2).sum() finsh=diff>threshold center = new_center print(f"diff:{diff:.5f}")
- 1.指定K个初始质心
- 2.利用质心对数据进行分类
- 3.对分类后的数据,计算其质心,称之为新质心
- 4.判断新质心与当前质心之间的差距,是否小于指定阈值,如果成立则跳出
- 如果不成立,则替换当前质心为新的质心
- 执行2步骤
- 5.输出最后得到的质心