[英]Randomly select vector in gensim word2vec
I trained a word2vec model using gensim and I want to randomly select vectors from it, and find the corresponding word.我使用 gensim 训练了一个 word2vec 模型,我想从中随机选择向量,并找到相应的单词。 What is the best what to do so?
最好的方法是什么?
If your Word2Vec
model instance is in the variable model
, then there's a list of all words known to the model in model.wv.index2word
.如果您的
Word2Vec
模型实例在变量model
,那么在model.wv.index2word
有模型已知的所有单词的列表。 (The properties are slightly different in older versions of gensim.) (旧版本的 gensim 中的属性略有不同。)
So, you can pick one item using Python's built-in choice()
method in the random
module:因此,您可以使用 Python 内置的
choice()
方法在random
模块中choice()
一项:
import random
print(random.choice(model.wv.index2entity)
If you want to get n random words (keys) from word2vec with Gensim 4.0.0 just use random.sample :如果您想使用 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))
Piece a cake :)一块蛋糕:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.