Getting started with community detection
Before we can start, we need a network to use. Let’s use NetworkX’s Les Miserables graph that we used in the previous chapter since it held several separate communities:
- Loading the network is simple:
import networkx as nx
import pandas as pd
G = nx.les_miserables_graph()
That’s all it takes to load the graph.
- There is a
weight
attribute that I do not want to include in the network because we don’t need edge weights for this simple demonstration. So, I’m going to drop it and rebuild the graph:df = nx.to_pandas_edgelist(G)[['source', 'target']]
# dropping 'weight'
G = nx.from_pandas_edgelist(df)
In those two steps, we converted the Les Miserables graph into a pandas
edge list, and we kept only the source
and target
fields, effectively dropping the weight
field. Let’s see how many nodes and edges exist in the network:
nx.info(G) 'Graph with...