![](/img/trans.png)
[英]Create a directed weighted graph using python-igraph and mysql results
[英]Python Reading from a file to create a weighted directed graph using networkx
我是python和Spyder的新手。 我試圖使用networkx從帶有格式的文本文件讀入圖形:
FromNodeId ToNodeId Weight
0 1 0.15
0 2 0.95
0 3 0.8
0 4 0.5
0 5 0.45
0 6 0.35
0 7 0.4
0 8 0.6
0 9 0.45
0 10 0.7
1 2 0.45
1 11 0.7
1 12 0.6
1 13 0.75
1 14 0.55
1 15 0.1
...
我想使用可以存儲這么大的圖形的Networkx圖形格式(大約10k節點,40k邊緣)。
import networkx as nx
import matplotlib.pyplot as plt
g = nx.read_edgelist('test.txt', nodetype=int, create_using= nx.DiGraph())
print(nx.info(g))
nx.draw(g)
plt.show()
當我運行此代碼時,沒有任何反應。 我正在使用Spyder進行編輯。 你能幫忙嗎? 謝謝!
你有第一行的符號#
( read_edgelist
默認跳過行以#
開頭):
#FromNodeId ToNodeId Weight
0 1 0.15
0 2 0.95
0 3 0.8
然后修改read_edgelist
調用以定義權重列的類型:
import networkx as nx
import matplotlib.pyplot as plt
g = nx.read_edgelist('./test.txt', nodetype=int,
data=(('weight',float),), create_using=nx.DiGraph())
print(g.edges(data=True))
nx.draw(g)
plt.show()
輸出:
[(0, 1, {'weight': 0.15}), (0, 2, {'weight': 0.95}), (0, 3, {'weight':
0.8}), (0, 4, {'weight': 0.5}), (0, 5, {'weight': 0.45}), (0, 6, {'weight': 0.35}), (0, 7, {'weight': 0.4}), (0, 8, {'weight': 0.6}), (0, 9, {'weight': 0.45}), (0, 10, {'weight': 0.7}), (1, 2, {'weight':
0.45}), (1, 11, {'weight': 0.7}), (1, 12, {'weight': 0.6}), (1, 13, {'weight': 0.75}), (1, 14, {'weight': 0.55}), (1, 15, {'weight':
0.1})]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.