我有一个2列的 Dataframe ,我特灵得到每对句子的余弦相似度得分。
Dataframe (df)
A B
0 Lorem ipsum ta lorem ipsum
1 Excepteur sint occaecat excepteur
2 Duis aute irure aute irure
我尝试过的一些代码片段是:
1. df["cosine_sim"] = df[["A","B"]].apply(lambda x1,x2:cosine_sim(x1,x2))
2. from spicy.spatial.distance import cosine
df["cosine_sim"] = df.apply(lambda row: 1 - cosine(row['A'], row['B']), axis = 1)
上述代码没有工作,我仍在尝试不同的方法,但在此期间,我将感谢任何指导,谢谢你提前!
预期输出:
A B cosine_sim
0 Lorem ipsum ta lorem ipsum 0.8
1 Excepteur sint occaecat excepteur 0.5
2 Duis aute irure aute irure 0.4
1条答案
按热度按时间igsr9ssn1#
您需要先将句子转换为向量,此过程称为 * 文本向量化 *。执行文本向量化的方法有很多,具体取决于所需的复杂程度、语料库的外观、和预期的应用。最简单的是“词袋”(BoW),我已经在下面实现了它。一旦你理解了将句子表示为向量的含义,你就可以使用其他更复杂的方法来表示 * 词法 * 相似性。例如:
进一步到 * 语义 * 相似性的度量,您可以使用Doc 2 Vec [1]这样的方法,它开始使用“嵌入空间”来表示文本的语义。最近的方法如SentenceBERT [2]和Dense Passage Retrieval [3]使用基于Transformer的技术(编码器-解码器)架构[4],以允许形成“上下文感知”表示。
溶液
输出
参考文献
[1] Le,Q.和Mikolov,T.,2014年6月。句子和文档的分布式表示。机器学习国际会议(第1188-1196页)。PMLR。
[2] N.赖默斯和I.古列维奇,2019年。句子-伯特:使用连体伯特网络的句子嵌入。arXiv预印本arXiv:1908.10084。
Karpukhin,V.,Oğuz,B.,Min,S.,刘易斯,P.,Wu,L.,Edunov,S.,Chen,D.和Yih,W.T.,2020.用于开放领域问答的密集段落检索. arXiv预印本arXiv:2004.04906.
[4] Vaswani,A.,Shazeer,N.,Parmar,N.,Uszkoreit,J.,Jones,L.,Gomez,A.N.,Kaiser,.和Polosukhin,I.,2017。注意力是你所需要的一切。神经信息处理系统的进展,30。