-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsp_kernel.py
More file actions
41 lines (33 loc) · 766 Bytes
/
sp_kernel.py
File metadata and controls
41 lines (33 loc) · 766 Bytes
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
"""
Spectrum kernel implementation [C.Leslie, 2002]
"""
import numpy as np
import multiprocessing as mp
from utils import kernel_worker
def sp_kernel(s1, s2, k):
"""
Spectrum kernel function.
"""
n = len(s1)
k_dict = {}
for i in range(n - k + 1):
chunk = i + k
sub1, sub2 = s1[i:chunk], s2[i:chunk]
if sub1 in k_dict:
k_dict[sub1][0] += 1
else:
k_dict[sub1] = [1, 0]
if sub2 in k_dict:
k_dict[sub2][1] += 1
else:
k_dict[sub2] = [0, 1]
return sum([v[0] * v[1] for v in k_dict.values()])
def sp_kernel_comb(s1, s2, k_list, weights=None):
"""
Linear combination of spectrum kernels.
"""
prods = np.array([sp_kernel(s1, s2, k) for k in k_list])
if weights is None:
return np.sum(prods)
else:
return np.sum(weights*prods)