Hands on notebook from the Graph Machine Learning short course taught at the Semana da Computação 2025 (Computing Week) at the Federal University of Rondônia (UNIR).
Given a social network, can we guess which people are likely to become friends? This is the link prediction problem, one of the classic tasks in graph machine learning.
The notebook walks through a complete pipeline on real Facebook friendship data:
- Load the friendship lists and build a graph with NetworkX
- Create a balanced dataset of existing links (positives) and missing links (negatives)
- Learn a vector representation for every user with Node2Vec
- Turn each pair of users into a feature vector by combining their embeddings
- Train a Random Forest to tell real links from fake ones
- Evaluate the model with ROC AUC and explore its predictions interactively
- How to represent a social network as a graph and inspect it (nodes, edges, degrees)
- Why link prediction is framed as binary classification over node pairs
- How negative sampling creates the "no link" examples the model needs
- How Node2Vec uses biased random walks and Word2Vec to embed nodes in a vector space
- How the Hadamard product (element wise multiplication) turns two node embeddings into one edge feature
- How to evaluate a link predictor with ROC AUC and read its predicted probabilities
- How to visualize embeddings with PCA and explore predictions with interactive widgets
facebook/*.edges → NetworkX graph → positive + negative pairs → train / test split (80 / 20)
NetworkX graph → Node2Vec (32 dims) → Hadamard edge features → Random Forest → ROC AUC
| Step | What happens | Key settings |
|---|---|---|
| Graph | Edge lists are merged into an undirected graph | nx.from_pandas_edgelist |
| Sampling | One negative pair is drawn for every existing edge | nx.non_edges, shuffled |
| Split | Pairs are split into train and test sets | test_size=0.2, random_state=42 |
| Embeddings | Random walks are fed to a Word2Vec model | dimensions=32, walk_length=5, num_walks=10, window=3 |
| Features | Embeddings of both endpoints are multiplied | wv[u] * wv[v] |
| Classifier | Random Forest on the edge features | n_estimators=100 |
| Metric | Area under the ROC curve on the test set | roc_auc_score |
The model reaches a test ROC AUC of about 0.99 in the recorded run. The notebook also prints the predicted probability for 30 test pairs next to the expected answer, so participants can see where the model gets it right and where it fails.
The last cell opens an interactive explorer: pick a user from a dropdown, set a probability threshold and the number of suggestions, and the notebook draws that user's real friends (blue) together with the top predicted new friends (green, dashed edges). It works like a small "people you may know" feature.
The facebook/ folder contains the ego-Facebook dataset from the
Stanford Network Analysis Project (SNAP). It is made of 10 ego
networks, each one centered on a survey participant, with anonymized user IDs.
| Extension | Content |
|---|---|
.edges |
Friendships between the ego's friends, one pair of IDs per line |
.circles |
Friend circles (lists) defined by the ego |
.feat / .egofeat |
Anonymized binary profile features of each friend and of the ego |
.featnames |
Names of the feature dimensions |
The notebook uses the graph structure only (the .edges files). Profile features and circles are left for
experimentation.
This is how the course was run.
- Click the Open in Colab badge above
- Upload the
facebookfolder to/content/in the Colab file panel, so the files end up at/content/facebook/ - Run the cells from top to bottom
On a standard Colab machine, the Node2Vec step takes around 2 minutes and training the Random Forest takes around 10 minutes (the timings are noted in the notebook). The interactive explorer takes about a minute per user.
git clone https://github.com/Wyllgner/Graph_Machine_Learning_SDC25.git
cd Graph_Machine_Learning_SDC25
pip install node2vec networkx pandas numpy scikit-learn matplotlib ipywidgets tqdm jupyter
jupyter notebookBefore running, make two small changes to the notebook:
- In step 1, point
folder_pathto the local folder:folder_path = "facebook" - In the imports cell, remove
matplotlib.use('Agg')so the plots show up inside Jupyter
The notebook is kept exactly as it was presented in the course. Anyone reusing it should know about two details:
- Duplicated node IDs. The loader also reads the
.circlesfiles. Those rows are dropped as empty, but along the way pandas turns thetargetcolumn into floats, so the same user appears as"650"and as"650.0". This is why the graph shows 7,918 nodes and 168,486 edges, exactly twice the real 3,959 nodes and 84,243 edges of the.edgesfiles. Reading only files ending in.edgesand loading IDs as strings (dtype=str) fixes it. - Optimistic score. Node2Vec is trained on the full graph, including the links that are later used as test positives, so the test AUC is higher than it would be on truly unseen links. A stricter setup removes the test edges from the graph before computing the embeddings.
Both points make good follow up exercises after going through the course.
| Path | Description |
|---|---|
Curso_GML_2025 (1).ipynb |
Course notebook (comments and outputs in Portuguese) |
facebook/ |
SNAP ego-Facebook dataset |
README.md |
This file |
- Course taught by Wyllgner França at the Semana da Computação 2025, UNIR
- Dataset: J. McAuley and J. Leskovec. Learning to Discover Social Circles in Ego Networks. NIPS, 2012
- Node2Vec: A. Grover and J. Leskovec. node2vec: Scalable Feature Learning for Networks. KDD, 2016