-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-1_clustering.py
209 lines (174 loc) · 7.78 KB
/
03-1_clustering.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
import pickle
from tqdm import tqdm
import pandas as pd
from collections import Counter, defaultdict
from itertools import chain, groupby, combinations
import os
import numpy as np
import gc
import sys
from sklearn import metrics
from sklearn.cluster import AffinityPropagation, KMeans
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from scipy.spatial.distance import cosine
from scipy import stats
from sklearn.metrics import silhouette_score
def create_slice_df(data_df, year, slice_len, journal):
if journal:
return data_df.loc[(data_df.year >= year) & (data_df.year < year+slice_len) & (data_df.journal == journal)]
else:
return data_df.loc[(data_df.year >= year) & (data_df.year < year+slice_len)]
### Von calable and Interpretable Semantic Change Detection, Montariol2021
# https://github.com/matejMartinc/scalable_semantic_shift/blob/master/measure_semantic_shift.py
def aff_prop(slice_df, column, preference):
embeddings = np.array(slice_df[column])
embeddings = [np.array(emb.tolist()) for emb in embeddings]
AP = AffinityPropagation(
preference=preference,
damping=0.9, #default 0.5, bei 0.75 gibt es keinen Convergence Error
max_iter = 400, #default 200
convergence_iter = 50, #default 15
affinity = "euclidean", # ‘euclidean’, ‘precomputed’ defaul ‘euclidean’
random_state=None)
AP.fit(embeddings)
#cluster_centers_indices = AP.cluster_centers_indices_
cluster_centers = AP.cluster_centers_
labels = AP.labels_
return labels, cluster_centers
### Von calable and Interpretable Semantic Change Detection, Montariol2021
# https://github.com/matejMartinc/scalable_semantic_shift/blob/master/measure_semantic_shift.py
def aff_prop(slice_df, column, preference):
embeddings = np.array(slice_df[column])
embeddings = [np.array(emb.tolist()) for emb in embeddings]
AP = AffinityPropagation(
preference=preference,
damping=0.9, #default 0.5, bei 0.75 gibt es keinen Convergence Error
max_iter = 400, #default 200
convergence_iter = 50, #default 15
affinity = "euclidean", # ‘euclidean’, ‘precomputed’ defaul ‘euclidean’
random_state=None)
AP.fit(embeddings)
#cluster_centers_indices = AP.cluster_centers_indices_
cluster_centers = AP.cluster_centers_
labels = AP.labels_
return labels, cluster_centers
# von montariol2021 (leicht verändert)
def combine_clusters(labels, embeddings, threshold, remove):
cluster_embeds = defaultdict(list)
for label, embed in zip(labels, embeddings):
cluster_embeds[label].append(embed)
min_num_examples = threshold
legit_clusters = []
for idd, num_examples in Counter(labels).items():
if num_examples >= threshold:
legit_clusters.append(idd)
if idd not in remove and num_examples < min_num_examples:
min_num_examples = num_examples
min_cluster_id = idd
if len(set(labels)) == 2:
return labels
min_dist = 1
all_dist = []
cluster_labels = ()
embed_list = list(cluster_embeds.items())
for i in range(len(embed_list)):
for j in range(i+1,len(embed_list)):
idd, embed = embed_list[i]
id2, embed2 = embed_list[j]
if idd in legit_clusters and id2 in legit_clusters:
dist = compute_averaged_embedding_dist(embed, embed2)
all_dist.append(dist)
if dist < min_dist:
min_dist = dist
cluster_labels = (idd, id2)
std = np.std(all_dist)
avg = np.mean(all_dist)
limit = avg - 4 * std
#limit = avg - 2 * std
#limit = 0.1
if min_dist < limit:
for n, i in enumerate(labels):
if i == cluster_labels[0]:
labels[n] = cluster_labels[1]
return combine_clusters(labels, embeddings, threshold, remove)
if min_num_examples >= threshold:
return labels
min_dist = 1
cluster_labels = ()
for idd, embed in cluster_embeds.items():
if idd != min_cluster_id:
dist = compute_averaged_embedding_dist(embed, cluster_embeds[min_cluster_id])
if dist < min_dist:
min_dist = dist
cluster_labels = (idd, min_cluster_id)
if cluster_labels[0] not in legit_clusters:
for n, i in enumerate(labels):
if i == cluster_labels[0]:
labels[n] = cluster_labels[1]
else:
if min_dist < limit:
for n, i in enumerate(labels):
if i == cluster_labels[0]:
labels[n] = cluster_labels[1]
else:
remove.append(min_cluster_id)
return combine_clusters(labels, embeddings, threshold, remove)
def compute_averaged_embedding_dist(t1_embeddings, t2_embeddings):
t1_mean = np.mean(t1_embeddings, axis=0)
t2_mean = np.mean(t2_embeddings, axis=0)
dist = cosine(t1_mean, t2_mean)
return dist
def k_means(slice_df, column, k, random_state):
embeddings = np.array(slice_df[column])
embeddings = [np.array(emb.tolist()) for emb in embeddings]
KM = KMeans(n_clusters=k,
init = "k-means++", #default = "k-means++"
n_init = "auto",
random_state=random_state).fit(embeddings)
labels = KM.labels_
cluster_centers = KM.cluster_centers_
return labels, cluster_centers
#journals = ["pr", "pra", "prb", "prc", "prd", "pre", "prl", "rmp"]
token_df = pd.read_pickle("../../data/embeddings/virtual_fulltexts_virtual_token_embeddings.pkl")
token_type = str(sys.argv[1])
cluster_method = str(sys.argv[2])
journal = str(sys.argv[3])
if journal == "all":
journal = None
token_df = token_df.sample(frac=0.66)
if journal:
token_df = token_df.loc[token_df.journal == journal]
# remove duplicate sentences
if token_type == "sentence_emb":
token_df["check_duplicates"] = token_df.sentence.apply(lambda x: " ".join(x))
token_df = token_df.drop_duplicates(subset=["check_duplicates", "doi"]).drop("check_duplicates", axis=1)
if token_type == "token_emb":
token_df = token_df.loc[token_df.token=="virtual"]
if cluster_method == "ap":
token_df["ap_cluster"] = None
token_df["ap_cluster_filtered"] = None
# Affinity Propagation
ap_labels, cluster_centers = aff_prop(token_df, token_type, preference=None)
for target_id, label in zip(token_df.index, ap_labels):
token_df.at[target_id, "ap_cluster"] = int(label)
# filter and combine clusters
ap_f_labels = combine_clusters(ap_labels, token_df[token_type], threshold=10, remove=[])
for target_id, label in zip(token_df.index, ap_f_labels):
token_df.at[target_id, "ap_cluster_filtered"] = int(label)
if cluster_method == "km":
k = 10
token_df["k_means"] = None
token_df["k_means_filtered"] = None
# K-Means clustering
km_labels, cluster_centers = k_means(token_df, token_type, k=k, random_state=None)
for target_id, label in zip(token_df.index, km_labels):
token_df.at[target_id, f"k_means"] = int(label)
# filter and combine clusters
km_f_labels = combine_clusters(np.array(km_labels.tolist()), token_df[token_type], threshold=10, remove=[])
for target_id, label in zip(token_df.index, km_f_labels):
token_df.at[target_id, f"k_means_filtered"] = int(label)
if token_type == "token_emb":
token_df.drop("sentence_emb", axis=1).to_pickle(f"../04_analysis/clustering/all_years/{token_type}_{cluster_method}_clustering_{journal if journal else 'all'}.pkl")
if token_type == "sentence_emb":
token_df.drop("token_emb", axis=1).to_pickle(f"../04_analysis/clustering/all_years/{token_type}_{cluster_method}_clustering_{journal if journal else 'all'}.pkl")