简单实现平面的点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()
k均值,k均值聚类算法
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]