根据m个项目和m x m个相似性数组创建元组列表

时间:2018-08-21 19:23:01

标签: python list dataframe tuples similarity

我列出了3个项目。

Items_list = ['a','b','c']

sklearn余弦相似度函数为项“ a”,“ b”和“ c”的所有组合提供了3 x 3矩阵的输出,如下所示:

similarities =[[1, 0.5, 0.2],
               [0.5, 1, 0.6],
               [0.2, 0.6, 1]]

我想用两列创建一个Pandas DataFrame,如下所示: 必需的输出:

  Col1                          Col2
0    a  [(a, 1), (b, 0.5), (c, 0.2)]
1    b  [(a, 0.5), (b, 1), (c, 0.6)]
2    c  [(a, 0.2), (b, 0.6), (c, 1)]

1 个答案:

答案 0 :(得分:0)

希望这就是您所需要的

import pandas as pd

item_list = ['a','b','c']

similarities =[[1, 0.5, 0.2],
               [0.5, 1, 0.6],
               [0.2, 0.6, 1]]

tuple_similarities = [list(zip(item_list, row)) for row in similarities]

df = pd.DataFrame({'Col1': item_list,
                   'Col2': tuple_similarities})

print(df)

输出:

  Col1                          Col2
0    a  [(a, 1), (b, 0.5), (c, 0.2)]
1    b  [(a, 0.5), (b, 1), (c, 0.6)]
2    c  [(a, 0.2), (b, 0.6), (c, 1)]