Intermediate Network Analysis in Python
Eric Ma
Data Carpentry instructor and author of nxviz package
import networkx as nx G = nx.Graph()
numbers = range(3) G.add_nodes_from(numbers, bipartite='customers')
letters = ['a', 'b'] G.add_nodes_from(letters, bipartite='products')
list(G.nodes(data=True))
[(0, {'bipartite': 'customers'}),
(1, {'bipartite': 'customers'}),
(2, {'bipartite': 'customers'}),
('b', {'bipartite': 'products'}),
('a', {'bipartite': 'products'})]
cust_nodes = [n for n, d in G.nodes(data=True) if
d['bipartite'] == 'customers']
cust_nodes
[(0, {'bipartite': 'customers'}),
(1, {'bipartite': 'customers'}),
(2, {'bipartite': 'customers'})]
nx.bipartite.degree_centrality(G, cust_nodes)
{0: 0.5,
1: 0.5,
2: 1.0,
'a': 0.333,
'b': 1.0}
Intermediate Network Analysis in Python