Graph machine learning (GML) learns from data organized as nodes and edges. It matters when relationships carry information that individual records do not: road segments affect neighboring segments, for example, and optimization problems can be represented through relationships and constraints between their elements. section 11.3 section 11.3.1 section 11.2
Start with a small graph
A table of soccer players can become a graph by adding a player–club edge for each plays-for relationship and a player–nationality edge for each belongs-to relationship. The resulting connections make relationships explicit rather than leaving them as columns in separate rows. section 11.3
G = nx.Graph() #1
for index, row in df.iterrows():
G.add_edge(row['Player'], row['Club'], relationship='plays_for') #2
for index, row in df.iterrows():
G.add_edge(row['Player'], row['Nationality'], relationship='belongs_to') #3
pos = nx.kamada_kawai_layout(G) #4
plt.figure(figsize=(20, 14)) #5
player_nodes = df['Player'].unique().tolist() #6
club_nodes = df['Club'].unique().tolist() #6
nationality_nodes = df['Nationality'].unique().tolist() #6
nx.draw_networkx_nodes(G, pos, nodelist=player_nodes, node_color='blue',
label='Player Name', node_shape='o') #7
nx.draw_networkx_nodes(G, pos, nodelist=club_nodes, node_color='red', label='Club', #7
node_shape='d') #7
nx.draw_networkx_nodes(G, pos, nodelist=nationality_nodes, node_color='gray', #7
label='Nationality', node_shape='v') #7
nx.draw_networkx_edges(G, pos) #8
edge_labels = nx.get_edge_attributes(G, 'relationship') #9
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=12) #9
nx.draw_networkx_labels(G, pos) #10
plt.legend(fontsize=13, loc='upper right')
plt.show()
#1 Create a new graph.
#2 Add nodes and edges for clubs.
#3 Add nodes and edges for nationalities.
#4 Create the layout
#5 Set the size of the figure.
#6 Get lists of player, club, and nationality nodes.
#7 Draw nodes in different colors.
#8 Draw edges.
#9 Draw edge labels.
#10 Draw node labels.
The NetworkX listing shows the edge-building step for the player, club, and nationality graph. section 11.3
A graph is not a regular grid like an image. Nodes can have different neighborhoods, so a graph convolutional network (GCN) works with each node and its connected neighbors rather than sliding a filter over fixed pixel positions. Its output should not depend on the order in which nodes are listed. section 11.3.1
A practical route from graph to prediction is:
- Create graph embeddings: lower-dimensional, continuous representations of graph data that can support node classification, link prediction, and community detection. section 11.10 section 11.3
- Choose a graph-aware model when connected elements influence one another. Predicting a route’s travel time by treating its road segments as independent can miss effects between neighboring segments. section 11.3.1
- For a node-level implementation, load Cora with PyTorch Geometric, pass node features and edge indices through GCN layers, and train using labeled nodes selected by a training mask. section 11.3.1
class GCN(torch.nn.Module):
def __init__(self):
super(GCN, self).__init__()
self.conv1 = GCNConv(dataset.num_node_features, 16)
self.conv2 = GCNConv(16, dataset.num_classes)
self.dropout = torch.nn.Dropout(0.5)
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = F.relu(x)
x = F.dropout(x, training=self.training)
x = self.conv2(x, edge_index)
return x
The GCN class makes the inputs and two graph-convolution layers concrete. section 11.3.1
Use the graph structure only when it represents the relationships the task needs. Graphs can also be very large and irregular; a small visualization establishes the data model, not how an approach will behave at scale. section 11.3


