for e in model2.wv.most_similar('智能制造', topn=200):
时间: 2023-04-05 13:01:33 浏览: 63
我可以回答这个问题。根据模型2的词向量,与“智能制造”最相似的词汇有:智能制造技术、智能制造产业、智能制造发展、智能制造应用、智能制造系统、智能制造产业链、智能制造平台、智能制造装备、智能制造产业园、智能制造工程等。
相关问题
import logging import jieba import gensim from gensim.models import Word2Vec def get_Segment(): texts = [] jieba.load_userdict("data\\name_dict.txt") with open('data\\in_the_name_of_people.txt','r',encoding='utf-8') as f: for line in f.readlines(): texts.append(list(jieba.cut(line.strip()))) with open('data\\in_the_name_of_people_segment.txt','w',encoding='utf-8')as f: for line in texts: f.write(" ".join(w for w in line)) f.write("\r\n") def getmodel(): logging.basicConfig(format='%(asctime)s : %(LeveLname)s : %(message)s',level=logging.INFO) sentences = word2vec.LineSentence('data\\in_the_name_of_people_segment.txt') model = word2vec.Word2Vec(sentences,min_count=1) return model if __name__=='__main__': get_Segment() model = getmodel() print('相似度: ',model.wv.similarity('人民','名义')) print(model.wv.similarity('候亮平','钟小艾')) print(model.mv.most_similar('候亮平',topn=10))
这段代码存在一些问题,我来帮你解释一下:
1. 代码中缺少了部分 import 语句,需要加上:
```
from gensim.models import word2vec
```
2. 在 `logging.basicConfig()` 函数的第一个参数中,`LeveLname` 应该是 `levelname`,需要修正。
3. 在 `getmodel()` 函数中,`word2vec.LineSentence()` 应该是 `word2vec.Text8Corpus()`,需要修正。
4. 在 `getmodel()` 函数中,`model.mv.most_similar()` 应该是 `model.wv.most_similar()`,需要修正。
修正后的代码如下:
```
import logging
import jieba
import gensim
from gensim.models import word2vec
def get_Segment():
texts = []
jieba.load_userdict("data\\name_dict.txt")
with open('data\\in_the_name_of_people.txt','r',encoding='utf-8') as f:
for line in f.readlines():
texts.append(list(jieba.cut(line.strip())))
with open('data\\in_the_name_of_people_segment.txt','w',encoding='utf-8')as f:
for line in texts:
f.write(" ".join(w for w in line))
f.write("\r\n")
def getmodel():
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
sentences = word2vec.Text8Corpus('data\\in_the_name_of_people_segment.txt')
model = word2vec.Word2Vec(sentences, min_count=1)
return model
if __name__=='__main__':
get_Segment()
model = getmodel()
print('相似度: ',model.wv.similarity('人民','名义'))
print(model.wv.similarity('候亮平','钟小艾'))
print(model.wv.most_similar('候亮平',topn=10))
```
希望能帮到你!
gensim 4.0版本以上怎么使用most_similar
在gensim 4.0版本中,most_similar方法已经被弃用,替代方法是most_similar_cosmul和most_similar_all_candidates。
most_similar_cosmul方法返回与给定单词最相似的单词和相似度,使用余弦相似度进行计算。它还接受一个参数topn,用于指定返回的最相似单词的数量。
示例代码:
```
from gensim.models import Word2Vec
model = Word2Vec.load('your_model_path')
similar_words = model.wv.most_similar_cosmul('car', topn=10)
print(similar_words)
```
most_similar_all_candidates方法返回与给定单词最相似的单词和相似度,使用内积进行计算。它还接受一个参数topn,用于指定返回的最相似单词的数量。
示例代码:
```
from gensim.models import Word2Vec
model = Word2Vec.load('your_model_path')
similar_words = model.wv.most_similar_all_candidates(['car'], topn=10)
print(similar_words)
```
请注意,在gensim 4.0版本中,Word2Vec模型的训练和使用方法也有所不同。如果您需要更多帮助,请提供更多的上下文和代码示例。