# Python简单代码实现k-means聚类算法

、关于聚类算法的原理，大家可以看这篇文章（http://t.csdn.cn/yrLpQ）。
、在我的代码里面就是只有五个点：x1（2,10），x2（2,5），x3（8,4），x4（5,8），x5（7,5），我们分两簇，以第二个点和第四个点为第一次的中心。

`import numpy as np
import random
x = [2, 2, 8, 5, 7]
y = [10, 5, 4, 8, 5]
d1 = np.zeros(5)
d2 = np.zeros(5)
k2_x = np.zeros(5)
k2_y = np.zeros(5)
k4_x = np.zeros(5)
k4_y = np.zeros(5)

k2_means_x = 2
k2_means_y = 5
k4_means_x = 5
k4_means_y = 8
q=0
for j in range(20):

for i in range(5):
d1[i] = ((x[i] - k2_means_x) ** 2 + (y[i] - k2_means_y) ** 2) ** 0.5
d2[i] = ((x[i] - k4_means_x) ** 2 + (y[i] - k4_means_y) ** 2) ** 0.5

for i in range(5):
if d1[i] < d2[i]:
k2_x[i] = x[i]
k2_y[i] = y[i]
else:
k4_x[i] = x[i]
k4_y[i] = y[i]

k2_means_x = 0
k2_means_y = 0
k4_means_x = 0
k4_means_y = 0
k = 0
a = 0
s = 0
z = 0
for i in range(5):
if k2_x[i] != 0:
k = k + 1
k2_means_x += k2_x[i]
if k2_y[i] != 0:
a = a + 1
k2_means_y += k2_y[i]
if k4_x[i] != 0:
s = s + 1
k4_means_x += k4_x[i]
if k4_y[i] != 0:
z = z + 1
k4_means_y += k4_y[i]
k2_means_x = k2_means_x / k
k2_means_y = k2_means_y / a
k4_means_x = k4_means_x / s
k4_means_y = k4_means_y / z

if q==k4_means_y:
print(“…”)
print(k2_means_x)
print(k2_means_y)
print(k4_means_x)
print(k4_means_y)
print(“…”)
break
q=k4_means_y
`

THE END

)">