如何根据生成的列表数据制作 pandas 数据框

K C*_*K C 5 python graph list dataframe pandas

我有一份共同作者名单:

ten_author_pairs = [('creutzig', 'gao'),
 ('creutzig', 'linshaw'),
 ('gao', 'linshaw'),
 ('jing', 'zhang'),
 ('jing', 'liu'),
 ('zhang', 'liu'),
 ('jing', 'xu'),
 ('briant', 'einav'),
 ('chen', 'gao'),
 ('chen', 'jing')]
Run Code Online (Sandbox Code Playgroud)

从这里我可以生成一个反例列表 - 即使用以下代码未连接的作者对:

#generating negative examples - 

from itertools import combinations

elements = list(set([e for l in ten_author_pairs for e in l])) # find all unique elements

complete_list = list(combinations(elements, 2)) # generate all possible combinations

#convert to sets to negate the order

set1 = [set(l) for l in ten_author_pairs]
complete_set = [set(l) for l in complete_list]

# find sets in `complete_set` but not in `set1`
ten_unconnnected = [list(l) for l in complete_set if l not in set1]

print(len(ten_author_pairs))
print(len(ten_unconnnected))
Run Code Online (Sandbox Code Playgroud)

接下来,我想实现一个链接预测问题,我想为其获取数据帧,如下所示:

author-pair          jaccard   Resource_Allocation    Adamic_Adar   Preferential cn_soundarajan_hopcroft      within_inter_cluster     link
creutzig-linshaw      0.25       0.25                  0.25          0.25          0.25                         0.25                     1 
Run Code Online (Sandbox Code Playgroud)

我可以计算这些并使用 networkx 文档将分数作为输出列表,但我无法将其放在一起作为表格,如上所示。

就像积极的例子(上面提到的列表)一样,我可以使用以下方法生成数据框:

df = pd.DataFrame(list, columns = ['u1','u2])
Run Code Online (Sandbox Code Playgroud)

然后用以下方法制作图表:

G = nx.from_pandas_edgelist(df, u1, u2, create_using = nx.Graph())
Run Code Online (Sandbox Code Playgroud)

之后对于杰卡德索引我可以应用:

nx.jaccard_coefficient(G)
Run Code Online (Sandbox Code Playgroud)

这会返回带有杰卡德分数的节点对列表。

“链接”列是用逻辑生成的 - 1 表示共同作者,0 表示反例中的配对。

但是,我需要将所有相应的分数作为上述表格。

任何人都可以帮助我如何构建上述数据框。

(提到的分数只是为了表明我需要的表格类型)

小智 0

哦——这是美好的两年,但我只是偶然发现了这一点......如果我正确理解你,建立在你的基础上:

from itertools import combinations
import pandas as pd
import networkx as nx

elements = list(set([e for l in ten_author_pairs for e in l]))
complete_list = list(combinations(elements, 2))
set1 = [set(l) for l in ten_author_pairs]

df = pd.DataFrame(set1, columns=["u1", "u2"])
G = nx.from_pandas_edgelist(df, "u1", "u2", create_using=nx.Graph())
Run Code Online (Sandbox Code Playgroud)

然后定义生成器列表

list_generators = [
    nx.jaccard_coefficient,
    nx.resource_allocation_index,
    nx.adamic_adar_index,
    nx.preferential_attachment,
]
Run Code Online (Sandbox Code Playgroud)

构建分数数据框:

dfx = pd.DataFrame()
for item_generator in list_generators:
    if dfx.shape[0]:
        dfx = dfx.merge(
            right=get_df_network(generator=item_generator, graph=G),
            left_index=True,
            right_index=True,
        )
    else:
        dfx = get_df_network(generator=item_generator, graph=G)
Run Code Online (Sandbox Code Playgroud)

最后合并到链接数据框中

df_link = (
    pd.DataFrame(set1, columns=["node_0", "node_1"])
    .set_index(["node_0", "node_1"])
    .assign(link=[1] * len(set1))
)

dfx.merge(df_link, left_index=True, right_index=True, how="outer").fillna(0)
Run Code Online (Sandbox Code Playgroud)

能胜任这份工作吗?