Code Architecture — node2vec Notebook
Overview
The notebook implements node2vec from scratch with NumPy (no node2vec or gensim package): second-order biased random walks with alias sampling, then Skip-gram with negative sampling. It runs on CPU on a small synthetic graph with three known communities and compares a BFS-like setting (p = 1, q = 2) with a DFS-like setting (p = 1, q = 0.5).
Section-by-Section Breakdown
1. Setup & Imports (cell 2)
- Imports
numpy,networkx,matplotlib, and from scikit-learnKMeans,TSNE,normalized_mutual_info_score,silhouette_score. - Saves figures as JPEG at dpi 80 to keep the notebook small, registers the
atexithook that saves the executed notebook on Kaggle, and setsSEED = 42forrandomandnumpy.
2. Graph Construction (cell 4)
build_synthetic_graph()makes 3 communities of 20 nodes. Each is a ring plus 15 random extra edges inside the community.- 4 bridge edges join the communities: (5, 25), (10, 45), (35, 55), (15, 38).
- The saved run prints 60 nodes and 103 edges and draws the graph coloured by community.
3. Biased Random Walks (cells 6–10)
compute_transition_probs(G, p, q)computes, for every ordered (previous, current) pair, the unnormalised weights 1/p (return to the previous node), 1 (neighbour of the previous node) and 1/q (move further away), then normalises them (Section 3.2.2).alias_setup/alias_drawbuild and sample alias tables so each step is O(1) (Section 3.2.3);build_alias_tablesbuilds one table per (previous, current) pair. The first step of each walk has no previous node, so it picks a neighbour uniformly withrandom.choice.- Cells 6 and 8 build the tables with p = 1, q = 0.5. The walk functions read only these tables, so the p and q used for the first embedding are the ones set in cell 6.
biased_random_walkandgenerate_walksrun 10 walks of length 10 from every node, 600 walks in total.
4. Skip-gram with Negative Sampling (cell 12)
skipgram_train(walks, vocab_size, dim=16, window_size=3, neg_samples=5, epochs=5, lr=0.025).- Two matrices: centre vectors
Wand context vectorsC, both initialised withrandn * 0.01. - For each (centre, context) pair inside the window: one positive update and 5 negatives drawn uniformly from all nodes. The learning rate is multiplied by 0.95 after each epoch. The loss is printed per epoch.
- Returns
Was the node embeddings.
5. Visualisation (cell 14)
visualize_embeddingsprojects the embeddings to 2-D with t-SNE, colours them by true community, runs K-Means with k = 3 and returns the NMI between clusters and true communities. The saved run prints NMI 1.0000 for the first embedding (p = 1, q = 0.5).
6. BFS-like vs DFS-like (cell 16)
run_node2vec(G, communities, p, q, ...)chains the steps above.- Called once with p = 1, q = 2 (BFS-like) and once with p = 1, q = 0.5 (DFS-like). The saved run prints NMI 1.0000 for both.
6.1 Repeat over 5 seeds (cell 18)
embed_quietly(G, p, q)runs the same pipeline without plots or epoch prints.- For seeds 0–4 and both settings it prints the NMI per seed, the mean NMI, and the silhouette score of the true communities in embedding space.
- Saved run: NMI 1.000 ± 0.000 for both settings; silhouette 0.361 ± 0.003 (q = 0.5) and 0.315 ± 0.004 (q = 2).
7. Graph with Learned Clusters (cell 20)
graph_with_clustersdraws the graph twice side by side: true communities, and K-Means clusters from the embeddings, with the NMI in the title.
8. Summary (cell 21)
- States what the saved run shows: both settings reach NMI 1.0 in the single runs and over 5 seeds, the silhouette difference is small but in the direction the paper describes, structural roles were not measured, and the graph is much smaller and easier than the paper’s datasets.
Differences from the Paper
| Item | Paper (Sections 4.2–4.4) | Notebook |
|---|---|---|
| Graphs | BlogCatalog, PPI, Wikipedia; link prediction on Facebook, PPI, arXiv | 60-node synthetic graph |
| Dimensions d | 128 | 16 |
| Walks per node r | 10 | 10 |
| Walk length l | 80 | 10 |
| Context size k | 10 | 3 |
| p, q | grid search over {0.25, 0.5, 1, 2, 4} | two fixed settings |
| Evaluation | Macro/Micro-F1 with a classifier, AUC for links | K-Means NMI and silhouette |