-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepwalk.py
135 lines (75 loc) · 2.75 KB
/
deepwalk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import random
import networkx as nx
from gensim.models import Word2Vec
class DeepWalk:
"""
Implement DeepWalk algorithm.
reference paper : DeepWalk: Online Learning of Social Representations
link : https://arxiv.org/abs/1403.6652
Using the algorithm can get graph embedding model with your network data.
"""
def __init__(self, G=None, adjlist_path=None, edgelist_path=None):
"""
Parameters
G : networkx : networkx graph.
adjlist_path : network file path.
"""
if G == adjlist_path == edgelist_path == None:
raise ValueError('all parameter is None, please check your input.')
try:
if G != None:
self.G = G
elif adjlist_path != None:
self.G = nx.read_adjlist(adjlist_path)
elif edgelist_path != None:
self.G = nx.read_edgelist(edgelist_path)
except Exception as e:
print(e)
def random_walk(self, iterations, start_node=None, random_walk_times=5):
"""
: Implement of random walk algorithm :
Parameters
----------------------------------------
iterations : int : random walk number of iteration
start_node : str : choose start node (random choose a node, if start_node is None)
random_walk_times : int : random walk times.
----------------------------------------
Returns
walk_records : list of walks record
"""
walk_records = []
for i in range(iterations):
if start_node is None:
s_node = random.choice(list(self.G.nodes()))
walk_path = [s_node]
else:
walk_path = [start_node]
current_node = s_node
while(len(walk_path) < random_walk_times):
neighbors = list(self.G.neighbors(current_node))
current_node = random.choice(neighbors)
walk_path.append(current_node)
walk_records.append(walk_path)
return walk_records
def buildWord2Vec(self, **kwargs):
"""
Using gensim to build word2vec model
Parameters
----------------------------------------
**kwargs
walk_path : list : random walk results
size : int : specific embedding dimension, default : 100 dim
window : int : specific learn context window size, default : 5
workers : int : specific workers. default : 2
----------------------------------------
Returns
walk_records : list of walks record
"""
walk_path = kwargs.get('walk_path', None)
if walk_path is None:
return
size = kwargs.get('size', 100)
window = kwargs.get('window', 5)
workers = kwargs.get('workers', 2)
embedding_model = Word2Vec(walk_path, size=size, window=window, min_count=0, workers=workers, sg=1, hs=1)
return embedding_model