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!
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With