-
Notifications
You must be signed in to change notification settings - Fork 47
/
neighbour.py
314 lines (257 loc) · 9.93 KB
/
neighbour.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import math
from ..evaluation import Scoresheet
from ..util import all_pairs
from .base import Predictor
from .util import (
neighbourhood,
neighbourhood_size,
neighbourhood_intersection_size,
neighbourhood_union_size,
)
__all__ = [
"AdamicAdar",
"AssociationStrength",
"CommonNeighbours",
"Cosine",
"DegreeProduct",
"Jaccard",
"MaxOverlap",
"MinOverlap",
"NMeasure",
"Pearson",
"ResourceAllocation",
]
class AdamicAdar(Predictor):
def predict(self, weight=None):
"""Predict by Adamic/Adar measure of neighbours
Parameters
----------
weight : None or string, optional
If None, all edge weights are considered equal.
Otherwise holds the name of the edge attribute used as weight.
"""
res = Scoresheet()
for a, b in self.likely_pairs():
intersection = set(neighbourhood(self.G, a)) & set(neighbourhood(self.G, b))
w = 0
for c in intersection:
if weight is not None:
numerator = self.G[a][c][weight] * self.G[b][c][weight]
else:
numerator = 1
w += numerator / math.log(neighbourhood_size(self.G, c, weight))
if w > 0:
res[(a, b)] = w
return res
class AssociationStrength(Predictor):
def predict(self, weight=None):
"""Predict by association strength of neighbours
Parameters
----------
weight : None or string, optional
If None, all edge weights are considered equal.
Otherwise holds the name of the edge attribute used as weight.
"""
res = Scoresheet()
for a, b in self.likely_pairs():
w = neighbourhood_intersection_size(self.G, a, b, weight) / (
neighbourhood_size(self.G, a, weight)
* neighbourhood_size(self.G, b, weight)
)
if w > 0:
res[(a, b)] = w
return res
class CommonNeighbours(Predictor):
def predict(self, alpha=1.0, weight=None):
r"""Predict using common neighbours
This is loosely based on Opsahl et al. (2010):
.. math ::
k(u, v) = |N(u) \cap N(v)|
s(u, v) = \sum_{i=1}^n x_i \cdot y_i
W(u, v) = k(u, v)^{1 - \alpha} \cdot s(u, v)^{\alpha}
Parameters
----------
alpha : float, optional
If alpha = 0, weights are ignored. If alpha = 1, only weights are
used (ignoring the number of intermediary nodes).
weight : None or string, optional
If None, all edge weights are considered equal.
Otherwise holds the name of the edge attribute used as weight.
"""
res = Scoresheet()
for a, b in self.likely_pairs():
if weight is None or alpha == 0.0:
w = neighbourhood_intersection_size(self.G, a, b, weight=None)
elif alpha == 1.0:
w = neighbourhood_intersection_size(self.G, a, b, weight=weight)
else:
k = neighbourhood_intersection_size(self.G, a, b, weight=None)
s = neighbourhood_intersection_size(self.G, a, b, weight=weight)
w = (k ** (1.0 - alpha)) * (s ** alpha)
if w > 0:
res[(a, b)] = w
return res
class Cosine(Predictor):
def predict(self, weight=None):
"""Predict by cosine measure of neighbours
Parameters
----------
weight : None or string, optional
If None, all edge weights are considered equal.
Otherwise holds the name of the edge attribute used as weight.
"""
res = Scoresheet()
for a, b in self.likely_pairs():
w = neighbourhood_intersection_size(self.G, a, b, weight) / math.sqrt(
neighbourhood_size(self.G, a, weight)
* neighbourhood_size(self.G, b, weight)
)
if w > 0:
res[(a, b)] = w
return res
class DegreeProduct(Predictor):
def predict(self, weight=None, minimum=1):
"""Predict by degree product (preferential attachment)
Parameters
----------
weight : None or string, optional
If None, all edge weights are considered equal.
Otherwise holds the name of the edge attribute used as weight.
minimum : int, optional (default = 1)
If the degree product is below this minimum, the corresponding
prediction is ignored.
"""
res = Scoresheet()
for a, b in all_pairs(self.eligible_nodes()):
w = neighbourhood_size(self.G, a, weight) * neighbourhood_size(
self.G, b, weight
)
if w >= minimum:
res[(a, b)] = w
return res
class Jaccard(Predictor):
def predict(self, weight=None):
"""Predict by Jaccard index of neighbours
Parameters
----------
weight : None or string, optional
If None, all edge weights are considered equal.
Otherwise holds the name of the edge attribute used as weight.
"""
res = Scoresheet()
for a, b in self.likely_pairs():
# Best performance: weighted numerator, unweighted denominator.
numerator = neighbourhood_intersection_size(self.G, a, b, weight)
denominator = neighbourhood_union_size(self.G, a, b, weight)
w = numerator / denominator
if w > 0:
res[(a, b)] = w
return res
class NMeasure(Predictor):
def predict(self, weight=None):
"""Predict by N measure of neighbours
The N measure was defined by Egghe (2009).
Parameters
----------
weight : None or string, optional
If None, all edge weights are considered equal.
Otherwise holds the name of the edge attribute used as weight.
"""
res = Scoresheet()
for a, b in self.likely_pairs():
w = (
math.sqrt(2)
* neighbourhood_intersection_size(self.G, a, b, weight)
/ math.sqrt(
neighbourhood_size(self.G, a, weight) ** 2
+ neighbourhood_size(self.G, b, weight) ** 2
)
)
if w > 0:
res[(a, b)] = w
return res
def _predict_overlap(predictor, function, weight=None):
res = Scoresheet()
for a, b in predictor.likely_pairs():
# Best performance: weighted numerator, unweighted denominator.
numerator = neighbourhood_intersection_size(predictor.G, a, b, weight)
denominator = function(
neighbourhood_size(predictor.G, a, weight),
neighbourhood_size(predictor.G, b, weight),
)
w = numerator / denominator
if w > 0:
res[(a, b)] = w
return res
class MaxOverlap(Predictor):
def predict(self, weight=None):
"""Predict by maximum overlap between neighbours
Parameters
----------
weight : None or string, optional
If None, all edge weights are considered equal.
Otherwise holds the name of the edge attribute used as weight.
"""
return _predict_overlap(self, max, weight)
class MinOverlap(Predictor):
def predict(self, weight=None):
"""Predict by minimum overlap between neighbours
Parameters
----------
weight : None or string, optional
If None, all edge weights are considered equal.
Otherwise holds the name of the edge attribute used as weight.
"""
return _predict_overlap(self, min, weight)
class Pearson(Predictor):
def predict(self, weight=None):
"""Predict by Pearson correlation between neighbours
Parameters
----------
weight : None or string, optional
If None, all edge weights are considered equal.
Otherwise holds the name of the edge attribute used as weight.
"""
res = Scoresheet()
# 'Full' Pearson looks at all possible pairs. Since those are likely
# of little value for link prediction, we restrict ourselves to pairs
# with at least one common neighbour.
for a, b in self.likely_pairs():
n = len(self.G)
a_l2norm = neighbourhood_size(self.G, a, weight)
b_l2norm = neighbourhood_size(self.G, b, weight)
a_l1norm = neighbourhood_size(self.G, a, weight, pow=1)
b_l1norm = neighbourhood_size(self.G, b, weight, pow=1)
intersect = neighbourhood_intersection_size(self.G, a, b, weight)
numerator = (n * intersect) - (a_l1norm * b_l1norm)
denominator = math.sqrt(n * a_l2norm - a_l1norm ** 2) * math.sqrt(
n * b_l2norm - b_l1norm ** 2
)
w = numerator / denominator
if w > 0:
res[(a, b)] = w
return res
class ResourceAllocation(Predictor):
def predict(self, weight=None):
"""Predict with resource allocation index of neighbours
Resource allocation was defined by Zhou, Lu & Zhang (2009, Eur. Phys.
J. B, 71, 623).
Parameters
----------
weight : None or string, optional
If None, all edge weights are considered equal.
Otherwise holds the name of the edge attribute used as weight.
"""
res = Scoresheet()
for a, b in self.likely_pairs():
intersection = set(neighbourhood(self.G, a)) & set(neighbourhood(self.G, b))
w = 0
for c in intersection:
if weight is not None:
numerator = float(self.G[a][c][weight] * self.G[b][c][weight])
else:
numerator = 1
w += numerator / neighbourhood_size(self.G, c, weight)
if w > 0:
res[(a, b)] = w
return res