-
Notifications
You must be signed in to change notification settings - Fork 1
/
exp_clustering_dbscan_print_titles.py
130 lines (87 loc) · 3.56 KB
/
exp_clustering_dbscan_print_titles.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
from text.bag_of_words import bow_from_news
from exp_corpus_loader import load_cleaned_news
from exp_corpus_loader import get_corpus_bow
import os
import numpy as np
import pandas as pd
import pickle
from sklearn.cluster import DBSCAN
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from MulticoreTSNE import MulticoreTSNE as TSNE
def load(filename):
with open(filename, "rb") as fp:
return pickle.load(fp)
def save(obj, filename):
with open(filename, "wb") as fp:
pickle.dump(obj, fp, protocol=4)
news = load_cleaned_news()
corpus, labels = get_corpus_bow()
ground_truth_file = 'data/vectors_ground_truth.bin'
print("Computing ground truth...")
if os.path.isfile(ground_truth_file):
doc_bow_2d = load(ground_truth_file)
else:
doc_bow = bow_from_news(corpus,
filename=None,
normalize_words=False)
sel_doc_bow = SelectKBest(chi2, k=5000).fit_transform(doc_bow, labels)
tsne = TSNE(n_components=2, n_jobs=4, random_state=1)
doc_bow_2d = tsne.fit_transform(sel_doc_bow)
save(doc_bow_2d, ground_truth_file)
idx_filter = np.where(labels != 'Unclassified')
ids = np.array([n['id'] for n in news])
titles = np.array([n['title'] for n in news])
print("Computing ground truth... DONE!")
print("Computing agglomerative clustering Doc2Vec...")
dbscan = DBSCAN(eps=0.35,
min_samples=5,
metric='precomputed',
n_jobs=2)
dist_file = 'data/dist_doc2vec.bin'
vectors_dist = load(dist_file)
pred_labels = dbscan.fit_predict(vectors_dist)
pd.DataFrame({'id': ids[idx_filter],
'title': titles[idx_filter],
'cluster': pred_labels[idx_filter],
'label': labels[idx_filter]}).to_csv('data/dbscan_doc2vec_groups.csv', index=False)
print("Computing agglomerative clustering Doc2Vec... DONE!")
print("Computing agglomerative clustering TF-IDF...")
dbscan = DBSCAN(eps=0.65,
min_samples=5,
metric='precomputed',
n_jobs=2)
dist_file = 'data/dist_tfidf.bin'
vectors_dist = load(dist_file)
pred_labels = dbscan.fit_predict(vectors_dist)
pd.DataFrame({'id': ids[idx_filter],
'title': titles[idx_filter],
'cluster': pred_labels[idx_filter],
'label': labels[idx_filter]}).to_csv('data/dbscan_tfidf_groups.csv', index=False)
print("Computing agglomerative clustering TF-IDF... DONE!")
print("Computing agglomerative clustering BOW...")
dbscan = DBSCAN(eps=0.6,
min_samples=20,
metric='precomputed',
n_jobs=2)
dist_file = 'data/dist_bow.bin'
vectors_dist = load(dist_file)
pred_labels = dbscan.fit_predict(vectors_dist)
pd.DataFrame({'id': ids[idx_filter],
'title': titles[idx_filter],
'cluster': pred_labels[idx_filter],
'label': labels[idx_filter]}).to_csv('data/dbscan_bow_groups.csv', index=False)
print("Computing agglomerative clustering BOW... DONE!")
print("Computing agglomerative clustering NEL...")
dbscan = DBSCAN(eps=0.75,
min_samples=15,
metric='precomputed',
n_jobs=2)
dist_file = 'data/dist_nel.bin'
vectors_dist = load(dist_file)
pred_labels = dbscan.fit_predict(vectors_dist)
pd.DataFrame({'id': ids[idx_filter],
'title': titles[idx_filter],
'cluster': pred_labels[idx_filter],
'label': labels[idx_filter]}).to_csv('data/dbscan_nel_groups.csv', index=False)
print("Computing agglomerative clustering NEL... DONE!")