-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRW.py
150 lines (104 loc) · 4.37 KB
/
RW.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import networkx as nx
import numpy as np
from numpy.linalg import inv
def kernel_formula(G):
print ('Finding kernel...')
#Parameters
stop_prob = 0.1 # Stopping probability for each node
r0 = stop_prob * stop_prob
nodes = G.nodes()
edges = G.edges()
shape = (len(nodes), len(nodes))
shapeT = (len(nodes) * len(nodes), len(nodes) * len(nodes))
shapeR = (len(nodes)*len(nodes), 1)
R0 = np.array([len(set.intersection(set(G.neighbors(x)), set(G.neighbors(y)))) / len(set.union(set(G.neighbors(x)), set(G.neighbors(y)))) for x in nodes for y in nodes])
R0.shape = shape
Pt = np.array([int(G.has_edge(x1, x)) * (1 - stop_prob) / len(G.neighbors(x)) for x in nodes for x1 in nodes])
Pt.shape = shape
r1 = np.array([sum([Pt[x1, x] * R0[x1, y1] * Pt[y1, y] for x1 in nodes for y1 in nodes]) for x in nodes for y in nodes])
r1.shape = shape
T = np.array([Pt[x1, x] * R0[x1, z1] * Pt[z1, z] for x in nodes for z in nodes for x1 in nodes for z1 in nodes])
T.shape = shapeT
identity = np.eye(len(nodes) * len(nodes))
H = identity - T
r1.shape = shapeR
K = np.matmul(inv(H),r1)
#uncomment the following lines of code if you prefer an approximation over an exact solution
'''
factor1 = np.matmul(T, r1)
#factor2 = np.matmul(T, factor1)
#factor3 = np.matmul(T, factor2)
K = r1 + factor1
'''
K.shape = shape
return K
def IRWK(G):
print ('Finding kernel by random walk...')
#Parameters
stop_prob = 0.1 # Stopping probability for each node
trans_prob = 0.01 # Transition probability
r0 = stop_prob * stop_prob
nodes = G.nodes()
edges = G.edges()
shape = (len(nodes), len(nodes))
shapeT = (len(nodes) * len(nodes), len(nodes) * len(nodes))
shapeR = (len(nodes)*len(nodes), 1)
R0 = np.array([len(set.intersection(set(G.neighbors(x)), set(G.neighbors(y)))) / len(set.union(set(G.neighbors(x)), set(G.neighbors(y)))) for x in nodes for y in nodes])
R0.shape = shape
Pt = np.array([int(G.has_edge(x1, x)) * (1 - stop_prob) / len(G.neighbors(x)) for x in nodes for x1 in nodes])
Pt.shape = shape
r1 = np.array([sum([Pt[x1, x] * R0[x1, y1] * Pt[y1, y] for x1 in nodes for y1 in nodes]) for x in nodes for y in nodes])
r1.shape = shape
T = np.array([Pt[x1, x] * R0[x1, z1] * Pt[z1, z] for x in nodes for z in nodes for x1 in nodes for z1 in nodes])
T.shape = shapeT
identity = np.eye(len(nodes) * len(nodes))
H = identity - T
R = R0
R.shape = shapeR
r1.shape = shapeR
for i in range(10):
R = r1 + np.matmul(T, R)
K = R
K.shape = shape
return K
def multiview_IRWK(Garr):
#input an array of graphs
print ('Finding kernel by random walk...')
#Parameters
stop_prob = 0.1 # Stopping probability for each node
trans_prob = 0.01 # Transition probability
r0 = stop_prob * stop_prob
views = len(Garr)
nodes = []
for G in Garr:
nodes.append(G.nodes())
shape = (len(nodes[0]), len(nodes[0]))
shapeT = (len(nodes[0]) * len(nodes[0]), len(nodes[0]) * len(nodes[0]))
shapeR = (len(nodes[0])*len(nodes[0]), 1)
R0, Pt, r1, T = [None]*views, [None]*views, [None]*views, [None]*views
for i in range(views):
R0[i] = np.array([(len(set.intersection(set(Garr[i].neighbors(x)), set(Garr[i].neighbors(y))))+1) / (len(set.union(set(Garr[i].neighbors(x)), set(Garr[i].neighbors(y))))+1) for x in nodes[i] for y in nodes[i]])
R0[i].shape = shape
Pt[i] = np.array([((int(Garr[i].has_edge(x1, x)) * (1 - stop_prob - trans_prob * (views - 1)))+1) / (len(Garr[i].neighbors(x))+1) for x in nodes[i] for x1 in nodes[i]])
Pt[i].shape = shape
r1[i] = np.array([sum([Pt[i][x1, x] * R0[i][x1, y1] * Pt[i][y1, y] for x1 in range(len(nodes[i])) for y1 in range(len(nodes[i]))]) for x in range(len(nodes[i])) for y in range(len(nodes[i]))])
r1[i].shape = shape
T[i] = np.array([Pt[i][x1, x] * R0[i][x1, z1] * Pt[i][z1, z] for x in range(len(nodes[i])) for z in range(len(nodes[i])) for x1 in range(len(nodes[i])) for z1 in range(len(nodes[i]))])
T[i].shape = shapeT
R = [None]*views
for j in range(views):
R[i].shape = shapeR
r1[i].shape = shapeR
for i in range(10):
for k in range(views):
restM = None
restM.shape = shapeR
for j in range(views):
if k!=j:
restM+=R[j]
R[k] = r1[k] + np.matmul(T[k], R[k]) + trans_prob*restM
K = [None]*views
for i in range(views):
K[i] = R[i]
K[i].shape = shape
return K