乘和加权重networkx图python

时间:2019-01-04 12:00:58

标签: python networkx

previously asked a question关于在networkx中乘以权重以找到有向图中节点的总体份额。如果两个节点之间只有一条路径,则提供的解决方案会很好,但如果路径多,则失败。一个简单的例子:

import pandas as pd
data = pd.DataFrame({'shop': ['S1', 'S1', 'S2', 'S2', 'S3'],
                     'owner': ['S2', 'S3', 'O1', 'O2', 'O1'],
                     'share': [0.8,   0.2,  0.5,  0.5, 1.0]})
  owner  shop  share
0    S2   S1    0.8
1    S3   S1    0.2
2    O1   S2    0.5
3    O2   S2    0.5
4    O1   S3    1.0

创建图形:

import networkx as nx    
G = nx.from_pandas_edgelist(data,'shop','owner',edge_attr = ('share'), 
                               create_using=nx.DiGraph())

pos=nx.spring_layout(G, k = 0.5, iterations = 20)
node_labels = {node:node for node in G.nodes()}
nx.draw_networkx(G, pos, labels = node_labels, arrowstyle = '-|>',
                 arrowsize = 20,  font_size = 15, font_weight = 'bold')

enter image description here

要获得O1在S1中的份额,需要将2条路径相乘然后相加。以前的解决方案无法做到这一点。有办法吗?

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式修改以前的解决方案:

from operator import mul

import pandas as pd
import networkx as nx
from functools import reduce

data = pd.DataFrame({'shop': ['S1', 'S1', 'S2', 'S2', 'S3'],
                     'owner': ['S2', 'S3', 'O1', 'O2', 'O1'],
                     'share': [0.8,   0.2,  0.5,  0.5, 1.0]})

G = nx.from_pandas_edgelist(data,'shop','owner',edge_attr = ('share'),
                               create_using=nx.DiGraph())

owners = set(data['owner'])
shops  = set(data['shop'])


result = []
summary = {}
for shop in shops:
    for owner in owners:
        for path in nx.all_simple_paths(G, shop, owner):
            share = reduce(mul, (G[start][end]['share'] for start, end in zip(path[:-1], path[1:])), 1)
            summary[(owner, shop)] = summary.get((owner, shop), 0) + share


summary = pd.DataFrame.from_dict(summary, orient = 'index', columns = 'share'.split())
print(summary)

输出

          share
(O2, S2)    0.5
(O2, S1)    0.4
(S3, S1)    0.2
(O1, S2)    0.5
(O1, S3)    1.0
(O1, S1)    0.6
(S2, S1)    0.8

该行:

share = reduce(mul, (G[start][end]['share'] for start, end in zip(path[:-1], path[1:])), 1)

计算特定路径的份额。然后,使用下一行在所有路径上汇总此份额:

summary[(owner, shop)] = summary.get((owner, shop), 0) + share