forked from iworldtong/text_gcn.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsne.py
64 lines (51 loc) · 1.54 KB
/
tsne.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
from matplotlib.backends.backend_pdf import PdfPages
from sklearn.manifold import TSNE
from matplotlib import pyplot as plt
import numpy as np
import os
data_set = 'R8' # 20ng R8 R52 ohsumed mr
data_path = './data'
f = open(os.path.join(data_path, data_set + '.train.index'), 'r')
lines = f.readlines()
f.close()
train_size = len(lines)
f = open(os.path.join(data_path, data_set + '_shuffle.txt'), 'r')
lines = f.readlines()
f.close()
target_names = set()
labels = []
for line in lines:
line = line.strip()
temp = line.split('\t')
labels.append(temp[2])
target_names.add(temp[2])
target_names = list(target_names)
f = open(os.path.join(data_path, data_set + '_doc_vectors.txt'), 'r')
lines = f.readlines()
f.close()
docs = []
for line in lines:
temp = line.strip().split()
values_str_list = temp[1:]
values = [float(x) for x in values_str_list]
docs.append(values)
fea = docs[train_size:] # int(train_size * 0.9)
label = labels[train_size:] # int(train_size * 0.9)
label = np.array(label)
fea = TSNE(n_components=2).fit_transform(fea)
pdf = PdfPages(data_set + '_gcn_doc_test.pdf')
cls = np.unique(label)
# cls=range(10)
fea_num = [fea[label == i] for i in cls]
for i, f in enumerate(fea_num):
if cls[i] in range(10):
plt.scatter(f[:, 0], f[:, 1], label=cls[i], marker='+')
else:
plt.scatter(f[:, 0], f[:, 1], label=cls[i])
plt.legend(ncol=5, loc='upper center', bbox_to_anchor=(0.48, -0.08), fontsize=11)
# plt.ylim([-20,35])
# plt.title(md_file)
plt.tight_layout()
pdf.savefig()
plt.show()
pdf.close()