Christopher
Christopher

Reputation: 2232

Calculating bipartite graph in networkx

Assume following exemplary edgelist:

source,target,weight
162600,117237,200
192084,50240,200
200854,125014,200
166729,37059,5
157279,77297,1
186788,35124,2
199103,112437,5
194523,125618,5
169139,103847,5
156565,85646,5
157159,85646,10
168455,115687,15
139567,108206,5
172702,120323,5
175029,120590,5
167596,85646,10
202163,83381,15
163786,109135,5
183035,124200,4
154266,124200,2
187899,124200,20
190849,124200,8
169657,118867,11
157848,101997,2
143224,87832,1
140758,101009,21
197618,101009,20
175454,50240,4
150071,112472,10
199517,121453,20

As you can see, some elements of source have a common target. Therefore, I thought it would make sense to calculate and visualize a bipartite graph in network X.

The documentation shows how it's done with manual values but I wonder how it can be applied to the edgelist from above (including weights.

I would be very thankful if someone could provide an example using the data above, though the input data might result in a sparse network.

Upvotes: 1

Views: 853

Answers (2)

Daniel Morgan
Daniel Morgan

Reputation: 46

Hack-ish way I have found to achieve loading bipartite sparse or otherwise network with weights into networkx is first to save said sparse matrix to tab txt (via pandas here) and then load it with bipartite.read_edgelist with data parameter specifying edge weights

INPUT.to_csv('test.edgelist',sep='\t',header=False,index=False)
G = bipartite.read_edgelist(
    'test.edgelist', nodetype=int, data=(("weight", float),)
)
list(G.edges(data=True))

Loading the saved edges in this way returns a networkx object. Once the (sparse) bipartite object is loaded, you can query top and bottom nodes:

top_nodes = {n for n, d in G.nodes(data=True) if d["bipartite"] == 0}
bottom_nodes = set(G) - top_nodes

Upvotes: 0

el_Rinaldo
el_Rinaldo

Reputation: 1018

I would suggest to run smoothly the add_weighted_edges_from() function in a directed graph. This just after placing your input data in a tuple list, as requested. Take a look to this documentation for more information

import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline

INPUT = [
    (162600,117237,200),
    (192084,50240,200),
    (200854,125014,200),
    (166729,37059,5),
    (157279,77297,1),
    (186788,35124,2),
    (199103,112437,5),
    (194523,125618,5),
    (169139,103847,5),
    (156565,85646,5),
    (157159,85646,10),
    (168455,115687,15),
    (139567,108206,5),
    (172702,120323,5),
    (175029,120590,5),
    (167596,85646,10),
    (202163,83381,15),
    (163786,109135,5),
    (183035,124200,4),
    (154266,124200,2),
    (187899,124200,20),
    (190849,124200,8),
    (169657,118867,11),
    (157848,101997,2),
    (143224,87832,1),
    (140758,101009,21),
    (197618,101009,20),
    (175454,50240,4),
    (150071,112472,10),
    (199517,121453,20)
]

G=nx.DiGraph()
G.add_weighted_edges_from(INPUT)

elarge = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] > 22]
esmall = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] <= 21]

pos = nx.spring_layout(G, k=1)

plt.figure(figsize=(15,10))

# nodes
nx.draw_networkx_nodes(G, pos, node_size=700)

# edges
nx.draw_networkx_edges(G, pos, edgelist=elarge,
                       width=6)
nx.draw_networkx_edges(G, pos, edgelist=esmall,
                       width=1, alpha=0.5, edge_color='b', style='dashed')

# labels
nx.draw_networkx_labels(G, pos, font_size=12, font_family='sans-serif')

plt.axis('off')
plt.show()

This should be the output

enter image description here

Upvotes: 0

Related Questions