从成对的 Word Mover Distance 得分列表构建数据框

Construct dataframe from pairwise Word Mover Distance score list

我想 运行 对我拥有的成对句子距离(词移动距离)列表进行 PCA 分析。到目前为止,我已经得到了每对句子的相似度分数。将所有成对相似性分数存储在列表中。我的主要问题是:

如何构建包含这些与原始句子索引相似度得分的矩阵?目前,该列表仅包含每对的分数。还没有找到将分数映射回句子本身的方法。

我理想的数据框是这样的:

>             Sentence1  Sentence2  Sentence3   
 Sentence1.     1          0.5        0.8
 Sentence2      0.5        1          0.4
 Sentence3      0.8        0.4        1

然而,我的相似度得分列表是这样的,没有索引:

[0.5, 0.8, 0.4]

如何将其转换为可以 运行 PCA 使用的数据框?谢谢!

----我构建成对相似度得分的步骤

# Tokenize all sentences in a column
tokenized_sentences = [s.split() for s in df[col]]

# calculate distance between 2 responses using wmd
def find_similar_docs(sentence_1, sentence_2):
   distance = model.wv.wmdistance(sentence_1, sentence_2)
   return distance

# find response pairs
pairs_sentences = list(combinations(tokenized_sentences, 2))

# get all similiarity scores between sentences
list_of_sim = []
for sent_pair in pairs_sentences:
   sim_curr_pair = find_similar_docs(sent_pair[0], sent_pair[1])
   list_of_sim.append(sim_curr_pair)

如果我有“1”而不是标记化的句子([“我”,“开放”,“交流”,“文化”])作为索引,会容易得多。 :) 所以我有点卡在这里...

使用 numpy 制作距离矩阵,然后转换为 pandas 数据帧。

import numpy as np
import pandas as pd

# calculate distance between 2 responses using wmd
def find_similar_docs(sentence_1, sentence_2):
    distance = model.wv.wmdistance(sentence_1, sentence_2)
    return distance
  
# create distance matrix
tokenized_sentences = [s.split() for s in df[col]]
l = len(tokenized_sentences)
distances = np.zeros((l, l))
for i in range(l):
    for j in range(l):
        distances[i, j] = find_similar_docs(tokenized_sentences[i], tokenized_sentences[j])

# make pandas dataframe
labels = ['sentence' + str(i + 1) for i in range(l)]
df = pd.DataFrame(data=distances, index=labels, columns=labels)
print(df)