-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
262 lines (232 loc) · 8.94 KB
/
utils.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
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.spatial.distance import euclidean
import umap
from sklearn.decomposition import PCA
from sklearn.metrics import silhouette_score
from sklearn.metrics import calinski_harabasz_score
from sklearn.metrics import davies_bouldin_score
from sklearn.cluster import *
import pandas as pd
def plot_similarity_mat(data: np.array, cluster_labels: np.array):
"""
plots sorted similarity matrix of given 2D numpy array
:param data:
:param cluster_labels:
:return:
"""
sorted_indices = np.argsort(cluster_labels)
sorted_data = data[sorted_indices]
size = sorted_data.shape[0]
similarity_matrix = np.zeros((size, size))
for i in range(size):
for j in range(size):
similarity_matrix[i, j] = 1 / (1 + euclidean(sorted_data[i], sorted_data[j]))
correlation_matrix = np.corrcoef(similarity_matrix)
# Plot heatmap
plt.figure(figsize=(10, 8))
sns.heatmap(similarity_matrix, cmap='magma', xticklabels=False, yticklabels=False)
plt.title('Sorted Similarity Matrix')
plt.xticks(np.arange(0, size, 100), np.arange(0, size, 100))
plt.yticks(np.arange(0, size, 100), np.arange(0, size, 100))
plt.xlabel('Points')
plt.ylabel('Points')
plt.show()
def UMAP_reduce(data: np.array, rdims: int = 60) -> np.array:
"""
performs umap reduction on given 2D numpy array and returns the reduced version
:param data:
:param rdims:
:return:
"""
reducer = umap.UMAP(
n_neighbors=25,
min_dist=0.01,
n_components=rdims)
reduced = reducer.fit_transform(data)
return reduced
def print_eval_scores(data: np.array, cluster_labels: np.array, n_clusters: int):
"""
prints 3 evaluation scores
silhouette_score, calinski_harabasz_score,davies_bouldin_score
:param data:
:param cluster_labels:
:param n_clusters:
:return:
"""
silhouette_avg = silhouette_score(data, cluster_labels)
calsinski_score = calinski_harabasz_score(data, cluster_labels)
davies_score = davies_bouldin_score(data, cluster_labels)
print(
"For n_clusters =",
n_clusters,
"\nThe average silhouette_score is :",
silhouette_avg,
"\nand calinski harabasz score is :",
calsinski_score,
"\ndavies bouldin score is: ",
davies_score
)
print("***************************************************")
def plot_score_silhouette(k: list[int], title: str, data: np.array, clustering, ax=None):
"""
plots silouette score with respect to different number of clusters
:param k:
:param title:
:param data:
:param clustering:
:param ax:
:return:
"""
silhouette_scores = []
for i in k:
clustering.n_clusters = i
labels = clustering.fit_predict(data) # Assuming you have your data stored in X
silhouette_avg = silhouette_score(data, labels)
silhouette_scores.append(silhouette_avg)
if ax is None:
fig, ax = plt.subplots()
ax.plot(k, silhouette_scores, marker='o', color='black')
ax.set_title(title)
ax.set_xlabel('Number of clusters (k)')
ax.set_ylabel('Silhouette Score')
ax.grid(True)
ax.axhline(min(silhouette_scores), color='red', linestyle='--', label=f'Min: {min(silhouette_scores):.2f}')
ax.axhline(max(silhouette_scores), color='blue', linestyle='--', label=f'Max: {max(silhouette_scores):.2f}')
# Label the minimum and maximum silhouette scores on the y-axis
ax.text(k[0], min(silhouette_scores), f'{min(silhouette_scores):.2f}', va='center', ha='right',
backgroundcolor='w')
ax.text(k[0], max(silhouette_scores), f'{max(silhouette_scores):.2f}', va='center', ha='right',
backgroundcolor='w')
ax.legend()
plt.show()
def plot_score_calinski_harabasz(k: list[int], title: str, data: np.array, clustering, ax=None):
"""
plots calinski_harabasz score with respect to different number of clusters
:param k:
:param title:
:param data:
:param clustering:
:param ax:
:return:
"""
silhouette_scores = []
for i in k:
clustering.n_clusters = i
labels = clustering.fit_predict(data) # Assuming you have your data stored in X
silhouette_avg = calinski_harabasz_score(data, labels)
silhouette_scores.append(silhouette_avg)
if ax is None:
fig, ax = plt.subplots()
ax.plot(k, silhouette_scores, marker='o', color='black')
ax.set_title(title)
ax.set_xlabel('Number of clusters (k)')
ax.set_ylabel('Calinski Harabaz index')
ax.grid(True)
ax.axhline(min(silhouette_scores), color='red', linestyle='--', label=f'Min: {min(silhouette_scores):.2f}')
ax.axhline(max(silhouette_scores), color='blue', linestyle='--', label=f'Max: {max(silhouette_scores):.2f}')
# Label the minimum and maximum silhouette scores on the y-axis
ax.text(k[0], min(silhouette_scores), f'{min(silhouette_scores):.2f}', va='center', ha='right',
backgroundcolor='w')
ax.text(k[0], max(silhouette_scores), f'{max(silhouette_scores):.2f}', va='center', ha='right',
backgroundcolor='w')
ax.legend()
plt.show()
def plot_davies_bouldin_index(k: list[int], title: str, data: np.array, clustering, ax=None):
"""
plots davies_bouldin score with respect to different number of clusters
:param k:
:param title:
:param data:
:param clustering:
:param ax:
:return:
"""
silhouette_scores = []
for i in k:
clustering.n_clusters = i
labels = clustering.fit_predict(data) # Assuming you have your data stored in X
silhouette_avg = davies_bouldin_score(data, labels)
silhouette_scores.append(silhouette_avg)
if ax is None:
fig, ax = plt.subplots()
ax.plot(k, silhouette_scores, marker='o', color='black')
ax.set_title(title)
ax.set_xlabel('Number of clusters (k)')
ax.set_ylabel('Davies Bouldin index')
ax.grid(True)
ax.axhline(min(silhouette_scores), color='red', linestyle='--', label=f'Min: {min(silhouette_scores):.2f}')
ax.axhline(max(silhouette_scores), color='blue', linestyle='--', label=f'Max: {max(silhouette_scores):.2f}')
# Label the minimum and maximum silhouette scores on the y-axis
ax.text(k[0], min(silhouette_scores), f'{min(silhouette_scores):.2f}', va='center', ha='right',
backgroundcolor='w')
ax.text(k[0], max(silhouette_scores), f'{max(silhouette_scores):.2f}', va='center', ha='right',
backgroundcolor='w')
ax.legend()
plt.show()
def print_clustered_papers(n_clusters: int, clustering, data: np.array, path: str):
"""
prints papers in clustered matter
:param n_clusters:
:param clustering:
:param data:
:param path:
:return:
"""
# kmeans = KMeans(init='k-means++', n_clusters=n_clusters)
if not isinstance(clustering, np.ndarray):
cluster_labels = clustering.fit_predict(data)
else:
cluster_labels = clustering
# kmeans: KMeans
cluster_dict = {}
for i, label in enumerate(cluster_labels):
# Add data point to the corresponding cluster in the dictionary
if label not in cluster_dict:
cluster_dict[label] = []
cluster_dict[label].append(i)
df = pd.read_csv(path, encoding='utf-8')
titles = df['title']
keywords = df['keywords']
for c_i in range(n_clusters):
print('CLUSTER ' + str(c_i))
for i in cluster_dict.get(c_i):
print(titles[i])
print('\n', keywords[i])
print('--------------------------')
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print_eval_scores(data, cluster_labels, n_clusters)
def write_clustered_papers(n_clusters: int, clustering, data: np.array, path: str):
"""
performs clustering and writes the result as a pandas dataframe
:param n_clusters:
:param clustering:
:param data:
:param path:
:return:
"""
if not isinstance(clustering, np.ndarray):
cluster_labels = clustering.fit_predict(data)
else:
cluster_labels = clustering
cluster_dict = {}
for i, label in enumerate(cluster_labels):
if label not in cluster_dict:
cluster_dict[label] = []
cluster_dict[label].append(i)
df = pd.read_csv(path, encoding='utf-8')
titles = df['title']
keywords = df['keywords']
clustered_texts = [[] for _ in range(n_clusters)]
for c_i in range(n_clusters):
for i in cluster_dict.get(c_i, []):
text = titles[i] + '\n' + keywords[i]
clustered_texts[c_i].append(text)
max_length = max(len(texts) for texts in clustered_texts)
for texts in clustered_texts:
texts.extend([''] * (max_length - len(texts)))
# Create DataFrame
df_clustered = pd.DataFrame({f'Cluster {c_i}': clustered_texts[c_i] for c_i in range(n_clusters)})
# Write DataFrame to CSV
df_clustered.to_csv(path, index=False)