def metrics point_1 point_2 return np sqrt point_1 point_2 sum def get

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def metrics(point_1, point_2):
return np.sqrt(((point_1 - point_2)**2).sum())
def get_nearest(x, centers):
return np.argmin([metrics(x, c) for c in centers])
def recalck_center(dots, idx):
return dots[idx].sum(axis=0) * 1.0 / len(dots[idx])
def get_dots_for_labels(l, lables):
return np.where(lables == l)[0]
def my_kmeans(dots, n, iters=100):
centers = 6.0*np.random.random((n, 3))
for i in xrange(iters):
lables = np.array([get_nearest(dot, centers) for dot in dots])
for l in range(n):
c_idx = get_dots_for_labels(l, lables)
centers[l] = recalck_center(dots, c_idx)
return lables