user12809368
user12809368

Reputation:

Connect column of nodes based on another column

I would need to build a network where nodes are websites and should be grouped based on a score assigned. If the website is new, then it will have a label 1, otherwise 0.

Example of data:

url          score             label
web1           5                 1
web2           10                1
web3           5                 0
web4           2                 0
...

I tried to use networkx to build the net. To group together the webs based on their score, I just used score as a common node (but probably there would be a better way to represent it). I would like to colour the webs based on label column, but I do not know how to do that. My code is:

import networkx as nx

G = nx.from_pandas_edgelist(df, 'url', 'score')


nodes = G.nodes()
plt.figure(figsize=(40,50)) 
pos = nx.draw(G, with_labels=True, 
              nodelist=nodes,
              node_size=1000) 

I hope you can give me some tips.

Upvotes: 0

Views: 153

Answers (1)

yatu
yatu

Reputation: 88236

Probably a partition graph might be a good idea if you want to include the score as a node too. You can start by creating the graph with nx.from_pandas_edgelist as you did, and update the node attributes as:

B = nx.from_pandas_edgelist(df, source='url', target='score')

node_view = B.nodes(data=True)
for partition_nodes, partition in zip((df.url, df.score), (0,1)):
    for node in partition_nodes.to_numpy():
        node_view[node]['bipartite'] = partition

Now we have the partition attributes for each node:

B.nodes(data=True)
NodeDataView({'web1': {'bipartite': 0}, 5: {'bipartite': 1}, 'web2': 
{'bipartite': 0}, 10: {'bipartite': 1}, 'web3': {'bipartite': 0}, 
'web4': {'bipartite': 0}, 2: {'bipartite': 1}})

The graph can be represented with a partition layout:

part1_nodes = [node for node, attr in B.nodes(data=True) if attr['bipartite']==0]
fig = plt.figure(figsize=(12,8))
plt.box(False)
nx.draw_networkx(
    B,
    pos = nx.drawing.layout.bipartite_layout(B, part1_nodes),
    node_color=[]
    node_size=800) 

enter image description here

Upvotes: 1

Related Questions