-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit_routines.py
470 lines (347 loc) · 14.2 KB
/
init_routines.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import numpy as np
import random
import seaborn as sns
import matplotlib.pyplot as plt
#from pyclustering.cluster.kmedoids import kmedoids
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.manifold import TSNE
from scipy.spatial.distance import euclidean, cdist
from dtw import dtw
import sys, os, re, glob, math
import scipy
import GLOBALS
from helpers import prototype_distance
import statistics
# init variations
## BASELINE 1: Perfect initialization
def perfect_init(points, labs, nprototypes, nclasses, classdict):
# 2D points. A jumping window. Initializes the first prototypes of three clusters.
representative = None
prototypes = []
proto_idx = []
proto_dist = []
assigned_clusters = [None]*(len(points))
labs = [classdict[x] for x in labs]
print(labs)
for c in range(0,nclasses):
idxs = [idx for idx,cl in enumerate(labs) if cl == c][0:nprototypes]
pts = [points[x] for x in idxs]
prototypes.append(pts)
print(idxs)
proto_dist.append(prototype_distance(pts))
proto_idx.append(idxs)
for idx in idxs:
assigned_clusters[idx] = c # assign a cluster to idx wala index
pvotes = [[0]*nprototypes for x in range(nclasses)]
GLOBALS.NEWEST = {key: -1 for key in range(nclasses)}
return (prototypes, proto_idx, assigned_clusters, proto_dist, pvotes, representative)
## BASELINE 2: Random initialization
def random_init(batch, _, nprototypes, nclasses, classdict):
if GLOBALS.DEBUG:
print('received sequences', len(batch))
prototypes = []
proto_idx = []
proto_dist = []
representative = None
assigned_clusters = [None]*(len(batch))
batch_idx = [i for i in range(len(batch))]
GLOBALS.NEWEST = {key: -1 for key in range(nclasses)}
# pick a random point as first proto
r = random.choice(batch_idx)
prototypes.append(r)
#print('init proto', r)
#iterate over batch and pick required protos randomly
while len(prototypes) < (nclasses*nprototypes):
r = random.choice(batch_idx)
if r not in prototypes:
prototypes.append(r)
# if all protos are selected
proto_idx = []
for x in range(nclasses):
start = x*(nprototypes)
end = ((x+1)*(nprototypes))
proto_idx.append(prototypes[start:end])
for p in prototypes[start:end]:
assigned_clusters[p] = x
prototypes = []
for c in proto_idx:
pts = [batch[p] for p in c]
prototypes.append(pts)
proto_dist.append([0]*nprototypes)
pvotes = [[0]*nprototypes for x in range(nclasses)]
return (prototypes, proto_idx, assigned_clusters, proto_dist, pvotes, representative)
## IMPL 4: K-med++ initialization
def nonuniform_init(batch, __, nprototypes, nclasses, classdict):
proto_dist = []
(prototypes, proto_idx, representative, assigned_clusters) = Kplusplus(batch, nprototypes, nclasses)
pvotes = [[0]*nprototypes for x in range(nclasses)]
GLOBALS.NEWEST = {key: -1 for key in range(nclasses)}
for c in range(nclasses):
proto_dist.append([0]*nprototypes)#prototype_distance(prototypes[c]))
return (prototypes, proto_idx, assigned_clusters, proto_dist, pvotes, representative)
def Kplusplus(batch, nprototypes, nclasses):
init_prototypes = []
idx = []
sec_protos = []
selected = set()
representative = None
assigned_clusters = [None]*(len(batch))
batch_idx = [i for i in range(len(batch))]
# pick a random point as first proto
r = random.choice(batch_idx)
init_prototypes.append(r)
selected.add(r)
#iterate over batch.
distance = []
# K-med++ : Choose the next medoid with a certain probability
for k in range(nclasses): # k = number of classes/clusters O(k)
dists = []
# O(b)
for candidate in batch_idx:
# compute min distance from one of the closest chosen protos
latest_proto = init_prototypes[-1]
d = round((dtw(batch[latest_proto], batch[candidate], dist_method="euclidean").distance)**2, 2)
GLOBALS.count_dist += 1
dists.append(d)
distance.append(dists)
#print('len of distance ', len(distance))
# Pick other protos of this cluster
dists = distance[-1]
di = {k: v for k,v in enumerate(dists)}
di = sorted(di.items(), key=lambda item: item[1])
others = []
i = 0
while len(others) < (nprototypes-1) and i < len(di):
nextt = di[i][0]
if nextt not in selected:
others.append(nextt)
selected.add(nextt)
i+=1
sec_protos.append(others)
assert len(others) == (nprototypes-1)
if k == nclasses-1:
break
# Pick a proto of other cluster
closest = [[p[candidate] for p in distance] for candidate in batch_idx]
#print(closest)
mindists = [min(x) for x in closest]
#print('closest', mindists)
mindists = np.array(mindists)
probs = mindists/mindists.sum()
#print('probs', probs)
cumprobs = probs.cumsum()
#print('cumprobs', cumprobs)
r = random.uniform(0,1)
#print('r', r)
for j,p in enumerate(cumprobs):
if r < p:
i = j
break
init_prototypes.append(i)
selected.add(i)
# if all protos are selected
idx = []
for i in range(nclasses):
l = [init_prototypes[i]]
l.extend(sec_protos[i])
idx.append(l)
for p in l:
assigned_clusters[p] = i
init_prototypes = []
for c in idx:
init_prototypes.append([batch[p] for p in c])
return (init_prototypes, idx, representative, assigned_clusters)
##### Stat implementations
## IMPL 5: K-med++ stat initialization
def st_nonuniform_init(batch, __, nprototypes, nclasses, classdict):
proto_dist = []
(prototypes, proto_idx, representative, assigned_clusters) = Kplusplus_stat(batch, nprototypes, nclasses)
pvotes = [[0]*nprototypes for x in range(nclasses)]
GLOBALS.NEWEST = {key: -1 for key in range(nclasses)}
for c in range(nclasses):
proto_dist.append(prototype_distance(prototypes[c]))
return (prototypes, proto_idx, assigned_clusters, proto_dist, pvotes, representative)
def Kplusplus_stat(batch, nprototypes, nclasses):
init_prototypes = []
idx = []
sec_protos = []
selected = set()
representative = None
assigned_clusters = [None]*(len(batch))
batch_idx = [i for i in range(len(batch))]
# pick a random point as first proto
r = random.choice(batch_idx)
init_prototypes.append(r)
selected.add(r)
#iterate over batch.
distance = []
# K-med++ : Choose the next medoid with a certain probability
for k in range(nclasses): # k = number of classes/clusters O(k)
dists = []
# O(b)
for candidate in batch_idx:
# compute min distance from one of the closest chosen protos
latest_proto = init_prototypes[-1]
d = round((euclidean_distances(np.array(batch[latest_proto]).reshape(1, -1),np.array(batch[candidate]).reshape(1, -1))[0][0])**2, 2)
GLOBALS.count_dist += 1
dists.append(d)
distance.append(dists)
#print('len of distance ', len(distance))
# Pick other protos of this cluster
dists = distance[-1]
di = {k: v for k,v in enumerate(dists)}
di = sorted(di.items(), key=lambda item: item[1])
others = []
i = 0
while len(others) < (nprototypes-1) and i < len(di):
nextt = di[i][0]
if nextt not in selected:
others.append(nextt)
selected.add(nextt)
i+=1
sec_protos.append(others)
assert len(others) == (nprototypes-1)
if k == nclasses-1:
break
# Pick a proto of other cluster
closest = [[p[candidate] for p in distance] for candidate in batch_idx]
#print(closest)
mindists = [min(x) for x in closest]
#print('closest', mindists)
mindists = np.array(mindists)
probs = mindists/mindists.sum()
#print('probs', probs)
cumprobs = probs.cumsum()
#print('cumprobs', cumprobs)
r = random.uniform(0,1)
#print('r', r)
for j,p in enumerate(cumprobs):
if r < p:
i = j
break
init_prototypes.append(i)
selected.add(i)
# if all protos are selected
idx = []
for i in range(nclasses):
l = [init_prototypes[i]]
l.extend(sec_protos[i])
idx.append(l)
for p in l:
assigned_clusters[p] = i
init_prototypes = []
for c in idx:
init_prototypes.append([batch[p] for p in c])
return (init_prototypes, idx, representative, assigned_clusters)
##### Stat implementations
## IMPL 6: K-med++ aggregated initialization
def aggr_nonuniform_init(batch, __, nprototypes, nclasses, classdict):
proto_dist = []
(prototypes, proto_idx, representative, assigned_clusters) = Kplusplus_aggr(batch, nprototypes, nclasses)
pvotes = [[0]*nprototypes for x in range(nclasses)]
GLOBALS.NEWEST = {key: -1 for key in range(nclasses)}
for c in range(nclasses):
proto_dist.append([0]*nprototypes)#prototype_distance(prototypes[c]))
return (prototypes, proto_idx, assigned_clusters, proto_dist, pvotes, representative)
def Kplusplus_aggr(batch, nprototypes, nclasses):
init_prototypes = []
idx = []
sec_protos = []
selected = set()
representative = None
assigned_clusters = [None]*(len(batch))
is_tuple = False
is_seq = False
batch_idx = [i for i in range(len(batch))]
# check if the data type is tuple or others
if isinstance(batch[0][0], tuple):
is_tuple = True
if len(batch[0]) > 2:
is_seq = True
# pick a random point as first proto
r = random.choice(batch_idx)
init_prototypes.append(r)
selected.add(r)
#iterate over batch.
distance = []
# K-med++ : Choose the next medoid with a certain probability
for k in range(nclasses): # k = number of classes/clusters O(k)
dists = []
batch_latest_proto_mean, batch_candidate_mean = -1, -1
latest_proto = init_prototypes[-1]
if is_tuple:
tupp = []
# compute mean of each dimension
grouped_features = zip(*batch[latest_proto])
for xa in grouped_features:
tupp.append(statistics.mean(xa))
batch_latest_proto_mean = [tuple(tupp)]
elif is_seq:
batch_latest_proto_mean = [statistics.mean(batch[latest_proto])]
else:
batch_latest_proto_mean = batch[latest_proto]
# O(b)
for candidate in batch_idx:
# compute min distance from one of the closest chosen protos
if is_tuple:
tupp = []
# compute mean of each dimension
grouped_features = zip(*batch[candidate])
for xa in grouped_features:
tupp.append(statistics.mean(xa))
batch_candidate_mean = [tuple(tupp)]
elif is_seq:
batch_candidate_mean = [statistics.mean(batch[candidate])]
else:
batch_candidate_mean = batch[candidate]
d = round((cdist(np.array(batch_latest_proto_mean).reshape(1, -1),np.array(batch_candidate_mean).reshape(1, -1), 'euclidean')[0][0])**2, 2)
GLOBALS.count_dist += 1
dists.append(d)
distance.append(dists)
#print('len of distance ', len(distance))
# Pick other protos of this cluster
dists = distance[-1]
di = {k: v for k,v in enumerate(dists)}
di = sorted(di.items(), key=lambda item: item[1])
others = []
i = 0
while len(others) < (nprototypes-1) and i < len(di):
nextt = di[i][0]
if nextt not in selected:
others.append(nextt)
selected.add(nextt)
i+=1
sec_protos.append(others)
assert len(others) == (nprototypes-1)
if k == nclasses-1:
break
# Pick a proto of other cluster
closest = [[p[candidate] for p in distance] for candidate in batch_idx]
#print(closest)
mindists = [min(x) for x in closest]
#print('closest', mindists)
mindists = np.array(mindists)
probs = mindists/mindists.sum()
#print('probs', probs)
cumprobs = probs.cumsum()
#print('cumprobs', cumprobs)
r = random.uniform(0,1)
#print('r', r)
for j,p in enumerate(cumprobs):
if r < p:
i = j
break
init_prototypes.append(i)
selected.add(i)
# if all protos are selected
idx = []
for i in range(nclasses):
l = [init_prototypes[i]]
l.extend(sec_protos[i])
idx.append(l)
for p in l:
assigned_clusters[p] = i
init_prototypes = []
for c in idx:
init_prototypes.append([batch[p] for p in c])
return (init_prototypes, idx, representative, assigned_clusters)