Kmeans算法在python的實(shí)現(xiàn)--簡(jiǎn)易二類區(qū)分

本代碼可以通過圖像展現(xiàn)出聚合結(jié)果,幫助理解。

import random

import sys

import matplotlib.pyplot as plt

#K均值聚類法

def randList(size):

? ? all_points = []

? ? for i in range(size):

? ? ? datas = [random.randint(1, 100), random.randint(1, 100)]

? ? ? if not datas in all_points:? # 去掉重復(fù)數(shù)據(jù)

? ? ? ? ? ? all_points.append(datas)

? ? print(all_points)

? ? return all_points

#最簡(jiǎn)單的二類區(qū)分 需要不斷迭代過程

def? Kmeans(AtypeList,BtypeList,randCenterA,randCenterB,initList,counts):

? ? lastAtypeList = AtypeList

? ? lastBtypeList = BtypeList

? ? AtypeList=[]

? ? BtypeList=[]

? ? for iL in initList:

? ? ? ? distanceToA = ((randCenterA[0] - iL[0]) * (randCenterA[0] - iL[0]) + (randCenterA[1] - iL[1]) * (

? ? ? ? randCenterA[1] - iL[1])) ** (0.5)

? ? ? ? distanceToB = ((randCenterB[0] - iL[0]) * (randCenterB[0] - iL[0]) + (randCenterA[1] - iL[1]) * (

? ? ? ? randCenterA[1] - iL[1])) ** (0.5)

? ? ? ? if distanceToA > distanceToB:

? ? ? ? ? ? AtypeList.append(iL)

? ? ? ? else:

? ? ? ? ? ? BtypeList.append(iL)

? ? #求得各類元素?cái)?shù)量:

? ? Anum = len(AtypeList)

? ? Bnum = len(BtypeList)

? ? newAxSum=0

? ? newAySum=0

? ? newBxSum=0

? ? newBySum=0

? ? for lA in AtypeList:

? ? ? ? newAxSum=newAxSum+lA[0]

? ? ? ? newAySum=newAySum+lA[1]

? ? for lB in BtypeList:

? ? ? ? newBxSum = newBxSum + lB[0]

? ? ? ? newBySum = newBySum + lB[1]

? ? randCenterA=[newAxSum/Anum,newAySum/Anum]

? ? randCenterB=[newBxSum/Bnum,newBySum/Bnum]

? ? #反復(fù)迭代,直至聚類元素不變?yōu)橹?/p>

? ? if (lastAtypeList==AtypeList and lastBtypeList==BtypeList) or counts > 1000 :

? ? ? ? print('迭代結(jié)束')

? ? ? ? print('質(zhì)心A為:'+str(randCenterA))

? ? ? ? print('質(zhì)心B為:' + str(randCenterB))

? ? ? ? print('聚類A元素為:' + str(AtypeList))

? ? ? ? print('聚類B元素為:' + str(BtypeList))

? ? ? ? print('迭代次數(shù):' + str(counts))

? ? ? ? #開始繪制圖譜

? ? ? ? for Aty in AtypeList:

? ? ? ? ? ? plt.scatter(Aty[0],Aty[1],c='b')

? ? ? ? for Bty in BtypeList:

? ? ? ? ? ? plt.scatter(Bty[0],Bty[1],c='g')

? ? ? ? plt.scatter(randCenterA[0], randCenterA[1], c='r')

? ? ? ? plt.scatter(randCenterB[0], randCenterB[1], c='r')

? ? ? ? plt.show()

? ? else:

? ? ? counts=counts+1

? ? ? Kmeans(AtypeList,BtypeList,randCenterA,randCenterB,initList,counts)

def ExampleSloveAndPaint(size):

? initList = randList(size)

? x=0

? #初始聚類中心 不一樣的情況下,聚合結(jié)果會(huì)有區(qū)別

? while x<1:

? ? ? print('原始數(shù)組為:' + str(initList))

? ? ? randCenterA = [random.randint(1, 100), random.randint(1, 100)]

? ? ? randCenterB = [random.randint(1, 100), random.randint(1, 100)]

? ? ? Kmeans([], [], randCenterA, randCenterB, initList, 0)

? ? ? x=x+1

def main ():

sys.setrecursionlimit(2000) #設(shè)置迭代上限

ExampleSloveAndPaint(20) #設(shè)置聚類數(shù)組的元素個(gè)數(shù)

main()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容