forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvisualize_countries.py
39 lines (30 loc) · 1.11 KB
/
visualize_countries.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
# Course URL:
# https://deeplearningcourses.com/c/natural-language-processing-with-deep-learning-in-python
# https://udemy.com/natural-language-processing-with-deep-learning-in-python
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import json
import numpy as np
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
def main(we_file='glove_model_50.npz', w2i_file='glove_word2idx_50.json'):
words = ['japan', 'japanese', 'england', 'english', 'australia', 'australian', 'china', 'chinese', 'italy', 'italian', 'french', 'france', 'spain', 'spanish']
with open(w2i_file) as f:
word2idx = json.load(f)
npz = np.load(we_file)
W = npz['arr_0']
V = npz['arr_1']
We = (W + V.T) / 2
idx = [word2idx[w] for w in words]
# We = We[idx]
tsne = TSNE()
Z = tsne.fit_transform(We)
Z = Z[idx]
plt.scatter(Z[:,0], Z[:,1])
for i in range(len(words)):
plt.annotate(s=words[i], xy=(Z[i,0], Z[i,1]))
plt.show()
if __name__ == '__main__':
main()