Constructing a graph
Before we can do anything, we need a graph to play with. As with the last chapter, we will make use of the NetworkX Les Miserables graph, for familiarity.
First, we’ll create the graph and remove the additional fields that we don’t need:
import networkx as nx import pandas as pd G = nx.les_miserables_graph() df = nx.to_pandas_edgelist(G)[['source', 'target']] # dropping 'weight' G = nx.from_pandas_edgelist(df) G_named = G.copy() G = nx.convert_node_labels_to_integers(G, first_label=0, ordering='default', label_attribute=None) nodes = G_named.nodes
If you look closely, I’ve included two lines of code that create a G_named
graph as a copy of G, and have converted node labels on graph G to numbers for use in Karate Club a bit later in this chapter. This is a required step for working with Karate Club.
Let’s visualize graph G for a sanity check:
draw_graph(G, node_size=4, edge_width...