【问题标题】:Word2vec in pandas dataframe熊猫数据框中的 Word2vec
【发布时间】: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


【解决方案1】:

我没有训练有素的word2vec 可用,所以我将展示如何使用伪造的word2vec 做你想做的事,通过tfidf 权重将单词转换为句子。

第 1 步。准备数据

from sklearn.feature_extraction.text import TfidfVectorizer
df = pd.DataFrame({"sentences": ["this is a sentence", "this is another sentence"]})

tfidf = TfidfVectorizer()
tfidf_matrix = tfidf.fit_transform(df.sentences).todense()
vocab = tfidf.vocabulary_
vocab
{'this': 3, 'is': 1, 'sentence': 2, 'another': 0}

第 2 步。有伪造的word2vec(我们的词汇量)

word2vec = np.random.randn(len(vocab),300)

第 3 步。 为句子计算包含 word2vec 的列:

sent2vec_matrix = np.dot(tfidf_matrix, word2vec) # word2vec here contains vectors in the same order as in vocab
df["sent2vec"] = sent2vec_matrix.tolist()
df

sentences   sent2vec
0   this is a sentence  [-2.098592110459085, 1.4292324332403232, -1.10...
1   this is another sentence    [-1.7879436822159966, 1.680865619703155, -2.00...

第 4 步。 计算相似度矩阵

from sklearn.metrics.pairwise import cosine_similarity
similarity = cosine_similarity(df["sent2vec"].tolist())
similarity
array([[1.        , 0.76557098],
       [0.76557098, 1.        ]])

要使您的word2vec 正常工作,您需要稍微调整第 2 步,以便word2vec 以相同的顺序(按值指定或按字母顺序)包含vocab 中的所有单词。

你的情况应该是:

sorted_vocab = sorted([word for word,key in vocab.items()])
sorted_word2vec = []
for word in sorted_vocab:
    sorted_word2vec.append(word2vec[word])

【讨论】:

  • 谢尔盖·布什马诺夫您好,非常感谢您的回答。我会问一个问题:您展示的内容是替换我的整个代码,还是只是要集成到我的代码中?我正在考虑来自两个不同列的文本,我想比较 Sent1 中的每一行句子和 Sent2 中的句子。根据我的理解(但可能我误解了),您的代码比较了同一列中的两行。如果我误解了你给我看的内容,你能告诉我吗?非常感谢
  • 我的建议是改变你的方法,只用一列句子(即将你的代码改为我的)。如果您仍然坚持将文本放在 2 个 df 列中,您仍然可以申请 cosine_similarity,但是您这样做的方式对我来说太复杂了。我只会做 3 件事:定义 sent2vec 函数,将其应用于两列,在两列之间应用 cosine_similarity。但老实说,我从来没有见过这样做的。
  • 感谢您的建议,谢尔盖。我试图做的是比较文本之间的相似性,一个在 col1 中,另一个在 col2 中,而不是在行中。例如,您可以将其视为文章的标题和语料库。我会选择你的方法,但重要的是保持两列而不是行之间的比较,因为它们对我来说意味着不同的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 2020-06-03
  • 2021-02-15
  • 2018-05-08
相关资源
最近更新 更多