-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernels.py
88 lines (79 loc) · 3.05 KB
/
kernels.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
import numpy as np
from sklearn import manifold
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics.pairwise import pairwise_distances
import utils as utils
from shortestpath import ShortestPathKernel
import signal
clf = SVC(kernel="linear", C = 1.0)
kernel = ShortestPathKernel()
kFold = StratifiedKFold(n_splits = 10, shuffle = True)
class TimeoutException(Exception):
pass
def timeout(signum, frame):
raise TimeoutException
signal.signal(signal.SIGALRM, timeout)
def run(numbers,label,folder):
(graph,label) = utils.readFromFile(numbers,label,folder)
adjMatrix = list()
for el in graph:
adjMatrix.append(el.get_adjacency_matrix())
adjMatrix = np.array(adjMatrix,dtype=object)
shortestPaths = kernel.shortestPaths(adjMatrix)
similarities = kernel.getSimilarities(shortestPaths)
distances = pairwise_distances(similarities, metric='euclidean',n_jobs=-1)
scores_ln = cross_val_score(clf, distances, label, cv = kFold, n_jobs= -1)
res = utils.prepareResults(scores_ln,-1,-1,"\n***************"+folder+"***************\nWithout Manifold Learning\n")
res = res + manifoldLearning(clf,distances,label,kFold,0) #LLE
res = res + manifoldLearning(clf,distances,label,kFold,1) #ISOMAP
utils.writeToFile(res)
if folder == "PPI":
print("** Analysis over PPI dataset are done, now SHOCK dataset is performed. **")
else:
print("** END! CHECK RESULTS.txt FILE! **")
def manifoldLearning(clf, D, label, kFold, flag):
best = None
worst = None
i_b = 0
j_b = 0
i_w = 0
j_w = 0
res = ""
for i in range(2,31):
for j in range(2,21):
if flag == 0:
x_t = manifold.LocallyLinearEmbedding(n_neighbors=i,n_components=j).fit_transform(D)
else:
x_t = manifold.Isomap(n_neighbors=i,n_components=j).fit_transform(D)
try:
signal.alarm(5)
scores_new = cross_val_score(clf,x_t,label,cv=kFold,n_jobs=-1)
except TimeoutException:
signal.alarm(0)
continue
else:
signal.alarm(0)
if j == 2 and i == 2:
best = scores_new
worst = scores_new
i_b = i
j_b = j
i_w = i
j_w = j
if np.mean(scores_new) < np.mean(worst):
worst = scores_new
i_w = i
j_w = j
if np.mean(scores_new) > np.mean(best):
best = scores_new
i_b = i
j_b = j
if flag == 0:
res = res + utils.prepareResults(best,i_b,j_b,"\nWith Manifold Learning\n*** LLE ***\nBest: ")
res = res + utils.prepareResults(worst,i_w,j_w,"\nWorst: ")
else:
res = res + utils.prepareResults(best,i_b,j_b,"\nWith Manifold Learning\n*** ISOMAP ***\nBest: ")
res = res + utils.prepareResults(worst,i_w,j_w,"\nWorst: ")
return res