python中word2vec如何建立
时间: 2023-06-26 21:05:06 浏览: 106
在Python中,可以使用gensim库来建立word2vec模型。以下是一个简单的例子:
首先,需要安装gensim库:
```python
!pip install gensim
```
然后,加载语料库并训练模型:
```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"]]
model = Word2Vec(sentences, min_count=1)
```
在这个例子中,我们加载一个包含多个句子的语料库,并使用`Word2Vec()`函数创建一个新的word2vec模型。`min_count`参数指定了在语料中出现次数不足指定次数的单词将被忽略,默认值为5。训练完成后,我们可以使用模型来查找与某个单词最相似的单词:
```python
similar_words = model.wv.most_similar('sentence')
print(similar_words)
```
输出:
```
[('for', 0.3252620108127594), ('this', 0.2979834077358246), ('more', 0.2846679985523224), ('is', 0.15957920217514038), ('the', 0.1412003197669983), ('first', -0.006387107789039612), ('one', -0.04775365096330643), ('and', -0.10795781719684601), ('yet', -0.16303491592407227), ('final', -0.16458679795265198)]
```
这个例子中,我们查找了与单词`"sentence"`最相似的单词,并且打印了结果。
阅读全文