Execute this notebook: Download locally
Node classification with Node2Vec¶
Introduction¶
An example of node classification on a homogeneous graph using the Node2Vec
representation learning algorithm. The example uses components from the stellargraph
, Gensim
, and scikit-learn
libraries.
Note: For clarity of exposition, this notebook forgoes the use of standard machine learning practices such as Node2Vec
parameter tuning, node feature standarization, data splitting that handles class imbalance, classifier selection, and classifier tuning to maximize predictive accuracy. We leave such improvements to the reader.
References¶
[1] Node2Vec: Scalable Feature Learning for Networks. A. Grover, J. Leskovec. ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD), 2016. (link)
[2] Distributed representations of words and phrases and their compositionality. T. Mikolov, I. Sutskever, K. Chen, G. S. Corrado, and J. Dean. In Advances in Neural Information Processing Systems (NIPS), pp. 3111-3119, 2013. (link)
[3] Gensim: Topic modelling for humans. (link)
[4] scikit-learn: Machine Learning in Python (link)
[3]:
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegressionCV
from sklearn.metrics import accuracy_score
import os
import networkx as nx
import numpy as np
import pandas as pd
from stellargraph.data import BiasedRandomWalk
from stellargraph import StellarGraph
from stellargraph import datasets
from IPython.display import display, HTML
%matplotlib inline
Dataset¶
For clarity, we use only the largest connected component, ignoring isolated nodes and subgraphs; having these in the data does not prevent the algorithm from running and producing valid results.
(See the “Loading from Pandas” demo for details on how data can be loaded.)
[4]:
dataset = datasets.Cora()
display(HTML(dataset.description))
G, node_subjects = dataset.load(largest_connected_component_only=True)
[5]:
print(G.info())
StellarGraph: Undirected multigraph
Nodes: 2485, Edges: 5209
Node types:
paper: [2485]
Edge types: paper-cites->paper
Edge types:
paper-cites->paper: [5209]
The Node2Vec algorithm¶
The Node2Vec algorithm introduced in [1] is a 2-step representation learning algorithm. The two steps are,
Use 2nd order random walks to generate sentences from a graph. A sentence is a list of node ids. The set of all sentences makes a corpus.
The corpus is then used to learn an embedding vector for each node in the graph. Each node id is considered a unique word/token in a dictionary that has size equal to the number of nodes in the graph. The Word2Vec algorithm [2], is used for calculating the embedding vectors.
Corpus generation using random walks¶
The stellargraph library provides an implementation for 2nd order random walks as required by Node2Vec. The random walks have fixed maximum length and are controlled by two parameters p
and q
. See [1] for a detailed description of these parameters.
We are going to start 10 random walks from each node in the graph with a length up to 100. We set parameter p
to 0.5 and q
to 2.0.
[6]:
rw = BiasedRandomWalk(G)
walks = rw.run(
nodes=list(G.nodes()), # root nodes
length=100, # maximum length of a random walk
n=10, # number of random walks per root node
p=0.5, # Defines (unormalised) probability, 1/p, of returning to source node
q=2.0, # Defines (unormalised) probability, 1/q, for moving away from source node
)
print("Number of random walks: {}".format(len(walks)))
Number of random walks: 24850
Representation Learning using Word2Vec¶
We use the Word2Vec
[2], implementation in the free Python library Gensim
[3], to learn representations for each node in the graph. It works with str
tokens, but the graph has integer IDs, so they’re converted to str
here.
We set the dimensionality of the learned embedding vectors to 128 as in [1].
[7]:
from gensim.models import Word2Vec
str_walks = [[str(n) for n in walk] for walk in walks]
model = Word2Vec(str_walks, size=128, window=5, min_count=0, sg=1, workers=2, iter=1)
[8]:
# The embedding vectors can be retrieved from model.wv using the node ID as key.
model.wv["19231"].shape
[8]:
(128,)
Visualise Node Embeddings¶
We retrieve the Word2Vec
node embeddings that are 128-dimensional vectors and then we project them down to 2 dimensions using the t-SNE algorithm.
[9]:
# Retrieve node embeddings and corresponding subjects
node_ids = model.wv.index2word # list of node IDs
node_embeddings = (
model.wv.vectors
) # numpy.ndarray of size number of nodes times embeddings dimensionality
node_targets = node_subjects[[int(node_id) for node_id in node_ids]]
[10]:
# Apply t-SNE transformation on node embeddings
tsne = TSNE(n_components=2)
node_embeddings_2d = tsne.fit_transform(node_embeddings)
[11]:
# draw the points
alpha = 0.7
label_map = {l: i for i, l in enumerate(np.unique(node_targets))}
node_colours = [label_map[target] for target in node_targets]
plt.figure(figsize=(10, 8))
plt.scatter(
node_embeddings_2d[:, 0],
node_embeddings_2d[:, 1],
c=node_colours,
cmap="jet",
alpha=alpha,
)
[11]:
<matplotlib.collections.PathCollection at 0x15b689710>
Downstream task¶
The node embeddings calculated using Word2Vec
can be used as feature vectors in a downstream task such as node attribute inference.
In this example, we will use the Node2Vec
node embeddings to train a classifier to predict the subject of a paper in Cora.
[12]:
# X will hold the 128-dimensional input features
X = node_embeddings
# y holds the corresponding target values
y = np.array(node_targets)
Data Splitting¶
We split the data into train and test sets.
We use 75% of the data for training and the remaining 25% for testing as a hold out test set.
[13]:
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.1, test_size=None)
[14]:
print(
"Array shapes:\n X_train = {}\n y_train = {}\n X_test = {}\n y_test = {}".format(
X_train.shape, y_train.shape, X_test.shape, y_test.shape
)
)
Array shapes:
X_train = (248, 128)
y_train = (248,)
X_test = (2237, 128)
y_test = (2237,)
Classifier Training¶
We train a Logistic Regression classifier on the training data.
[15]:
clf = LogisticRegressionCV(
Cs=10, cv=10, scoring="accuracy", verbose=False, multi_class="ovr", max_iter=300
)
clf.fit(X_train, y_train)
[15]:
LogisticRegressionCV(Cs=10, class_weight=None, cv=10, dual=False,
fit_intercept=True, intercept_scaling=1.0, l1_ratios=None,
max_iter=300, multi_class='ovr', n_jobs=None, penalty='l2',
random_state=None, refit=True, scoring='accuracy',
solver='lbfgs', tol=0.0001, verbose=False)
Predict the hold out test set.
[16]:
y_pred = clf.predict(X_test)
Calculate the accuracy of the classifier on the test set.
[17]:
accuracy_score(y_test, y_pred)
[17]:
0.7223960661600357
Execute this notebook: Download locally