(we_file='glove_model_50.npz', w2i_file='glove_word2idx_50.json')
| 13 | from sklearn.manifold import TSNE |
| 14 | |
| 15 | def main(we_file='glove_model_50.npz', w2i_file='glove_word2idx_50.json'): |
| 16 | words = ['japan', 'japanese', 'england', 'english', 'australia', 'australian', 'china', 'chinese', 'italy', 'italian', 'french', 'france', 'spain', 'spanish'] |
| 17 | |
| 18 | with open(w2i_file) as f: |
| 19 | word2idx = json.load(f) |
| 20 | |
| 21 | npz = np.load(we_file) |
| 22 | W = npz['arr_0'] |
| 23 | V = npz['arr_1'] |
| 24 | We = (W + V.T) / 2 |
| 25 | |
| 26 | idx = [word2idx[w] for w in words] |
| 27 | # We = We[idx] |
| 28 | |
| 29 | tsne = TSNE() |
| 30 | Z = tsne.fit_transform(We) |
| 31 | Z = Z[idx] |
| 32 | plt.scatter(Z[:,0], Z[:,1]) |
| 33 | for i in range(len(words)): |
| 34 | plt.annotate(s=words[i], xy=(Z[i,0], Z[i,1])) |
| 35 | plt.show() |
| 36 | |
| 37 | |
| 38 | if __name__ == '__main__': |
no test coverage detected