-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.py
65 lines (54 loc) · 1.75 KB
/
graph.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
# -*- coding: utf-8 -*-
from eden.graph import Vectorizer
from sklearn.metrics import pairwise
from scipy.sparse import vstack
class CDNK_Vectorizer():
def __init__(self,
L=None,
n_nodes=None,
d=2,
r=1,
discrete=True,
nbits=20,
n_jobs=1):
""" Constructor
Parameters:
- max_deg:
- cli_threshold:
- r: r
- d: distance
- nbits:
- n_jobs:
"""
self.L = L
self.n_nodes = n_nodes
self.d = d
self.r = r
self.discrete = discrete
self.nbits = nbits
def vectorize(self, g):
""" Vectorize graph nodes
Return: a matrix in which rows are the vectors that represents for nodes
"""
vec = Vectorizer(nbits=self.nbits,
d=self.d,
r=self.r,
discrete=self.discrete
)
M = vec.vertex_transform([g])[0]
M_reduce = []
for idx in range(self.n_nodes):
vec = M[idx, :]
for l in range(1, self.L):
vec = vec + M[idx + l*self.n_nodes, :]
M_reduce.append(vec)
M = vstack(M_reduce)
return M
def cdnk(self, g=None):
"""Compute graph node kernel matrix encoding node similarities
Return:
Kernel matrix
"""
M = self.vectorize(g)
K = pairwise.linear_kernel(M, M)
return K