liveBook · Manning

Embedding

As explained in Graph Algorithms for Data Science

Node embedding is a technique that represents graph nodes as numerical vectors so that selected structural or relational similarities are preserved.

A node embedding represents each graph node as a numeric vector. The goal is to place nodes close together when the graph property that matters for an analysis is similar. A homophily-oriented embedding preserves neighborhood: connected nodes, often assumed to have similar labels, appear nearby. A structural-role embedding instead places nodes with similar roles—such as bridges between communities—near one another. section 9.1.1

Choose the meaning of closeness before choosing an algorithm. Homophily-based vectors suit tasks where linked entities are expected to share an attribute, such as predicting a stream’s language from its shared audience. Structural-role vectors suit role analysis in networks such as coauthorship or internet routing. FastRP is named as a homophily-oriented option, while RolX encodes structural roles. section 9.1.1 section 9.2

Node2vec creates embeddings by adapting word2vec’s skip-gram idea to graphs. Word2vec learns vectors by predicting nearby words; node2vec first generates graph “sentences” with random walks, then treats each visited node as a word. Walks start from every node, and walk length determines sentence size. In weighted graphs, larger relationship weights raise the chance that a walk traverses that relationship. section 9.3.1 section 9.3.2

Figure 9.10 Using random walks to produce sentences
09-10

A random walk turns a path through neighboring nodes into a sentence that can train the skip-gram model. section 9.3.2

A practical embedding workflow is:

  • Project the graph needed for the question, including direction and any relationship weight. section 9.3.3
  • Run node2vec and select an embedding dimension; an eight-element vector is used for stream nodes. section 9.3.3
  • Set walk behavior to match the objective. An inOutFactor of 0.5 encourages depth-first walks for homophily-based embeddings. section 9.3.3
  • Store vectors on nodes, then use known labels to train and evaluate a classifier. section 9.2

Evaluate whether the vectors express the intended proximity rather than treating them as opaque output. For connected node pairs, compare Euclidean distance and cosine similarity: lower Euclidean distance means closer vectors, while cosine values near 1 mean high similarity. Inspect how similarity varies with node degree and relationship weight; nodes with more connections can be less similar to their individual neighbors. section 9.3.4

Predicting stream language

Build an undirected, weighted graph in which streams are connected by shared audience. Calculate node2vec vectors, using relationship weight and an inOutFactor of 0.5, then train a random forest on streams with known languages. Evaluate predictions with a classification report and confusion matrix before using the result for new streams. section 9.2 section 9.3.3

Embeddings can also represent relationships. TransE assigns low-dimensional vectors to nodes and relationships in a knowledge graph, aiming for a head vector plus a relation vector to lie close to the tail vector for observed triples. This supports the intuition behind knowledge-graph completion, while its mathematical implementation is deferred. section 11.1.2

How Graph Algorithms for Data Science covers it

How deep it goes
Explains node2vec through skip-gram, random walks, configuration, and similarity checks, while deferring hyperparameter optimization.
Examples
Uses friendship interests, coauthorship bridges, routers, and weighted shared-audience stream graphs.
What it assumes
Starts from graph neighborhoods and introduces neural-network context through word2vec without requiring prior neural-network work.
What you can do after
Apply it via Neo4j Graph Data Science node2vec and evaluate vectors before training a stream-language classifier.
How it connects
Connects embeddings to homophily, structural roles, node2vec, word2vec, random walks, node classification, and TransE.

Also explained in

Compare how 47 books explain embedding →

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.