我正在networkx中绘制一棵树,并添加了百分比份额作为权重。示例:商店和商店的所有者,可以是另一家商店:
import pandas as pd
data = pd.DataFrame({'shop': ['S1', 'S1', 'S1', 'S2', 'S2', 'S3', 'S3', 'S3'],
'owner': ['O1', 'O2', 'S2', 'S3', 'O3', 'O4', 'O5', 'O6'],
'share': [0.2, 0.2, 0.6, 0.5, 0.5, 0.1, 0.1, 0.8]})
data
shop owner share
S1 O1 0.2
S1 O2 0.2
S1 S2 0.6
S2 S3 0.5
S2 O3 0.5
S3 O4 0.1
S3 O5 0.1
S3 O6 0.8
我可以在 networkx 中创建一个有向图,如下所示:
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')
我如何乘以图表中每个商店的权重,以便获得所有者和百分比份额?像下面这样:
output = pd.DataFrame({'shop': ['S1', 'S1', 'S1', 'S1', 'S1', 'S1', 'S2',
'S2', 'S2','S2', 'S3', 'S3', 'S3'],
'owner': ['O1', 'O2', 'O3', 'O4', 'O5', 'O6', 'O3',
'O4', 'O5','O6', 'O4', 'O5', 'O6'],
'share': [0.2, 0.2, 0.3, 0.03, 0.03, 0.24, 0.5, 0.05,
0.05, 0.4, 0.1, 0.1, 0.8]})
output
shop owner share
S1 O1 0.2
S1 O2 0.2
S1 O3 0.3
S1 O4 0.03
S1 O5 0.03
S1 O6 0.24
S2 O3 0.5
S2 O4 0.05
S2 O5 0.05
S2 O6 0.4
S3 O4 0.1
S3 O5 0.1
S3 O6 0.8
更新:感谢这个问题here https://stackoverflow.com/questions/47341773/algorithm-to-multiply-edges-of-a-networkx-graph我可以得到任意 2 个选定节点之间的乘积(权重相乘)(见下文),然后如何为数据框中的所有节点获得与上述相同的结果?
start = 'S1' # start node
end = 'O5' # end node
all_paths = [path for path in nx.all_simple_paths(G, start, end)]
for p in all_paths: # keep track of each path
for _ in range(len(p)): # for each node in this path
pairs = zip(p, p[1:]) # get sequence of nodes
product = 1
for pair in pairs: # for each pair of nodes in this path
an_edge = G.get_edge_data(pair[0], pair[1])
product *= an_edge['share']