From CSV to Bipartite Network to One-Mode Projection

I am continuing my effort to develop my own stack of scripts that do exactly what I want and that I understand how they work. They are not, in all honesty, scalable like the work done by Tim Tangherlini, but my work here pleases me.

I don’t have weighting in place yet to make the projection have more meaning than it does, but this code uses `pandas` and works quite well:

#! /usr/bin/env python

import pandas as pd, networkx as nx, matplotlib.pyplot as plt
from networkx.algorithms import bipartite

# Build lists of nodes and edges:

df = (pd.read_csv(‘tales-01.txt’, header=None)
.apply(lambda x : pd.DataFrame ([[x.iloc[0,0],v] for v in x.iloc[0,1:]]))
edges = df.values.tolist()
nodes_0 = list(set(df[‘text’].values.tolist()))
nodes_1 = list(set(df[‘word’].values.tolist()))

# Build a bipartite graph:

B = nx.Graph()
B.add_nodes_from(nodes_0, bipartite=0) # Add the node attribute “bipartite”
B.add_nodes_from(nodes_1, bipartite=1)

# Project one side of the graph:

G = nx.projected_graph(B, nodes_1)
with_labels = True,
node_color = ‘#00CCFF’)

# Choose your output:
# plt.savefig(“graphing.png”, dpi=300)

For those less familiar with bipartite networks, Wikipedia as always has [a decent introduction][].

[a decent introduction]:

Leave a Reply