-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_Kmeans_v2.py
77 lines (61 loc) · 2.71 KB
/
dot_Kmeans_v2.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import numpy as np
def dot_Kmeans(verts, horizs, nodes, max_coords, niter=10, ntimes=10):
clusters_s = {}
print('NODEEES v2')
print(nodes)
for exec in range(ntimes):
xs = np.random.uniform(low=0, high=max_coords[1], size=len(horizs))
ys = np.random.uniform(low=0, high=max_coords[0], size=len(verts))
print(xs.shape, ys.shape)
n_clusters = len(horizs) + len(verts)
clusters = np.array([-1] * len(nodes), dtype=int) # assign each point to a cluster
dist_clust = np.array([np.inf] * len(nodes))
for k in range(niter):
# maximization
for cluster in range(len(horizs)):
d = nodes - np.array([xs[cluster], horizs[cluster]])
d = np.sum(d*d, axis=1)
for j in range(len(d)):
if clusters[j] < 0 or d[j] < dist_clust[j]:
clusters[j] = cluster
dist_clust[j] = d[j]
for cluster in range(len(verts)):
d = nodes - np.array([verts[cluster], ys[cluster]])
d = np.sum(d*d, axis=1)
for j in range(len(d)):
if clusters[j] < 0 or d[j] < dist_clust[j]:
clusters[j] = cluster + len(horizs)
dist_clust[j] = d[j]
dist = np.sum(dist_clust)
print('TOT DISTANCE!', dist)
# expectation
for cluster in range(len(horizs)):
members = clusters == cluster
if np.any(members):
xs[cluster] = np.mean(nodes[members, 0])
else:
xs[cluster] = np.random.uniform(low=0, high=max_coords[1])
for cluster in range(len(verts)):
members = clusters == (cluster + len(horizs))
if np.any(members):
ys[cluster] = np.mean(nodes[members, 1])
else:
ys[cluster] = np.random.uniform(low=0, high=max_coords[0])
key = tuple(clusters)
if not key in clusters_s:
clusters_s[key] = (1, xs, ys, dist)
elif dist < clusters_s[key][3]:
clusters_s[key] = (clusters_s[key][0] + 1, xs, ys, dist)
else:
clusters_s[key] = (clusters_s[key][0] + 1, clusters_s[key][1], clusters_s[key][2], clusters_s[key][3])
clusters, n_max, xs, ys, dist = [], 0, [], [], np.inf
for cluster, (n, x, y, d) in clusters_s.items():
if n > n_max:
n_max = n
clusters = cluster
xs = x
ys = y
if d > dist:
print('it is worse in distance!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
dist = d
return clusters, xs, ys, dist