Node2Vec implementation in Julia with LightGraphs
Author ollin18
9 Stars
Updated Last
2 Years Ago
Started In
December 2018

Build Status codecov



] add

This is a Julia implementation of Node2Vec with SimpleWeightedGraphs which is built upon LightGraphs.

Node2Vec is an algorithm for network feature learning via biased random walks which we could describe as phrases with each visited node being a word. With that analogy we can easily see that the following step is to perform a Word2Vec Skip-gram approach to embed our nodes in a vector space. Is also to be noted that Node2Vec is a generalization of DeepWalk setting p=q=1.

The implementation is stable but I'm still refactoring and writing documentation. It needs the LightGraphs and SimpleWeightedGraphs packages to get the weighted network structure and it is very simple as we only have three functions, node2vec_walk, simulate_walks, learn_embeddings.


First we read the data

using TSne, Plots, Colors, GraphPlot, LightGraphs, SimpleWeightedGraphs,DelimitedFiles, StatsBase, Node2Vec

tred = readdlm("data/networks/adyacencias.csv",'|')
Nodes = readdlm("data/networks/los_nombres.csv",',')
sen = readdlm("data/networks/senators.csv",'|')
partidos = readdlm("data/networks/los_partidos.csv",'|')

Then we can create our network; build a dictionary with the node names and add the edges.

dic_nodes = Dict{String,Int64}(Dict(Nodes[i]=>i for i in eachindex(Nodes)))
g = SimpleWeightedGraph()
G = Graph()
last_node = lastindex(Nodes)
add_vertices!(g, last_node)
add_vertices!(G, last_node)
for n in 1:Int64(size(tred)[1])

Now the Node2Vec algorithm. First the walks

# simulate_walks(network,num,length,p,q)
walks = simulate_walks(g,5,80,2,2)

It is possible to create your own simulated walks with node2vec_walk(network,node,length,p,q), the function simulate_walks randomly shuffles the initial node to avoid the first-mover advantage.

Then we do the Word2Vec step through Word2Vec.jl. It strangely takes the character "" as a word so we cut it.

model = learn_embeddings(walks)
vectors = model.vectors

You can specify embedding vector length with size keyword argument in the learn_embeddings function.

In order to plot the embedded network we need to perform a dimensionality reduction (working on the parameters)

vectors = model.vectors
senators = vectors[:,2:size(vectors)[2]]
dv = tsne(senators')

The current network is a representation of the Mexican Senate so we want to color each node projection by their party affiliation.

for j in 1:128
   for i in 1:128
       if sen[i,2]==Nodes[j]

the_nodes = model.vocab[2:end]
nodes_num = map(x->parse(Int64,x),the_nodes)
ord_party = Vector{Any}(undef,128)
for i in eachindex(nodes_num)

for i in eachindex(ord_party)
    if ord_party[i] == "PRI"
        ord_party[i] = 1
    elseif ord_party[i] == "PAN"
        ord_party[i] = 2
    elseif ord_party[i] == "PRD"
        ord_party[i] = 3
    elseif ord_party[i] == "PVEM"
        ord_party[i] = 4
    elseif ord_party[i] == "PT"
        ord_party[i] = 5
    elseif ord_party[i] == "Independiente"
        ord_party[i] = 6
nodecolor = [colorant"red",colorant"blue",colorant"yellow",colorant"green",colorant"orange",colorant"violet"]
nodefillc =  nodecolor[ord_party]

Now we can finally plot the projection

title!("Mexican Senate - Node2Vec proj with p=2 q=2")

And compare to the original network (with an unweighted LightGraphs network)



If you find node2vec useful in your research, we ask that you cite the original paper:

    author = {Grover, Aditya and Leskovec, Jure},
    title = {Node2Vec: Scalable Feature Learning for Networks},
    booktitle = {Proceedings of the 22Nd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining},
    series = {KDD '16},
    year = {2016},
    isbn = {978-1-4503-4232-2},
    location = {San Francisco, California, USA},
    pages = {855--864},
    numpages = {10},
    url = {},
    doi = {10.1145/2939672.2939754},
    acmid = {2939754},
    publisher = {ACM},
    address = {New York, NY, USA},
    keywords = {feature learning, graph representations, information networks, node embeddings},

Used By Packages

No packages found.