Word2Vec: A Powerful Python Library for Word Vectors

Python Natural Language Processing Tool: Word2Vec from Beginner to Practical

Hello everyone, I am Niu Ge! Today, I will take you deep into understanding Word2Vec, a very important tool in the field of natural language processing. With it, we can enable computers to truly “understand” the relationships between words, achieving smarter text processing.

What is Word2Vec?

Imagine if we could transform each word into a string of numbers, and that the numbers for words with similar meanings are also close together. This is what Word2Vec aims to achieve! A simple example: “王” – “男” + “女” ≈ “后”, isn’t it magical?

Installing Necessary Libraries

Run the following in Python:

pip install gensim
import gensim
from gensim.models import Word2Vec
import jieba

Your First Word2Vec Model

Let’s start with a simple example:

Run the following in Python:

sentences = [
    [‘I’, ‘like’, ‘to’, ‘eat’, ‘apples’],
    [‘I’, ‘love’, ‘to’, ‘eat’, ‘bananas’],
    [‘puppy’, ‘likes’, ‘to’, ‘eat’, ‘bones’]
]

model = Word2Vec(sentences, vector_size=100, window=3, min_count=1)

# Find similar words
similar_words = model.wv.most_similar(‘like’)
print(“Words similar to ‘like’:”, similar_words)

Practical Case: Sentiment Analysis

Let’s do a simple review sentiment analysis:

Run the following in Python:

# Prepare training data
comments = [
    “This restaurant is really nice, the food is delicious”,
    “The service attitude is poor, I won’t come again”,
    “The price is reasonable, the environment is great”
]

# Tokenization
cut_comments = [list(jieba.cut(comment)) for comment in comments]

# Train the model
model = Word2Vec(cut_comments, vector_size=100, window=5, min_count=1)

# Get word vector
def get_word_vector(word):
    try:
        return model.wv[word]
    except KeyError:
        return None

Practical Tips

  1. Model Parameter Explanation

    • vector_size: Dimension of word vectors
    • window: Context window size
    • min_count: Word frequency threshold
  2. ⚠️ Common Issues

    • The training corpus should be large enough
    • Pay attention to the accuracy of Chinese word segmentation
    • Training word vectors takes time, it is recommended to use pre-trained models

Exercises

  1. Try using Word2Vec to find the most similar words to “Beijing”.
  2. Implement a simple word analogy: “Man – Woman = King – ?”

Reference Answers:

Run the following in Python:

# Answer for Exercise 1
similar_cities = model.wv.most_similar(‘Beijing’)

# Answer for Exercise 2
result = model.wv.most_similar(
    positive=[‘King’, ‘Woman’],
    negative=[‘Man’]
)

Advanced Applications

  • Text Classification
  • Sentiment Analysis
  • Recommendation Systems
  • Machine Translation

Friends, today’s Python learning ends here! Remember to practice hands-on, and feel free to ask me any questions in the comments. I wish you all a happy learning journey, may your path in Python become smoother! See you next time!

Leave a Comment