Skip to content

Instantly share code, notes, and snippets.

@nomeyer
Created February 19, 2016 11:39
Show Gist options
  • Save nomeyer/72da19d9c1011427ac4d to your computer and use it in GitHub Desktop.
Save nomeyer/72da19d9c1011427ac4d 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