-
Notifications
You must be signed in to change notification settings - Fork 39
/
plot_word_evolution.py
135 lines (114 loc) · 4.8 KB
/
plot_word_evolution.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
import scipy.io
import matplotlib.pyplot as plt
import data
import pickle
import numpy as np
beta = scipy.io.loadmat('./beta_100.mat')['values'] ## K x T x V
print('beta: ', beta.shape)
with open('un/min_df_100/timestamps.pkl', 'rb') as f:
timelist = pickle.load(f)
print('timelist: ', timelist)
T = len(timelist)
ticks = [str(x) for x in timelist]
print('ticks: ', ticks)
## get vocab
data_file = 'un/min_df_100'
vocab, train, valid, test = data.get_data(data_file, temporal=True)
vocab_size = len(vocab)
## plot topics
num_words = 10
times = [0, 10, 40]
num_topics = 50
for k in range(num_topics):
for t in times:
gamma = beta[k, t, :]
top_words = list(gamma.argsort()[-num_words+1:][::-1])
topic_words = [vocab[a] for a in top_words]
print('Topic {} .. Time: {} ===> {}'.format(k, t, topic_words))
print('Topic Climate Change...')
num_words = 10
for t in range(46):
gamma = beta[46, t, :]
top_words = list(gamma.argsort()[-num_words+1:][::-1])
topic_words = [vocab[a] for a in top_words]
print('Time: {} ===> {}'.format(t, topic_words))
fig, axes = plt.subplots(nrows=2, ncols=4, figsize=(18, 9), dpi=80, facecolor='w', edgecolor='k')
ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8 = axes.flatten()
ticks = [str(x) for x in timelist]
#plt.xticks(np.arange(T)[0::10], timelist[0::10])
words_1 = ['vietnam', 'war', 'pakistan', 'indonesia']
tokens_1 = [vocab.index(w) for w in words_1]
betas_1 = [beta[1, :, x] for x in tokens_1]
for i, comp in enumerate(betas_1):
ax1.plot(range(T), comp, label=words_1[i], lw=2, linestyle='--', marker='o', markersize=4)
ax1.legend(frameon=False)
print('np.arange(T)[0::10]: ', np.arange(T)[0::10])
ax1.set_xticks(np.arange(T)[0::10])
ax1.set_xticklabels(timelist[0::10])
ax1.set_title('Topic "Southeast Asia"', fontsize=12)
words_5 = ['health', 'education', 'hunger', 'terrorism', 'water']
tokens_5 = [vocab.index(w) for w in words_5]
betas_5 = [beta[5, :, x] for x in tokens_5]
for i, comp in enumerate(betas_5):
ax2.plot(comp, label=words_5[i], lw=2, linestyle='--', marker='o', markersize=4)
ax2.legend(frameon=False)
ax2.set_xticks(np.arange(T)[0::10])
ax2.set_xticklabels(timelist[0::10])
ax2.set_title('Topic "Poverty & Development"', fontsize=12)
words_11 = ['iran', 'iraq', 'imperialism']
tokens_11 = [vocab.index(w) for w in words_11]
betas_11 = [beta[11, :, x] for x in tokens_11]
for i, comp in enumerate(betas_11):
ax3.plot(comp, label=words_11[i], lw=2, linestyle='--', marker='o', markersize=4)
ax3.legend(frameon=False)
ax3.set_xticks(np.arange(T)[0::10])
ax3.set_xticklabels(timelist[0::10])
ax3.set_title('Topic "War"', fontsize=12)
words_13 = ['iran', 'treaty', 'trade', 'race', 'nonproliferation']
tokens_13 = [vocab.index(w) for w in words_13]
betas_13 = [beta[13, :, x] for x in tokens_13]
for i, comp in enumerate(betas_13):
ax4.plot(comp, label=words_13[i], lw=2, linestyle='--', marker='o', markersize=4)
ax4.legend(frameon=False)
ax4.set_xticks(np.arange(T)[0::10])
ax4.set_xticklabels(timelist[0::10])
ax4.set_title('Topic "Nuclear Weapons"', fontsize=12)
#words_28 = ['men', 'equality', 'gender', 'female', 'education']
words_28 = ['education', 'gender', 'equality']
tokens_28 = [vocab.index(w) for w in words_28]
betas_28 = [beta[28, :, x] for x in tokens_28]
for i, comp in enumerate(betas_28):
ax5.plot(comp, label=words_28[i], lw=2, linestyle='--', marker='o', markersize=4)
ax5.legend(frameon=False)
ax5.set_xticks(np.arange(T)[0::10])
ax5.set_xticklabels(timelist[0::10])
ax5.set_title('Topic "Human Rights"', fontsize=12)
words_30 = ['exploitation', 'legal', 'rules', 'negotiations']
tokens_30 = [vocab.index(w) for w in words_30]
betas_30 = [beta[30, :, x] for x in tokens_30]
for i, comp in enumerate(betas_30):
ax6.plot(comp, label=words_30[i], lw=2, linestyle='--', marker='o', markersize=4)
ax6.legend(frameon=False)
ax6.set_xticks(np.arange(T)[0::10])
ax6.set_xticklabels(timelist[0::10])
ax6.set_title('Topic "Ocean Exploitation"', fontsize=12)
words_46 = ['ozone', 'warming', 'emissions', 'waste']
tokens_46 = [vocab.index(w) for w in words_46]
betas_46 = [beta[46, :, x] for x in tokens_46]
for i, comp in enumerate(betas_46):
ax7.plot(comp, label=words_46[i], lw=2, linestyle='--', marker='o', markersize=4)
ax7.legend(frameon=False)
ax7.set_xticks(np.arange(T)[0::10])
ax7.set_xticklabels(timelist[0::10])
ax7.set_title('Topic "Climate Change"', fontsize=12)
words_49 = ['apartheid', 'independence', 'colonial', 'democratic']
tokens_49 = [vocab.index(w) for w in words_49]
betas_49 = [beta[49, :, x] for x in tokens_49]
for i, comp in enumerate(betas_49):
ax8.plot(comp, label=words_49[i], lw=2, linestyle='--', marker='o', markersize=4)
ax8.legend(frameon=False)
ax8.set_title('Topic "Africa"', fontsize=12)
ax8.set_xticks(np.arange(T)[0::10])
ax8.set_xticklabels(timelist[0::10])
plt.savefig('word_evolution.png')
plt.show()