【发布时间】:2020-10-11 20:25:33
【问题描述】:
我正在尝试应用 word2vec 来检查我的数据集每一行的两列的相似性。
例如:
Sent1 Sent2
It is a sunny day Today the weather is good. It is warm outside
What people think about democracy In ancient times, Greeks were the first to propose democracy
I have never played tennis I do not know who Roger Feder is
要应用 word2vec,我考虑以下几点:
import numpy as np
words1 = sentence1.split(' ')
words2 = sentence2.split(' ')
#The meaning of the sentence can be interpreted as the average of its words
sentence1_meaning = word2vec(words1[0])
count = 1
for w in words1[1:]:
sentence1_meaning = np.add(sentence1_meaning, word2vec(w))
count += 1
sentence1_meaning /= count
sentence2_meaning = word2vec(words1[0])
count = 1
for w in words1[1:]:
sentence1_meaning = np.add(sentence1_meaning, word2vec(w))
count += 1
sentence1_meaning /= count
sentence2_meaning = word2vec(words2[0])
count = 1
sentence2_meaning = word2vec(words2[0])
count = 1
for w in words2[1:]:
sentence2_meaning = np.add(sentence2_meaning, word2vec(w))
count += 1
sentence2_meaning /= count
#Similarity is the cosine between the vectors
similarity = np.dot(sentence1_meaning, sentence2_meaning)/(np.linalg.norm(sentence1_meaning)*np.linalg.norm(sentence2_meaning))
但是,这应该适用于不在 pandas 数据框中的两个句子。
您能否告诉我在使用 pandas 数据帧检查 send1 和 sent2 之间的相似性时应用 word2vec 需要做什么?我想要一个新的结果列。
【问题讨论】:
-
在一列中有句子。计算
word2vec句子表示。计算 [square] 成对距离矩阵。
标签: python pandas nlp word2vec