-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautonto.py
1265 lines (1028 loc) · 38 KB
/
autonto.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#General Imports
import os
import math
import operator
import argparse
from collections import defaultdict
import argparse
import subprocess
from math import log
#Imports for clustering phase
from scipy.spatial.distance import cosine
import warnings
import numpy as np
import scipy.sparse as sp
from joblib import Parallel, delayed
from sklearn.cluster import KMeans
from sklearn.cluster import _k_means_fast as _k_means
from sklearn.cluster.k_means_ import (
_check_sample_weight,
_init_centroids,
_labels_inertia,
_tolerance,
_validate_center_shape,
)
from sklearn.preprocessing import normalize
from sklearn.utils import check_array, check_random_state
from sklearn.utils.extmath import row_norms, squared_norm
from sklearn.utils.validation import _num_samples
# Imports for local embedding phase
from gensim.models import Word2Vec
from gensim.models.word2vec import LineSentence
# Helper Functions
def load_embeddings(embedding_file):
if embedding_file is None:
return {}
word_to_vec = {}
with open(embedding_file, 'r') as fin:
header = fin.readline()
for line in fin:
items = line.strip().split()
word = items[0]
vec = [float(v) for v in items[1:]]
word_to_vec[word] = vec
return word_to_vec
def kl_divergence(p, q):
if len(p) != len(q):
print('KL divergence error: p, q have different length')
c_entropy = 0
for i in range(len(p)):
if p[i] > 0:
c_entropy += p[i] * math.log(float(p[i]) / q[i])
return c_entropy
def avg_weighted_colors(color_list, c_size):
# print color_list
# given a weighted color list, return the result
result_color = [0] * c_size
for (color, weight) in color_list:
w_color = [x * weight for x in color]
# print w_color
result_color = map(operator.add, result_color, w_color)
# print result_color
return l1_normalize(result_color)
def l1_normalize(p):
sum_p = sum(p)
if sum_p <= 0:
print('Normalizing invalid distribution')
return [x/sum_p for x in p]
def cossim(p, q):
if len(p) != len(q):
print('KL divergence error: p, q have different length')
p_len = q_len = mix_len = 0
for i in range(len(p)):
mix_len += p[i] * q[i]
p_len += p[i] * p[i]
q_len += q[i] * q[i]
return mix_len / (math.sqrt(p_len) * math.sqrt(q_len))
def euclidean_distance(p, q):
if len(p) != len(q):
print ('Euclidean distance error: p, q have different length')
distance = 0
for i in range(len(p)):
distance += math.pow(p[i] - q[i], 2)
return math.sqrt(distance)
def euclidean_cluster(ps, c):
if len(ps) == 0 or c == None:
print ('Cluster is empty')
distance = 0
for p in ps:
for i in range(len(p)):
distance += math.pow(p[i] - c[i], 2)
distance /= len(ps)
return math.sqrt(distance)
def dot_product(p, q):
if len(p) != len(q):
print ('KL divergence error: p, q have different length')
p_len = q_len = mix_len = 0
for i in range(len(p)):
mix_len += p[i] * q[i]
return mix_len
def softmax(score_list):
# normalization of exp
exp_sum = 0
for score in score_list:
exp_sum += math.exp(score)
exp_list = []
for score in score_list:
normal_value = math.exp(score) / exp_sum
exp_list.append(normal_value)
return exp_list
def softmax_for_map(t_map):
exp_sum = 0
for key in t_map:
score = t_map[key]
exp_sum += math.exp(score)
for key in t_map:
score = t_map[key]
normal_value = math.exp(score) / exp_sum
t_map[key] = normal_value
def avg_emb_with_distinct(ele_map, embs_from, dist_map, vec_size):
avg_emb = [0] * vec_size
t_weight = 0
for key, value in ele_map.iteritems():
t_emb = embs_from[key]
w = value * dist_map[key]
for i in range(vec_size):
avg_emb[i] += w * t_emb[i]
t_weight += w
for i in range(vec_size):
avg_emb[i] /= t_weight
return avg_emb
def avg_emb(ele_map, embs_from, vec_size):
avg_emb = [0] * vec_size
t_weight = 0
for key, value in ele_map.iteritems():
t_emb = embs_from[key]
w = value
for i in range(vec_size):
avg_emb[i] += w * t_emb[i]
t_weight += w
for i in range(vec_size):
avg_emb[i] /= t_weight
return avg_emb
def load_hier_f(hier_f):
hier_map = {}
with open(hier_f) as f:
idx = 0
for line in f:
topic = line.split()[0]
hier_map[topic] = idx
idx += 1
return hier_map
# ensure the path for the output file exist
def ensure_directory_exist(file_name):
directory = os.path.dirname(file_name)
if not os.path.exists(directory):
os.makedirs(directory)
# IO methods
# the complete data set
class DataSet:
def __init__(self, embedding_file, document_file):
self.documents = self.load_documents(document_file)
self.embeddings = self.load_embeddings(embedding_file)
# the initial complete set of keywords
# self.keywords = self.load_keywords(candidate_file)
# self.keyword_set = set(self.keywords)
# self.documents_trimmed = self.get_trimmed_documents(self.documents, self.keyword_set)
# assert len(self.documents) == len(self.documents_trimmed)
def load_embeddings(self, embedding_file):
if embedding_file is None:
return {}
word_to_vec = {}
with open(embedding_file, 'r') as fin:
header = fin.readline()
for line in fin:
items = line.strip().split()
word = items[0]
vec = [float(v) for v in items[1:]]
word_to_vec[word] = vec
return word_to_vec
def load_documents(self, document_file):
documents = []
with open(document_file, 'r') as fin:
for line in fin:
keywords = line.strip().split()
documents.append(keywords)
print('Length in Dataset.load_documents - ',len(documents))
return documents
# sub data set for each cluster
class SubDataSet:
def __init__(self, full_data, doc_id_file, keyword_file):
self.keywords = self.load_keywords(keyword_file, full_data)
self.keyword_to_id = self.gen_keyword_id()
self.keyword_set = set(self.keywords)
self.embeddings = self.load_embeddings(full_data)
self.documents, self.original_doc_ids = self.load_documents(full_data, doc_id_file)
self.keyword_idf = self.build_keyword_idf()
def load_keywords(self, keyword_file, full_data):
keywords = []
with open(keyword_file, 'r') as fin:
for line in fin:
keyword = line.strip()
if keyword in full_data.embeddings:
keywords.append(keyword)
else:
print(keyword, ' not in the embedding file')
return keywords
def gen_keyword_id(self):
keyword_to_id = {}
for idx, keyword in enumerate(self.keywords):
keyword_to_id[keyword] = idx
return keyword_to_id
def load_embeddings(self, full_data):
embeddings = full_data.embeddings
ret = []
for word in self.keywords:
vec = embeddings[word]
ret.append(vec)
return np.array(ret)
def load_documents(self, full_data, doc_id_file):
'''
:param full_data:
:param doc_id_file:
:return: trimmed documents along with its corresponding ids
'''
doc_ids = self.load_doc_ids(doc_id_file)
trimmed_doc_ids, trimmed_docs = [], []
#print(len(full_data.documents))
for doc_id in doc_ids:
#print(doc_id)
doc = full_data.documents[doc_id]
trimmed_doc = [e for e in doc if e in self.keyword_set]
if len(trimmed_doc) > 0:
trimmed_doc_ids.append(doc_id)
trimmed_docs.append(trimmed_doc)
return trimmed_docs, trimmed_doc_ids
def load_doc_ids(self, doc_id_file):
doc_ids = []
with open(doc_id_file, 'r') as fin:
for line in fin:
doc_id = int(line.strip())
doc_ids.append(doc_id)
return doc_ids
def build_keyword_idf(self):
keyword_idf = defaultdict(float)
for doc in self.documents:
word_set = set(doc)
for word in word_set:
if word in self.keyword_set:
keyword_idf[word] += 1.0
N = len(self.documents)
for w in keyword_idf:
keyword_idf[w] = log(1.0 + N / keyword_idf[w])
return keyword_idf
# output_file: one integrated file;
def write_cluster_members(self, clus, cluster_file, parent_dir):
n_cluster = clus.n_cluster
clusters = clus.clusters # a dict: cluster id -> keywords
with open(cluster_file, 'w') as fout:
for clus_id in range(n_cluster):
members = clusters[clus_id]
for keyword_id in members:
keyword = self.keywords[keyword_id]
fout.write(str(clus_id) + '\t' + keyword + '\n')
# write the cluster for each sub-folder
clus_centers = clus.center_ids
for clus_id, center_keyword_id in clus_centers:
center_keyword = self.keywords[center_keyword_id]
output_file = parent_dir + center_keyword + '/seed_keywords.txt'
ensure_directory_exist(output_file)
members = clusters[clus_id]
with open(output_file, 'w') as fout:
for keyword_id in members:
keyword = self.keywords[keyword_id]
fout.write(keyword + '\n')
def write_cluster_centers(self, clus, parent_description, output_file):
clus_centers = clus.center_ids
center_names = []
with open(output_file, 'w') as fout:
for cluster_id, keyword_idx in clus_centers:
keyword = self.keywords[keyword_idx]
center_names.append(keyword)
fout.write(keyword + ' ' + parent_description + '\n')
return center_names
def write_document_membership(self, clus, output_file, parent_dir):
n_cluster = clus.n_cluster
keyword_membership = clus.membership # an array containing the membership of the keywords
cluster_document_map = defaultdict(list) # key: cluster id, value: document list
with open(output_file, 'w') as fout:
for idx, doc in zip(self.original_doc_ids, self.documents):
doc_membership = self.get_doc_membership(n_cluster, doc, keyword_membership)
cluster_id = self.assign_document(doc_membership)
cluster_document_map[cluster_id].append(idx)
fout.write(str(idx) + '\t' + str(cluster_id) + '\n')
# write the document ids for each sub-folder
clus_centers = clus.center_ids
for clus_id, center_keyword_id in clus_centers:
center_keyword = self.keywords[center_keyword_id]
output_file = parent_dir + center_keyword + '/doc_ids.txt'
ensure_directory_exist(output_file)
doc_ids = cluster_document_map[clus_id]
with open(output_file, 'w') as fout:
for doc_id in doc_ids:
fout.write(str(doc_id) + '\n')
def get_doc_membership(self, n_cluster, document, keyword_membership):
ret = [0.0] * n_cluster
## Strength of each document on each cluster is the tf-idf score. The tf part is considered during the
## enumeration of document tokens.
for keyword in document:
keyword_id = self.keyword_to_id[keyword]
cluster_id = keyword_membership[keyword_id]
idf = self.keyword_idf[keyword]
ret[cluster_id] += idf
return ret
def assign_document(self, doc_membership):
## Currently document cluster is a hard partition.
best_idx, max_score = -1, 0
for idx, score in enumerate(doc_membership):
if score > max_score:
best_idx, max_score = idx, score
return best_idx
# Keyphrase ranking functions
def read_caseolap_result(case_file):
phrase_map = {}
cell_map = {}
cell_cnt = 0
with open(case_file) as f:
for line in f:
cell_cnt += 1
segments = line.strip('\r\n ').split('\t')
cell_id, phs_str = segments[0], segments[1][1:-1]
cell_map[cell_id] = []
segments = phs_str.split(', ')
for ph_score in segments:
parts = ph_score.split('|')
ph, score = parts[0], float(parts[1])
if ph not in phrase_map:
phrase_map[ph] = {}
phrase_map[ph][cell_id] = score
cell_map[cell_id].append((ph, score))
return phrase_map, cell_map, cell_cnt
def rank_phrase(case_file):
ph_dist_map = {}
smoothing_factor = 0.0
phrase_map, cell_map, cell_cnt = read_caseolap_result(case_file)
unif = [1.0 / cell_cnt] * cell_cnt
for ph in phrase_map:
ph_vec = [x[1] for x in phrase_map[ph].items()]
if len(ph_vec) < cell_cnt:
ph_vec += [0] * (cell_cnt - len(ph_vec))
# smoothing
ph_vec = [x + smoothing_factor for x in ph_vec]
ph_vec = l1_normalize(ph_vec)
ph_dist_map[ph] = kl_divergence(ph_vec, unif)
ranked_list = sorted(ph_dist_map.items(), key=operator.itemgetter(1), reverse=True)
return ranked_list
def write_keywords(o_file, ranked_list, thres):
with open(o_file, 'w+') as g:
for ph in ranked_list:
if ph[1] > thres:
g.write('%s\n' % (ph[0]))
tmp_file = o_file + '-score.txt'
with open(tmp_file, 'w+') as g:
for ph in ranked_list:
g.write('%s\t%f\n' % (ph[0], ph[1]))
def main_rank_phrase(input_f, output_f, thres):
ranked_list = rank_phrase(input_f)
write_keywords(output_f, ranked_list, thres)
print("[CaseOLAP] Finish pushing general terms up")
from heapq import heappush, heappop, heappushpop, nsmallest, nlargest
import codecs
import math
import ast
import argparse
import copy
class CaseSlim:
def bm25_df_paper(self, df, max_df, tf, dl, avgdl, k=1.2, b=0.5, multiplier=3):
score = tf * (k + 1) / (tf + k * (1 - b + b * (dl / avgdl)))
df_factor = math.log(1 + df, 2) / math.log(1 + max_df, 2)
score *= df_factor
score *= multiplier
return score
def softmax_paper(self, score_list):
# normalization of exp
exp_sum = 1
for score in score_list:
exp_sum += math.exp(score)
exp_list = []
for score in score_list:
normal_value = math.exp(score) / exp_sum
exp_list.append(normal_value)
return exp_list
def compute(self, score_type='ALL'):
'''
-- score_type --
ALL: all three factors
POP: only popularity
DIS: only distinctive
INT: only integrity
NOPOP: no populairty
NODIS: no distinctive
NOINT: no integrity
'''
scores = {}
multiplier = 1
sum_self = self.sum_cnt
num_context_cells = len(self.sum_cnt_context) + 1
total_sum = sum(self.sum_cnt_context.values()) + sum_self
avgdl = total_sum / float(num_context_cells)
# method 1
for phrase in self.phrase_cnt:
lower_phrase = phrase.lower()
score = 1
nor_phrase = self.normalize(lower_phrase)
self_cnt = self.phrase_cnt[phrase]
self_df = self.phrase_df[phrase]
group = [(self_df, self.max_df, self_cnt, sum_self)]
self.context_groups[phrase] = []
for phrase_group, phrase_values in self.phrase_cnt_context.items():
context_df = self.phrase_df_context[phrase_group].get(phrase, 0)
sum_context = self.sum_cnt_context[phrase_group]
context_cnt = phrase_values.get(phrase, 0)
maxdf_context = self.max_df_context[phrase_group]
if (context_cnt > 0):
group.append((context_df, maxdf_context, context_cnt, sum_context))
self.context_groups[phrase].append((context_df, maxdf_context, context_cnt, sum_context))
score_list = []
for record in group:
score_list.append(self.bm25_df_paper(record[0], record[1], record[2], record[3], avgdl))
distinct = self.softmax_paper(score_list)[0]
popularity = math.log(1 + self_df, 2)
try:
integrity = float(self.global_scores[nor_phrase])
except:
integrity = 0.8
if score_type == 'ALL':
score = distinct * popularity * integrity
elif score_type == 'POP':
score = popularity
elif score_type == 'DIS':
score = distinct
elif score_type == 'INT':
score = integrity
elif score_type == 'NOPOP':
score = distinct * integrity
elif score_type == 'NODIS':
score = popularity * integrity
elif score_type == 'NOINT':
score = popularity * distinct
else:
score = 0
scores[phrase] = score
ranked_list = [(phrase, scores[phrase]) for phrase in sorted(scores, key=scores.get, reverse=True)]
return ranked_list
def agg_phrase_cnt_df(self, freq_data, selected_docs = None):
phrase_cnt = {}
phrase_df = {}
if selected_docs == None:
for doc_index in freq_data:
for phrase in freq_data[doc_index]:
if phrase not in phrase_cnt:
phrase_cnt[phrase] = 0
phrase_cnt[phrase] += freq_data[doc_index][phrase]
else:
for doc_index in selected_docs:
for phrase in freq_data.get(doc_index, {}):
if phrase not in phrase_cnt:
phrase_cnt[phrase] = 0
if phrase not in phrase_df:
phrase_df[phrase] = 0
phrase_cnt[phrase] += freq_data[doc_index][phrase]
phrase_df[phrase] += 1
return phrase_cnt, phrase_df
def normalize(self, word):
word = word.lower()
result = []
for i in range(len(word)):
if word[i].isalpha() or word[i] == '\'':
result.append(word[i])
else:
result.append(' ')
word = ''.join(result)
return ' '.join(word.split())
def __init__(self, freq_data, selected_docs, context_doc_groups, global_scores=None):
# print 'handle slim version'
self.phrase_cnt, self.phrase_df = self.agg_phrase_cnt_df(freq_data, selected_docs)
self.phrase_cnt_context = {}
self.phrase_df_context = {}
if len(self.phrase_df) > 0:
self.max_df = max(self.phrase_df.values())
else:
self.max_df = 0
self.max_df_context = {}
self.dc_context = {}
self.self_dc = len(selected_docs)
self.sum_cnt = sum(self.phrase_cnt.values())
self.sum_cnt_context = {}
self.global_scores = global_scores
for group, docs in context_doc_groups.items():
self.phrase_cnt_context[group], self.phrase_df_context[group] = self.agg_phrase_cnt_df(freq_data, docs)
if len(self.phrase_df_context[group]) > 0:
self.max_df_context[group] = max(self.phrase_df_context[group].values())
else:
self.max_df_context[group] = 0
self.dc_context[group] = len(docs)
self.sum_cnt_context[group] = sum(self.phrase_cnt_context[group].values())
# added for exploration
self.context_groups = {}
self.ranked_list = []
def read_data(label_f, link_f):
'''
:param label_f: doc_membership_file
:param link_f: keyword_cnt, <doc_id>\t<word1>\t<count1>\t<word2>\t<count2>
:return:
cells: key: cell_id (int), value: doc_id_list
freq_data: key: doc_id, value: a dict (key: phrase, value: phrase count)
phrases: a set of phrases
'''
cells = {}
freq_data = {}
docs = set()
phrases = set()
with open(label_f, 'r+') as f:
for line in f:
segments = line.strip('\n\r').split('\t')
cell = segments[1]
doc_id = segments[0]
if cell not in cells:
cells[cell] = []
cells[cell].append(doc_id)
docs.add(doc_id)
print('[CaseOLAP] Read document cluster membership file done.')
with open(link_f, 'r+') as f:
for line in f:
segments = line.strip('\n\r ').split('\t')
doc_id = segments[0]
if doc_id not in docs:
continue
if doc_id not in freq_data:
freq_data[doc_id] = {}
for i in range(1, len(segments), 2):
phrase, w = segments[i], int(segments[i+1])
phrases.add(phrase)
freq_data[doc_id][phrase] = w
print('[CaseOLAP] Read keyword_cnt file done.')
return cells, freq_data, phrases
def read_target_tokens(token_f):
'''
:param token_f: cluster_keyword_file
:return:
'''
tokens = set()
with open(token_f, 'r+') as f:
for line in f:
segments = line.strip('\r\n ').split('\t')
tokens.add(segments[1])
print('[CaseOLAP] Read keyword cluster membership file done.')
return tokens
def run_caseolap(cells, freq_data, target_phs, o_file, verbose=3, top_k=200):
of = open(o_file, 'w+')
for cell in cells:
print('[CaseOLAP] Running CaseOLAP for cell: %s' % cell)
selected_docs = cells[cell]
context_doc_groups = copy.copy(cells)
context_doc_groups.pop(cell, None)
caseslim = CaseSlim(freq_data, selected_docs, context_doc_groups)
top_phrases = caseslim.compute(score_type="NOINT")
of.write('%s\t' % cell)
phr_str = ', '.join([ph[0] + '|' + str(ph[1]) for ph in top_phrases if ph[0] in target_phs])
of.write('[%s]\n' % phr_str)
print('[CaseOLAP] Finished CaseOLAP for cell: %s' % cell)
def main_caseolap(link_f, cell_f, token_f, output_f):
cells, freq_data, phrases = read_data(cell_f, link_f)
target_phs = read_target_tokens(token_f)
run_caseolap(cells, freq_data, target_phs, output_f)
### Spherical Clustering
def _spherical_kmeans_single_lloyd(
X,
n_clusters,
sample_weight=None,
max_iter=300,
init="k-means++",
verbose=False,
x_squared_norms=None,
random_state=None,
tol=1e-4,
precompute_distances=True,
):
"""
Modified from sklearn.cluster.k_means_.k_means_single_lloyd.
"""
random_state = check_random_state(random_state)
sample_weight = _check_sample_weight(sample_weight, X)
best_labels, best_inertia, best_centers = None, None, None
# init
centers = _init_centroids(
X, n_clusters, init, random_state=random_state, x_squared_norms=x_squared_norms
)
if verbose:
print("Initialization complete")
# Allocate memory to store the distances for each sample to its
# closer center for reallocation in case of ties
distances = np.zeros(shape=(X.shape[0],), dtype=X.dtype)
# iterations
for i in range(max_iter):
centers_old = centers.copy()
# labels assignment
# TODO: _labels_inertia should be done with cosine distance
# since ||a - b|| = 2(1 - cos(a,b)) when a,b are unit normalized
# this doesn't really matter.
labels, inertia = _labels_inertia(
X,
sample_weight,
x_squared_norms,
centers,
precompute_distances=precompute_distances,
distances=distances,
)
# computation of the means
if sp.issparse(X):
centers = _k_means._centers_sparse(
X, sample_weight, labels, n_clusters, distances
)
else:
centers = _k_means._centers_dense(
X.astype(float),
sample_weight.astype(float),
labels,
n_clusters,
distances.astype(float),
)
# l2-normalize centers (this is the main contibution here)
centers = normalize(centers)
if verbose:
print("Iteration %2d, inertia %.3f" % (i, inertia))
if best_inertia is None or inertia < best_inertia:
best_labels = labels.copy()
best_centers = centers.copy()
best_inertia = inertia
center_shift_total = squared_norm(centers_old - centers)
if center_shift_total <= tol:
if verbose:
print(
"Converged at iteration %d: "
"center shift %e within tolerance %e" % (i, center_shift_total, tol)
)
break
if center_shift_total > 0:
# rerun E-step in case of non-convergence so that predicted labels
# match cluster centers
best_labels, best_inertia = _labels_inertia(
X,
sample_weight,
x_squared_norms,
best_centers,
precompute_distances=precompute_distances,
distances=distances,
)
return best_labels, best_inertia, best_centers, i + 1
def spherical_k_means(
X,
n_clusters,
sample_weight=None,
init="k-means++",
n_init=10,
max_iter=300,
verbose=False,
tol=1e-4,
random_state=None,
copy_x=True,
n_jobs=1,
algorithm="auto",
return_n_iter=False,
):
"""Modified from sklearn.cluster.k_means_.k_means.
"""
if n_init <= 0:
raise ValueError(
"Invalid number of initializations."
" n_init=%d must be bigger than zero." % n_init
)
random_state = check_random_state(random_state)
if max_iter <= 0:
raise ValueError(
"Number of iterations should be a positive number,"
" got %d instead" % max_iter
)
best_inertia = np.infty
# avoid forcing order when copy_x=False
order = "C" if copy_x else None
X = check_array(
X, accept_sparse="csr", dtype=[np.float64, np.float32], order=order, copy=copy_x
)
# verify that the number of samples given is larger than k
if _num_samples(X) < n_clusters:
raise ValueError(
"n_samples=%d should be >= n_clusters=%d" % (_num_samples(X), n_clusters)
)
tol = _tolerance(X, tol)
if hasattr(init, "__array__"):
init = check_array(init, dtype=X.dtype.type, order="C", copy=True)
_validate_center_shape(X, n_clusters, init)
if n_init != 1:
warnings.warn(
"Explicit initial center position passed: "
"performing only one init in k-means instead of n_init=%d" % n_init,
RuntimeWarning,
stacklevel=2,
)
n_init = 1
# precompute squared norms of data points
x_squared_norms = row_norms(X, squared=True)
if n_jobs == 1:
# For a single thread, less memory is needed if we just store one set
# of the best results (as opposed to one set per run per thread).
for it in range(n_init):
# run a k-means once
labels, inertia, centers, n_iter_ = _spherical_kmeans_single_lloyd(
X,
n_clusters,
sample_weight,
max_iter=max_iter,
init=init,
verbose=verbose,
tol=tol,
x_squared_norms=x_squared_norms,
random_state=random_state,
)
# determine if these results are the best so far
if best_inertia is None or inertia < best_inertia:
best_labels = labels.copy()
best_centers = centers.copy()
best_inertia = inertia
best_n_iter = n_iter_
else:
# parallelisation of k-means runs
seeds = random_state.randint(np.iinfo(np.int32).max, size=n_init)
results = Parallel(n_jobs=n_jobs, verbose=0)(
delayed(_spherical_kmeans_single_lloyd)(
X,
n_clusters,
sample_weight,
max_iter=max_iter,
init=init,
verbose=verbose,
tol=tol,
x_squared_norms=x_squared_norms,
# Change seed to ensure variety
random_state=seed,
)
for seed in seeds
)
# Get results with the lowest inertia
labels, inertia, centers, n_iters = zip(*results)
best = np.argmin(inertia)
best_labels = labels[best]
best_inertia = inertia[best]
best_centers = centers[best]
best_n_iter = n_iters[best]
if return_n_iter:
return best_centers, best_labels, best_inertia, best_n_iter
else:
return best_centers, best_labels, best_inertia
class SphericalKMeans(KMeans):
"""Spherical K-Means clustering
Modfication of sklearn.cluster.KMeans where cluster centers are normalized
(projected onto the sphere) in each iteration.
Parameters
----------
n_clusters : int, optional, default: 8
The number of clusters to form as well as the number of
centroids to generate.
max_iter : int, default: 300
Maximum number of iterations of the k-means algorithm for a
single run.
n_init : int, default: 10
Number of time the k-means algorithm will be run with different
centroid seeds. The final results will be the best output of
n_init consecutive runs in terms of inertia.
init : {'k-means++', 'random' or an ndarray}
Method for initialization, defaults to 'k-means++':
'k-means++' : selects initial cluster centers for k-mean
clustering in a smart way to speed up convergence. See section
Notes in k_init for more details.
'random': choose k observations (rows) at random from data for
the initial centroids.
If an ndarray is passed, it should be of shape (n_clusters, n_features)
and gives the initial centers.
tol : float, default: 1e-4
Relative tolerance with regards to inertia to declare convergence
n_jobs : int
The number of jobs to use for the computation. This works by computing
each of the n_init runs in parallel.
If -1 all CPUs are used. If 1 is given, no parallel computing code is
used at all, which is useful for debugging. For n_jobs below -1,
(n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one
are used.
random_state : integer or numpy.RandomState, optional
The generator used to initialize the centers. If an integer is
given, it fixes the seed. Defaults to the global numpy random
number generator.
verbose : int, default 0
Verbosity mode.
copy_x : boolean, default True
When pre-computing distances it is more numerically accurate to center
the data first. If copy_x is True, then the original data is not
modified. If False, the original data is modified, and put back before
the function returns, but small numerical differences may be introduced
by subtracting and then adding the data mean.
normalize : boolean, default True
Normalize the input to have unnit norm.
Attributes
----------
cluster_centers_ : array, [n_clusters, n_features]
Coordinates of cluster centers
labels_ :
Labels of each point
inertia_ : float
Sum of distances of samples to their closest cluster center.
"""