Last active
July 16, 2019 18:26
-
-
Save michelkana/fa8b49ccc5b248c761239b1da29854d8 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
# function to compute and display analogies between a group of word-pairs. | |
def plot_analogy(word_groups, model, func, colors, title, ax=None): | |
if ax==None: | |
fig, ax = plt.subplots(1,1,figsize=(5,5)) | |
for i, words in enumerate(word_groups): | |
analogical_word = model.most_similar(positive=[words[0], words[1]], | |
negative=[words[2]], | |
topn=1)[0][0] | |
words.append(analogical_word) | |
word_vectors = np.array([model[w] for w in words]) | |
twodim = func.fit_transform(word_vectors)[:,:2] | |
ax.scatter(twodim[:,0], twodim[:,1], edgecolors='k', c=colors[i]) | |
for word, (x,y) in zip(words, twodim): | |
ax.text(x+0.05, y+0.05, word); | |
ax.set_title(title) | |
word_groups = [['russia', 'paris', 'france'], | |
['woman', 'king', 'man'], | |
['dog', 'kitten', 'cat'], | |
['chef', 'stone', 'sculptor'], | |
['bees', 'den', 'bears'] | |
] | |
colors = ['r', 'b', 'y', 'm', 'c'] | |
plot_analogy(word_groups, model, PCA(), colors, 'PCA - 100d embeddings') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment