Skip to content

Instantly share code, notes, and snippets.

@jaredjburgess
Created February 21, 2016 15:04
Show Gist options
  • Save jaredjburgess/494d604f67e3a614b8f5 to your computer and use it in GitHub Desktop.
Save jaredjburgess/494d604f67e3a614b8f5 to your computer and use it in GitHub Desktop.
Visualise data using the t-SNE algorithm in Python
# Visualise given word embeddings
# words is a list of words
# data is the vector representation of each word
# Train the algorithm
from sklearn.manifold import TSNE
vis_algo = TSNE(random_state=0, verbose=10, init='pca', n_iter=200)
vis = vis_algo.fit_transform(data)
# Plot the resulting visualisation
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(50, 50))
ax.scatter(vis[:, 0], vis[:, 1])
for i, w in enumerate(words):
ax.annotate(w, (vis[i, 0], vis[i, 1]))
plt.savefig('/path/to/visualisation.eps')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment