要对不同节点配置不同的pq值,需要sampling_strategy这个参数配合。通过dict_my_strategy传入一个字典即可。
import networkx as nx
import matplotlib.pyplot as plt
from node2vec import Node2Vec
G = nx.les_miserables_graph()
# nx.draw_networkx(G, with_labels=True) # draw the graph, simplest way.
# nx.draw_spring(G, with_labels = True)
# plt.show()
dict_my_strategy = {'Myriel':{'p':10, 'q':0.1}} # use this dict to regulate the p,q for specific nodes.
print(' walking...')
node2vec = Node2Vec(G, dimensions=16, walk_length=20, num_walks=10, p=1, q=1, workers=1, sampling_strategy=dict_my_strategy)
# Embed nodes
print(' training...')
model = node2vec.fit(window=4, min_count=1)