Projecting graph vertices into a vector space. Part 2. Markov chain and Word2Vec

Ilya Kotov7 Dec 2022

Problem

A social network wants to recommend friends, but checking every pair of users takes too long and costs too much compute. The friendship graph first has to become plain numeric vectors, so that similar users can be found by clustering. Part 1 of the series used matrix factorization for this. The second way uses random walks.

Example from the article: a high-school student builds a friend recommender for his school social network. A competitor from another school wants to match it.

Solution in brief

Treat a random walk over the graph as a "sentence" and the nodes as "words". Generate many walks from every node with a transition matrix, train Word2Vec on them, and cluster the resulting vectors with DBSCAN. A user gets recommended people from the same cluster who are not yet friends.

Questions this case answers
  • How to get dense vectors for the nodes of a graph when the standard embedding tools only accept sequences of tokens
  • How to shortlist candidate links between nodes cheaply, without scoring every pair
  • How to turn the vertices of a friendship graph into numeric vectors that can be clustered?
  • How to recommend new friends in a social network without comparing every pair of users?

Tools and methods

  • Transition matrix — the adjacency matrix with each row divided by its sum, giving the probability of moving from one node to another
  • Random walks — generate node sequences from the transition matrix
  • Word2Vec (Gensim) — turns the sequences into node vectors
  • DBSCAN (scikit-learn) — clusters the vectors
  • NumPy — matrix products and random.choice
Continue reading — it's free

Google or a one-time email code. No password.