Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. from matplotlib import pyplot as plt
  2. from sklearn.manifold import TSNE
  3.  
  4. def tsne_plot():
  5. "Creates and TSNE model and plots it"
  6. labels = []
  7. tokens = []
  8.  
  9. for word in selected_words:
  10. if word in word2index.keys():
  11. tokens.append(word2vec(word))
  12. labels.append(word)
  13.  
  14. tsne_model = TSNE(perplexity=40, n_components=2, init='pca', n_iter=2500, random_state=23)
  15. new_values = tsne_model.fit_transform(tokens)
  16.  
  17. x = []
  18. y = []
  19. for value in new_values:
  20. x.append(value[0])
  21. y.append(value[1])
  22.  
  23. plt.figure(figsize=(16, 16))
  24. for i in range(len(x)):
  25. plt.scatter(x[i],y[i])
  26. plt.annotate(labels[i],
  27. xy=(x[i], y[i]),
  28. xytext=(5, 2),
  29. textcoords='offset points',
  30. ha='right',
  31. va='bottom')
  32. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement