相关文章推荐
腹黑的弓箭  ·  通过openpyxl的delete_rows ...·  1 年前    · 
逃课的黑框眼镜  ·  document.visibilitySta ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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:

4 3 5 1

Each line represents a node and its neighbors, i.e. 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.

Yes, but the file is too big to upload. Basically the structure is like what I listed, for each line, the first node is a userid in social network, followed by other IDs that are "friends" with the first ndoe. I'm starting to doubt whether it is a edge list or not? – beefHotpot Aug 31, 2018 at 15:19

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:

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)]
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.