python实现K-means算法
生活随笔
收集整理的這篇文章主要介紹了
python实现K-means算法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
K-means算法流程:
- 隨機選k個樣本作為初始聚類中心
- 計算數據集中每個樣本到k個聚類中心距離,并將其分配到距離最小的聚類中心
- 對于每個聚類,重新計算中心
- 回到2,至得到局部最優解
python代碼:
import random
import numpy as np
import matplotlib.pyplot as pltplt.ion()#開啟交互,matplotlib默認阻塞模式,直到調用plt.show()才會顯示def getDistance(point1,point2): #求距離return ((point1[0]-point2[0])**2+(point1[1]-point2[1])**2)**0.5def cluster(): #根據中心聚類distance=np.zeros((N,k))for i in range(N):minimum=9999for j in range(k):distance[i,j]=getDistance(point[i],centers[j])for j in range(k):if distance[i,j]<minimum:minimum=distance[i,j]center[i]=centers[j]def getE(): #求誤差平方和sum_=0for i in range(k):for j in range(N):if np.all(center[j]==centers[i]):sum_+=getDistance(point[j],centers[i])**2return sum_def getNewCenters():#獲得新的中心點for i in range(k):count=0temp_x=0temp_y=0for j in range(N):if np.all(center[j]==centers[i]):count+=1temp_x+=point[j,0]temp_y+=point[j,1]temp_x/=count;temp_y/=count;centers[i]=np.array([temp_x,temp_y])def show(): #展示for i in range(k):for j in range(N):if np.all(center[j]==centers[i]):plt.scatter(point[j,0],point[j,1],c=cnames[i],s=10)plt.scatter(centers[:,0],centers[:,1],c='black',s=50)k=3 #聚類中心個數
N=100 #數據集個數
cnames=['red','yellow','blue','chocolate','darkcyan','darksalmon','red','pink','yellow']center=np.zeros((N,2)) #各數據分配的中心
point=np.random.rand(N,2) #數據集中的樣本
index=np.random.choice(N,k,replace=False)
centers=point[index[:]] #隨機抽取K個作為聚類中心cluster()
show()t1=0
t=getE()
while t-t1:t1=tgetNewCenters()cluster()t=getE()plt.pause(0.2)plt.clf()show()plt.ioff()
代碼效果:
總結
以上是生活随笔為你收集整理的python实现K-means算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Matlab视频流处理:读取,播放,保存
- 下一篇: python拟合曲线(小批量随机梯度下降