深度阅读

How to visualize Word2Vec embeddings in Gensim?

作者
作者
2023年08月22日
更新时间
10.94 分钟
阅读时间
0
阅读量

To visualize Word2Vec embeddings in Gensim, you can use the t-SNE algorithm to reduce the dimensionality of the embeddings, and then plot them using a scatter plot. Here is an example code snippet:

import gensim
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt

# Load trained Word2Vec model
model = gensim.models.Word2Vec.load('path/to/model')

# Get vectors for a sample of words
sample_words = ['cat', 'dog', 'bird', 'horse', 'fish', 'snake']
vectors = [model.wv[word] for word in sample_words]

# Use t-SNE to reduce dimensionality to 2D
tsne = TSNE(n_components=2)
vectors_2d = tsne.fit_transform(vectors)

# Plot the words as points on a scatter plot
plt.scatter(vectors_2d[:, 0], vectors_2d[:, 1])
for i, word in enumerate(sample_words):
    plt.annotate(word, xy=(vectors_2d[i, 0], vectors_2d[i, 1]))
plt.show()

This code will plot a scatter plot of the selected words, where words that have similar contexts in the original dataset should be grouped together in the plot. You can adjust the size of the plot and other visualization parameters as desired.

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。