Created
February 19, 2016 11:39
-
-
Save nomeyer/72da19d9c1011427ac4d to your computer and use it in GitHub Desktop.
Visualise data using the t-SNE algorithm in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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