[英]Calculating bipartite graph in networkx
假設以下示例性邊緣列表:
source,target,weight
162600,117237,200
192084,50240,200
200854,125014,200
166729,37059,5
157279,77297,1
186788,35124,2
199103,112437,5
194523,125618,5
169139,103847,5
156565,85646,5
157159,85646,10
168455,115687,15
139567,108206,5
172702,120323,5
175029,120590,5
167596,85646,10
202163,83381,15
163786,109135,5
183035,124200,4
154266,124200,2
187899,124200,20
190849,124200,8
169657,118867,11
157848,101997,2
143224,87832,1
140758,101009,21
197618,101009,20
175454,50240,4
150071,112472,10
199517,121453,20
如您所見, source
的某些元素具有共同的target
。 因此,我認為在 .network X 中計算和可視化二分圖是有意義的。
該文檔顯示了它是如何使用手動值完成的,但我想知道如何從上面將它應用於邊緣列表(包括權重。
如果有人可以提供使用上述數據的示例,我將非常感激,盡管輸入數據可能會導致 sparse.network。
我建議在有向圖中平穩運行add_weighted_edges_from()函數。 根據需要將輸入數據放入元組列表之后。 請參閱此文檔以獲取更多信息
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
INPUT = [
(162600,117237,200),
(192084,50240,200),
(200854,125014,200),
(166729,37059,5),
(157279,77297,1),
(186788,35124,2),
(199103,112437,5),
(194523,125618,5),
(169139,103847,5),
(156565,85646,5),
(157159,85646,10),
(168455,115687,15),
(139567,108206,5),
(172702,120323,5),
(175029,120590,5),
(167596,85646,10),
(202163,83381,15),
(163786,109135,5),
(183035,124200,4),
(154266,124200,2),
(187899,124200,20),
(190849,124200,8),
(169657,118867,11),
(157848,101997,2),
(143224,87832,1),
(140758,101009,21),
(197618,101009,20),
(175454,50240,4),
(150071,112472,10),
(199517,121453,20)
]
G=nx.DiGraph()
G.add_weighted_edges_from(INPUT)
elarge = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] > 22]
esmall = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] <= 21]
pos = nx.spring_layout(G, k=1)
plt.figure(figsize=(15,10))
# nodes
nx.draw_networkx_nodes(G, pos, node_size=700)
# edges
nx.draw_networkx_edges(G, pos, edgelist=elarge,
width=6)
nx.draw_networkx_edges(G, pos, edgelist=esmall,
width=1, alpha=0.5, edge_color='b', style='dashed')
# labels
nx.draw_networkx_labels(G, pos, font_size=12, font_family='sans-serif')
plt.axis('off')
plt.show()
這應該是輸出
我發現實現加載二分稀疏或 otherwise.network with weights into.networkx 的 hack-ish 方法首先將所述稀疏矩陣保存到 tab txt(通過此處的 pandas),然后使用bipartite.read_edgelist
加載它, data
參數指定邊緣權重
INPUT.to_csv('test.edgelist',sep='\t',header=False,index=False)
G = bipartite.read_edgelist(
'test.edgelist', nodetype=int, data=(("weight", float),)
)
list(G.edges(data=True))
以這種方式加載保存的邊會返回 a.networkx object。加載(稀疏)二分 object 后,您可以查詢頂部和底部節點:
top_nodes = {n for n, d in G.nodes(data=True) if d["bipartite"] == 0}
bottom_nodes = set(G) - top_nodes
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.