(we_file='word_embeddings.npy', w2i_file='wikipedia_word2idx.json', Model=PCA)
| 14 | from sklearn.decomposition import PCA, TruncatedSVD |
| 15 | |
| 16 | def main(we_file='word_embeddings.npy', w2i_file='wikipedia_word2idx.json', Model=PCA): |
| 17 | We = np.load(we_file) |
| 18 | V, D = We.shape |
| 19 | with open(w2i_file) as f: |
| 20 | word2idx = json.load(f) |
| 21 | idx2word = {v:k for k,v in iteritems(word2idx)} |
| 22 | |
| 23 | model = Model() |
| 24 | Z = model.fit_transform(We) |
| 25 | plt.scatter(Z[:,0], Z[:,1]) |
| 26 | for i in range(V): |
| 27 | plt.annotate(s=idx2word[i], xy=(Z[i,0], Z[i,1])) |
| 28 | plt.show() |
| 29 | |
| 30 | |
| 31 | if __name__ == '__main__': |
no test coverage detected