How to use the node2vec.Node2Vec function in node2vec

To help you get started, we’ve selected a few node2vec examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eliorc / node2vec / example.py View on Github external
import networkx as nx
from node2vec import Node2Vec

# FILES
EMBEDDING_FILENAME = './embeddings.emb'
EMBEDDING_MODEL_FILENAME = './embeddings.model'

# Create a graph
graph = nx.fast_gnp_random_graph(n=100, p=0.5)

# Precompute probabilities and generate walks
node2vec = Node2Vec(graph, dimensions=64, walk_length=30, num_walks=200, workers=4)

# Embed
model = node2vec.fit(window=10, min_count=1, batch_words=4)  # Any keywords acceptable by gensim.Word2Vec can be passed, `diemnsions` and `workers` are automatically passed (from the Node2Vec constructor)

# Look for most similar nodes
model.wv.most_similar('2')  # Output node names are always strings

# Save embeddings for later use
model.wv.save_word2vec_format(EMBEDDING_FILENAME)

# Save model for later use
model.save(EMBEDDING_MODEL_FILENAME)
github D2KLab / entity2rec / entity2rec / entity2vec.py View on Github external
from __future__ import print_function
import json
from os.path import isfile, join
from os import makedirs
from os import listdir
import argparse
from node2vec import Node2Vec
import time
import codecs
from sparql import Sparql
import shutil


class Entity2Vec(Node2Vec):

    """Generates a set of property-specific entity embeddings from a Knowledge Graph"""

    def __init__(self, is_directed, preprocessing, is_weighted, p, q, walk_length, num_walks, dimensions, window_size,
                 workers, iterations, feedback_file):

        Node2Vec.__init__(self, is_directed, preprocessing, is_weighted, p, q, walk_length, num_walks, dimensions,
                          window_size, workers, iterations)

        self.feedback_file = feedback_file

    def e2v_walks_learn(self, properties_names, dataset):

        n = self.num_walks

        p = int(self.p)
github D2KLab / entity2rec / entity2rec / node2vec_recommender.py View on Github external
import time
from gensim.models.keyedvectors import KeyedVectors
import numpy as np
from evaluator import Evaluator
from parse_args import parse_args
from sklearn.cluster import KMeans
from node2vec import Node2Vec
import os


class Node2VecRecommender(Node2Vec):

    def __init__(self, dataset, p=1, q=4, walk_length=100,
                 num_walks=50, dimensions=200, window_size=30, workers=8, iterations=5):

        Node2Vec.__init__(self, False, True, False, p, q, walk_length, num_walks, dimensions, window_size,
                          workers, iterations)

        self.dataset = dataset

        file = 'num%d_p%d_q%d_l%d_d%d_iter%d_winsize%d.emd' % (num_walks, p, q,
                                                               walk_length, dimensions,
                                                               iterations, window_size)

        self.path = 'datasets/%s/node2vec/' % self.dataset + file

        if file not in os.listdir('datasets/%s/node2vec/' % self.dataset):
github D2KLab / entity2rec / entity2rec / optimize_node2vec_hyper_params.py View on Github external
c_v = [10, 15]

scores = {}

for d in d_v:

    for p in p_v:

        for q in q_v:

            for c in c_v:

                # run node2vec to generate the embedding

                node2vec_graph = Node2Vec(args.directed, args.preprocessing, args.weighted, p, q,
                                          args.walk_length,
                                          args.num_walks, d, c, args.workers, args.iter)

                node2vec_graph.run('datasets/%s/graphs/feedback.edgelist' % args.dataset,
                                   'emb/%s/feedback/num%d_p%d_q%d_l%d_d%d_iter%d_winsize%d.emd'
                                   % (args.dataset, args.num_walks, p, q,
                                      args.walk_length, d, args.iter, c))

                # initialize entity2rec recommender
                node2vec_rec = Node2VecRecommender(args.dataset, p=p, q=q,
                                                   walk_length=args.walk_length, num_walks=args.num_walks,
                                                   dimensions=d, window_size=c,
                                                   iterations=args.iter)

                # initialize evaluator
github feixyz10 / network-representation-learning / source / node2vec_for_tencent.py View on Github external
import networkx as nx 
import numpy as np 
import random
from sklearn.metrics import roc_auc_score

from node2vec import Node2Vec

random.seed(616)
edges = np.load('../tencent/train_edges.npy')
G = nx.Graph()
for i in range(169209):
    G.add_node(i)
G.add_edges_from(edges)
        
node2vec = Node2Vec(G, emb_size=128, p=4, q=1, length_walk=50, num_walks=10, window_size=10, num_iters=2)
w2v = node2vec.train(workers=4, is_loadmodel=False, is_loaddata=True)

pos_test = np.load('../tencent/test_edges.npy')
neg_test = np.load('../tencent/test_edges_false.npy')

y_true = [True]*pos_test.shape[0] + [False]*neg_test.shape[0]
X = np.vstack([pos_test, neg_test])

print('Testing...')
y_score = []
for u, v in X:
    y_score.append(w2v.wv.similarity(str(u), str(v)))

auc_test = roc_auc_score(y_true, y_score)
print('Tencent, test AUC:', auc_test)