【问题标题】:Reading Text File of Graph data using NetworkX使用 NetworkX 读取图形数据的文本文件
【发布时间】:2017-04-27 16:46:47
【问题描述】:

我对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()

【问题讨论】:

    标签: graph networkx


    【解决方案1】:

    请找到解决方案。这可能会帮助像我这样的人:

    # 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      相关资源
      最近更新 更多