liveBook · Manning

Node2Vec

As explained in Graph Algorithms for Data Science

node2vec is a graph representation-learning algorithm that generates node embeddings from biased random walks balancing breadth-first and depth-first exploration.

Node2Vec turns graph nodes into fixed-length vectors so their network positions can be used in machine-learning tasks. It is a transductive node-embedding method: embeddings describe nodes already present in the graph, so adding new nodes requires retraining. Its walks can be tuned to emphasize either nearby, similar nodes (homophily) or similar structural roles. section 9.3 section 9.5 section 9.1.1

The algorithm creates training data by starting random walks from every node. Each walk is treated as a sentence: visited nodes are its words. Those sentences are passed to a word2vec skip-gram model, which learns vector representations from nodes that occur near one another in walks. Walk length controls the sentence length. section 9.3.2 section 9.5

Unlike a first-order walk, Node2Vec uses second-order biased walks: the next move considers both the current node and the previous step. Edge weights can also affect traversal probability. This lets the walk behavior be tuned toward graph proximity or structural patterns rather than treating all neighboring moves equally. section 9.3.2 section 9.5

A practical Node2Vec workflow is:

  • Project the relevant graph, choosing relationship orientation and any relationship weight to retain. section 9.3.3
  • Run Node2Vec with an embedding dimension and walk-bias settings, then write each vector to a node property. section 9.3.3
  • Use known labels to train a classifier on the embeddings, then evaluate predictions with a classification report and confusion matrix. section 9.2 section 9.3.6
  • Compare configurations by their embedding distances and classification accuracy; also test the effect of removing edge weights. section 9.3.6

Predicting stream language from shared audiences

Represent streams as nodes and shared audience as weighted, undirected relationships. Because streams close in this network are likely to share a language, generate homophily-oriented Node2Vec embeddings, train a random forest on streams with known languages, and predict labels for new streams. An inOutFactor of 0.5 encourages more depth-first walks for this proximity-focused goal. section 9.2 section 9.3.3 section 9.3.6

Listing 9.7 Calculating node2vec embeddings and storing them to the database
data = run_query("""
CALL gds.beta.node2vec.write('twitch',
 {embeddingDimension:8, relationshipWeightProperty:'weight',
  inOutFactor:0.5, writeProperty:'node2vec'})
""")

This query writes eight-element Node2Vec vectors for the projected stream graph, uses relationship weights, and applies an inOutFactor of 0.5. section 9.3.3

How Graph Algorithms for Data Science covers it

How deep it goes
Explains random-walk training and implementation parameters, while deferring hyperparameter optimization.
Examples
Uses weighted shared-audience links among streams to predict streamer language with random forest classification.
What it assumes
Assumes an already constructed graph, a Jupyter environment, Neo4j access, and scikit-learn for model training.
What you can do after
Apply it via Neo4j Graph Data Science to write embeddings, train a classifier, and evaluate predictions.
How it connects
Connects node embeddings with random walks, word2vec skip-gram, homophily, structural roles, and node classification.

Also explained in

Compare how 3 books explain Node2Vec →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Algorithms for Data Science. The section links point to the parts of the book it draws on.