Word2vec-获取相似度 [英] Word2vec - get rank of similarity
问题描述
鉴于我有一个word2vec模型(由gensim设计),我想获得单词之间的排名相似度.例如,假设我有"desk"一词,与"desk"最相似的词是:
Given I got a word2vec model (by gensim), I want to get the rank similarity between to words. For example, let's say I have the word "desk" and the most similar words to "desk" are:
- 表0.64
- 主席0.61
- book 0.59
- 铅笔0.52
我想创建一个函数,以便:
I want to create a function such that:
f(办公桌,书本)= 3由于书本是第三个与办公桌最相似的词.是否存在?最有效的方法是什么?
f(desk,book) = 3 Since book is the 3rd most similar word to desk. Does it exists? what is the most efficient way to do this?
推荐答案
您可以使用 rank(entity1,entity2)
获取距离-与索引相同.
You can use the rank(entity1, entity2)
to get the distance - same as the index.
model.wv.rank(sample_word, most_similar_word)
这里不需要下面给出的单独功能.出于参考目的保留它.
A separate function as given below won't be necessary here. Keeping it for information sake.
假设您在一个元组列表中包含单词列表及其向量,由 model.wv.most_similar(sample_word)
返回,如图所示
Assuming you have the list of words and their vectors in a list of tuples, returned by model.wv.most_similar(sample_word)
as shown
[('table', 0.64), ('chair', 0.61), ('book', 0.59), ('pencil', 0.52)]
下面的函数接受样本单词和最相似的单词作为params,并返回在输出中存在的索引或等级(例如[2])
The following function accepts the sample word and the most similar word as params, and returns the index or rank (eg. [2]) if it's present in the output
def rank_of_most_similar_word(sample_word, most_similar_word):
l = model.wv.most_similar(sample_word)
return [x+1 for x, y in enumerate(l) if y[0] == most_similar_word]
sample_word = 'desk'
most_similar_word = 'book'
rank_of_most_similar_word(sample_word, most_similar_word)
注意:按照注释中的建议,在使用 model.wv.most_like()
时,使用 topn = x
可获得前x个最相似的单词.
Note: use topn=x
to get the top x most similar words while using model.wv.most_similar()
, as suggested in the comments.
这篇关于Word2vec-获取相似度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!