繁体   English   中英

Networkx-从文件中读取邻接表

[英]Networkx - read an adjacency list from a file

我正在做一个与链接预测有关的机器学习项目,但是我被网络X困住了:

我尝试读取的训练数据存储在具有以下结构的“ train.txt”文件中:

1 2
2 3
4 3 5 1

每行代表一个节点及其邻居,即在第3行中:节点4与节点3、5和1连接。

我用来读取网络数据的代码是:

G = nx.read_edgelist('train.txt',delimiter = "\t",create_using = nx.DiGraph(),nodetype = int)

但是此代码引发TypeError异常:无法按以下方式转换边缘数据:

TypeError:无法将边缘数据(['3105725','2828522','4394015','2367409','2397416',...,'759864'])转换为字典。

欢迎来到SO!

您的评论是正确的-这不是经典意义上的优势清单。 networkx的边缘列表如下所示:

1 2
2 3
4 1
4 3
4 5

这是解决问题的一种方法:逐行读取文件,然后将每个边添加到图形中。

import networkx as nx

D= nx.DiGraph()
with open('train.txt','r') as f:
    for line in f:
        line=line.split('\t')#split the line up into a list - the first entry will be the node, the others his friends
        if len(line)==1:#in case the node has no friends, we should still add him to the network
            if line[0] not in D:
                nx.add_node(line[0])
        else:#in case the node has friends, loop over all the entries in the list
            focal_node = line[0]#pick your node
            for friend in line[1:]:#loop over the friends
                D.add_edge(focal_node,friend)#add each edge to the graph

nx.draw_networkx(D) #for fun draw your network

看起来不错!

nx.read_edgelist除了每个边缘的源和目标之外,还希望每个边缘有任意数据的行,因此这不是您应该使用的数据。
networkx提供了一种使用nx.read_adjlist从文件读取邻接表的方法。
考虑一个文件graph_adjlist.txt

1   2   3   4
2   5
3   5
4   5

可以根据以下邻接表创建图形。

import networkx as nx

G = nx.read_adjlist('graph_adjlist.txt', create_using = nx.DiGraph(), nodetype = int)

print(G.nodes(),G.edges())
# [1, 2, 3, 4, 5] [(1, 2), (1, 3), (1, 4), (2, 5), (3, 5), (4, 5)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM