简体   繁体   中英

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:

The training data I'm trying to read is stored in a "train.txt" file with the following structure:

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.

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: Failed to convert edge data (['3105725', '2828522', '4394015', '2367409', '2397416',...,'759864']) to dictionary.

Welcome to SO!

Your comment is correct - this is not an edge list in the classical sense. An edge list for networkx looks something like:

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.
networkx offers a way to read an adjacency list from a file by using nx.read_adjlist .
Consider a file 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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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