[英]Randomly select vector in gensim word2vec
我使用 gensim 训练了一个 word2vec 模型,我想从中随机选择向量,并找到相应的单词。 最好的方法是什么?
如果您的Word2Vec
模型实例在变量model
,那么在model.wv.index2word
有模型已知的所有单词的列表。 (旧版本的 gensim 中的属性略有不同。)
因此,您可以使用 Python 内置的choice()
方法在random
模块中choice()
一项:
import random
print(random.choice(model.wv.index2entity)
如果您想使用 Gensim 4.0.0 从 word2vec 中获取n 个随机单词(键),只需使用random.sample :
import random
import gensim
# Here we use Gensim 4.0.0
w2v = gensim.models.KeyedVectors.load_word2vec_format("model.300d")
# Get 10 random words (keys) from word2vec model
random_words = random.sample(w2v.index_to_key, 10)
print("Random words: "+ str(random_words))
一块蛋糕:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.