简体   繁体   English

Networkx-从文件中读取邻接表

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

I'm doing a machine learning project related to link prediction.But I'm stuck at reading data with networkX: 我正在做一个与链接预测有关的机器学习项目,但是我被网络X困住了:

The training data I'm trying to read is stored in a "train.txt" file with the following structure: 我尝试读取的训练数据存储在具有以下结构的“ train.txt”文件中:

1 2
2 3
4 3 5 1

Each line represents a node and its neighbors, ie in line 3: node 4 is connected with nodes 3, 5 and 1. 每行代表一个节点及其邻居,即在第3行中:节点4与节点3、5和1连接。

The code I'm using to read the network data is : 我用来读取网络数据的代码是:

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

But this code raises a TypeError exception: failed to convert edge data as follows: 但是此代码引发TypeError异常:无法按以下方式转换边缘数据:

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

Welcome to SO! 欢迎来到SO!

Your comment is correct - this is not an edge list in the classical sense. 您的评论是正确的-这不是经典意义上的优势清单。 An edge list for networkx looks something like: networkx的边缘列表如下所示:

1 2
2 3
4 1
4 3
4 5

Here is one way to solve your problem: read in the file line by line, and add each edge to your graph as you go. 这是解决问题的一种方法:逐行读取文件,然后将每个边添加到图形中。

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 expects a line per edge with arbitrary data, in addition to the source and destination of the edge, so it's not what you should use in you case. nx.read_edgelist除了每个边缘的源和目标之外,还希望每个边缘有任意数据的行,因此这不是您应该使用的数据。
networkx offers a way to read an adjacency list from a file by using nx.read_adjlist . networkx提供了一种使用nx.read_adjlist从文件读取邻接表的方法。
Consider a file graph_adjlist.txt . 考虑一个文件graph_adjlist.txt

1   2   3   4
2   5
3   5
4   5

The graph can be created according to the adjacency list as follows. 可以根据以下邻接表创建图形。

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