python使用word2vec词向量表示
时间: 2023-07-07 19:15:49 浏览: 123
在 Python 中,我们可以使用 Gensim 库来训练和使用 Word2Vec 模型。下面是一个简单的示例代码,展示如何使用 Word2Vec 将文本转换为词向量表示:
```python
from gensim.models import Word2Vec
# 准备训练数据
sentences = [['this', 'is', 'the', 'first', 'sentence', 'for', 'word2vec'],
['this', 'is', 'the', 'second', 'sentence'],
['yet', 'another', 'sentence'],
['one', 'more', 'sentence'],
['and', 'the', 'final', 'sentence']]
# 训练 Word2Vec 模型
model = Word2Vec(sentences, size=100, window=5, min_count=1, workers=4)
# 使用训练好的模型将文本转换为词向量表示
vector = model.wv['sentence']
print(vector)
```
在上面的代码中,我们首先使用 Gensim 库准备了训练数据,然后使用 Word2Vec 训练了一个模型。接着,我们可以使用模型的 `wv` 属性来获取词向量,其中 `wv` 表示 Word2Vec 的词向量。最后,我们使用 `model.wv['sentence']` 表示获取单词 `sentence` 的词向量表示。
阅读全文