Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transforming Python list to networkx graph

Currently I have a list:

[['Мама мыть', 10, 'рама'],
 ['Мама мыть', 10, 'рама', 5, 'долго'],
 ['Мама мыть', 10, 'рама', 3, 'вчера'],
 ['Мама мыть', 10, 'рама', 3, 'вчера', 1, 'поздно']]

I need somehow to convert it to Networkx edges, where pairs of words should become nodes of the graph, and integers between become weights :

G = nx.Graph()
G.add_edge('Мама мыть', 'рама', weight=10)
G.add_edge('рама', 'долго', weight=5)
G.add_edge('рама', 'вчера', weight=3)
G.add_edge('вчера', 'поздно', weight=1)

Currently I'm stuck and have no ideas. Any help would be appreciated!

like image 989
Alex Nikitin Avatar asked May 14 '18 12:05

Alex Nikitin


People also ask

How do I make a graph in NetworkX?

Draw the graph G using Matplotlib. Draw the nodes of the graph G. Draw the edges of the graph G. Draw node labels on the graph G.

What is Nbunch in NetworkX?

nbunch. An nbunch is a single node, container of nodes or None (representing all nodes). It can be a list, set, graph, etc.. To filter an nbunch so that only nodes actually in G appear, use G.

Can NetworkX handle large graphs?

For NetworkX, a graph with more than 100K nodes may be too large. I'll demonstrate that it can handle a network with 187K nodes in this post, but the centrality calculations were prolonged. Luckily, there are some other packages available to help us with even larger graphs.


1 Answers

Since you have repeated information, I suggest starting by creating a dictionary to ensure uniqueness. I add a check to make sure you don't have conflicting distances for repeated elements.

In [1]:
distances = {}
for row in l:
    for i in range(0, len(row)-1, 2):
        key_tuple = (row[i], row[i+2])
        d = row[i+1]
        if key_tuple in distances.keys():
            if distances[key_tuple] != d:
                print("Warning: Found a conflicting distance for {}: {} and "
                      "{}. Using last".format(key_tuple, distances[key_tuple], d))
        distances[key_tuple] = d

In [2]: distances
Out[2]:
 {('Мама мыть', 'рама'): 10,
 ('рама', 'долго'): 5,
 ('рама', 'вчера'): 3,
 ('вчера', 'поздно'): 1}

Then you can create your edges using that dictionary.

In [3]:
import networkx as nx
G = nx.Graph()
for k, v in distances.items():
    G.add_edge(k[0], k[1], weight=v)
like image 131
Julien Marrec Avatar answered Oct 10 '22 09:10

Julien Marrec