python實(shí)現(xiàn)k均值算法示例(k均值聚類算法)
簡單實(shí)現(xiàn)平面的點(diǎn)K均值分析,使用歐幾里得距離,并用pylab展示。
import pylab as pl
#calc Euclid squire
def calc_e_squire(a, b):
return (a[0]- b[0]) ** 2 + (a[1] - b[1]) **2
#init the 20 point
a = [2,4,3,6,7,8,2,3,5,6,12,10,15,16,11,10,19,17,16,13]
b = [5,6,1,4,2,4,3,1,7,9,16,11,19,12,15,14,11,14,11,19]
#define two k_value
k1 = [6,3]
k2 = [6,1]
#defint tow cluster
sse_k1 = []
sse_k2 = []
while True:
sse_k1 = []
sse_k2 = []
for i in range(20):
e_squire1 = calc_e_squire(k1, [a[i], b[i]])
e_squire2 = calc_e_squire(k2, [a[i], b[i]])
if (e_squire1 <= e_squire2):
sse_k1.append(i)
else:
sse_k2.append(i)
#change k_value
k1_x = sum([a[i] for i in sse_k1]) / len(sse_k1)
k1_y = sum([b[i] for i in sse_k1]) / len(sse_k1)
k2_x = sum([a[i] for i in sse_k2]) / len(sse_k2)
k2_y = sum([b[i] for i in sse_k2]) / len(sse_k2)
if k1 != [k1_x, k1_y] or k2 != [k2_x, k2_y]:
k1 = [k1_x, k1_y]
k2 = [k2_x, k2_y]
else:
break
kv1_x = [a[i] for i in sse_k1]
kv1_y = [b[i] for i in sse_k1]
kv2_x = [a[i] for i in sse_k2]
kv2_y = [b[i] for i in sse_k2]
pl.plot(kv1_x, kv1_y, 'o')
pl.plot(kv2_x, kv2_y, 'or')
pl.xlim(1, 20)
pl.ylim(1, 20)
pl.show()
- Python實(shí)現(xiàn)Kmeans聚類算法
- Python實(shí)現(xiàn)的Kmeans++算法實(shí)例
- python實(shí)現(xiàn)kMeans算法
- python kmeans聚類簡單介紹和實(shí)現(xiàn)代碼
- Python KMeans聚類問題分析
- python中kmeans聚類實(shí)現(xiàn)代碼
- Python聚類算法之凝聚層次聚類實(shí)例分析
- Python聚類算法之DBSACN實(shí)例分析
- Python聚類算法之基本K均值實(shí)例詳解
- K-means聚類算法介紹與利用python實(shí)現(xiàn)的代碼示例
- Python實(shí)現(xiàn)的KMeans聚類算法實(shí)例分析
相關(guān)文章
python連接、操作mongodb數(shù)據(jù)庫的方法實(shí)例詳解
這篇文章主要介紹了python連接、操作mongodb數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式詳細(xì)分析了Python針對(duì)MongoDB數(shù)據(jù)庫的連接、查詢、排序等相關(guān)操作技巧,需要的朋友可以參考下2019-09-09python中str內(nèi)置函數(shù)用法總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于python中str內(nèi)置函數(shù)用法總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2020-12-12使用 Python 玩轉(zhuǎn) GitHub 的貢獻(xiàn)板(推薦)
這篇文章主要介紹了使用 Python 玩轉(zhuǎn) GitHub 的貢獻(xiàn)板的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04python注冊(cè)釘釘回調(diào)事件的實(shí)現(xiàn)
釘釘有回調(diào)事件流程,本文主要介紹了python注冊(cè)釘釘回調(diào)事件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08