2016-12-12 104 views
0

我對networkX非常陌生。所以在非常基本的事情上有問題。使用NetworkX讀取圖形數據的文本文件

我在文本文件中的網絡數據格式如下:

InNode OutNode 

N1  N5 
N2  N4 
N3  N6 
N2  N2 
N4  N7 

我的問題有以下幾點:

1)如何讀取使用networkX數據,這樣我可以得到圖形之間的節點和邊緣?

2)如何計算網絡的自我邊緣(N2,N2)?

我試過下面的代碼。但它並沒有給我正確的答案。

import matplotlib 
import networkx as net 
import urllib 
import csv 


g = net.Graph() 

f1 = csv.reader(open("data.txt","rb")) 

for row in f1: 
    g.add_nodes_from(row) 

len(g) 

g.number_of_nodes() 

回答

2

請找到解決方案。這可能有助於像我這樣的人:

# Reading the file. "DiGraph" is telling to reading the data with node-node. "nodetype" will identify whether the node is number or string or any other type. 


g = nx.read_edgelist("data.txt",create_using=nx.DiGraph(), nodetype = int) 

# check if the data has been read properly or not. 

nx.info(g) 

# count the number of nodes 

g.number_of_nodes() 

# number of self-nodes 

g.selfloop_edges()