Implementing Node2Vec
Now that we have the functions to generate biased random walks, the implementation of Node2Vec is very similar to implementing DeepWalk. It is so similar that we can reuse the same code and create sequences with and to implement DeepWalk as a special case of Node2Vec. Let’s reuse Zachary’s Karate Club for this task:
As in the previous chapter, our goal is to correctly classify each member of the club as part of one of the two groups (“Mr. Hi” and “Officer”). We will use the node embeddings provided by Node2Vec as input to a machine learning classifier (Random Forest in this case).
Let’s see how to implement it step by step:
- First, we want to install the
gensim
library to use Word2Vec. This time, we will use version 3.8.0 for compatibility reasons:!pip install -qI gensim==3.8.0
- We import the required libraries:
from gensim.models.word2vec import Word2Vec from sklearn.ensemble import RandomForestClassifier...