Created
November 28, 2017 09:25
-
-
Save aneesha/ef095a2c9aed4de4cfc53d6d9d39413d to your computer and use it in GitHub Desktop.
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
# Method to plot the top no_similar_words in 2D using TSNE | |
def display_closestwords_tsnescatterplot(model, word, word_vector_dimension, no_similar_words, plot_title): | |
arr = np.empty((0,word_vector_dimension), dtype='f') | |
word_labels = [word] | |
# get close words | |
close_words = model.similar_by_word(word, topn=no_similar_words) | |
# add the vector for each of the closest words to the array | |
arr = np.append(arr, np.array([model[word]]), axis=0) | |
for wrd_score in close_words: | |
wrd_vector = model[wrd_score[0]] | |
word_labels.append(wrd_score[0]) | |
arr = np.append(arr, np.array([wrd_vector]), axis=0) | |
# find tsne coords for 2 dimensions | |
tsne = TSNE(n_components=2, random_state=0) | |
np.set_printoptions(suppress=True) | |
Y = tsne.fit_transform(arr) | |
x_coords = Y[:, 0] | |
y_coords = Y[:, 1] | |
# display scatter plot | |
plt.scatter(x_coords, y_coords) | |
for label, x, y in zip(word_labels, x_coords, y_coords): | |
plt.annotate(label, xy=(x, y), xytext=(0, 0), textcoords='offset points') | |
plt.xlim(x_coords.min()+0.00005, x_coords.max()+0.00005) | |
plt.ylim(y_coords.min()+0.00005, y_coords.max()+0.00005) | |
plt.xticks(rotation=35) | |
plt.title(plot_title) | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment