-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgk_base.py
69 lines (37 loc) · 1.39 KB
/
gk_base.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
"""
Base class for graph kernels.
Author : Sandro Vega Pons
"""
import numpy as np
class GK_Base(object):
"""Base class for graph kernels
"""
def compare_pairwise(self, graph_list, normalize=True):
"""Compute the all-pairs kernel values for a list of graphs.
Parameters
----------
graph_list: list of ndarray
A list of graphs (adjacency matrices)
Return
------
K: ndarray, shape = (len(graph_list), len(graph_list))
The similarity matrix of all graphs in graph_list.
"""
#To be defined by each graph kernel.
def _normalize(self, K):
"""Normalize kernel matrix K by using:
k_norm(g1, g2) = k(g1, g2) / sqrt(k(g1,g1) * k(g2,g2))
Parameters
----------
K: ndarray
Kernel matrix to be normalized
Return
------
K_norm: ndarray
Normalized kernel matrix
"""
K_norm = np.zeros(K.shape)
for i in range(K.shape[0]):
for j in range(K.shape[1]):
K_norm[i,j] = K[i,j] / np.sqrt(K[i,i] * K[j,j])
return K_norm