Middle+
How does Word2Vec differ from Bag of Words?
sobes.tech AI
Answer from AI
Bag of Words (BoW) is a simple way of representing text, where each document is encoded as a vector of word frequencies without considering order and context. For example, the vector shows how many times each word appears in the document.
Word2Vec is a neural network model trained to represent words as dense vectors (embeddings) that reflect semantic relationships between words. Word2Vec vectors take into account context and allow finding similar words and relationships between them.
Summary:
- BoW — sparse, high-dimensional representation, ignoring order and meaning.
- Word2Vec — dense, low-dimensional representation, considering semantics and context.
Example:
# BoW example
from sklearn.feature_extraction.text import CountVectorizer
corpus = ['кот и собака', 'собака и мышь']
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus).toarray()
print(X)
# Word2Vec example (simplified)
from gensim.models import Word2Vec
sentences = [['кот', 'и', 'собака'], ['собака', 'и', 'мышь']]
model = Word2Vec(sentences, vector_size=10, window=2, min_count=1)
print(model.wv['кот'])