Skip to content

Instantly share code, notes, and snippets.

@michelkana
Created July 14, 2019 20:34
Show Gist options
  • Save michelkana/afbb568231b189a276607c077ebb598b to your computer and use it in GitHub Desktop.
Save michelkana/afbb568231b189a276607c077ebb598b to your computer and use it in GitHub Desktop.
# get word2vec embeddings
words = ['russia', 'moscow', 'france', 'paris']
word_vectors = np.array([model[w] for w in words])
# pca transformation
twodim = PCA().fit_transform(word_vectors)[:,:2]
# t-sne transformation
twodim_tsne = TSNE().fit_transform(word_vectors)[:,:2]
# pca plot
plt.figure(figsize=(3,3))
plt.scatter(twodim[:,0], twodim[:,1], edgecolors='k', c='r')
for word, (x,y) in zip(words, twodim):
plt.text(x+0.05, y+0.05, word);
# t-sne plot
plt.figure(figsize=(3,3))
plt.scatter(twodim_tsne[:,0], twodim_tsne[:,1], edgecolors='k', c='r')
for word, (x,y) in zip(words, twodim_tsne):
plt.text(x+0.05, y+0.05, word);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment