In [1]:
import time
from sklearn import svm
from sklearn import cross_validation as cv
from kcat import datasets as ds
from kcat.kernels import functions as fn
from kcat.utils import pgen
In [2]:
X, Y, y = ds.Synthetic(1000, 25).data_arrays
Xpgen = pgen(X)
Xp = Xpgen(X)
In [3]:
# Test kernel k0:
clf = svm.SVC(kernel='precomputed')
t0 = time.time()
gram = fn.k0(Y, Y)
print("Execution time: {:0.3f} s".format(time.time() - t0))
scores = cv.cross_val_score(clf, gram, y, cv=2)
print("Mean score: {:0.3f}".format(scores.mean()))
In [5]:
# Test kernel k1:
clf = svm.SVC(kernel='precomputed')
t0 = time.time()
gram = fn.k1(X, X, Xp, Xp)
print("Execution time: {:0.3f} s".format(time.time() - t0))
scores = cv.cross_val_score(clf, gram, y, cv=2)
print("Mean score: {:0.3f}".format(scores.mean()))
In [6]:
# Test kernel k2:
clf = svm.SVC(kernel='precomputed')
t0 = time.time()
gram = fn.k2(X, X, Xp, Xp)
print("Execution time: {:0.3f} s".format(time.time() - t0))
scores = cv.cross_val_score(clf, gram, y, cv=2)
print("Mean score: {:0.3f}".format(scores.mean()))
In [8]:
# Test kernel m1:
clf = svm.SVC(kernel='precomputed')
t0 = time.time()
gram = fn.m3(X, X, Xp, Xp, alpha=1.5**-4, prev='f1', post='ident', gamma=2**-2)
print("Execution time: {:0.3f} s".format(time.time() - t0))
scores = cv.cross_val_score(clf, gram, y, cv=2)
print("Mean score: {:0.3f}".format(scores.mean()))
In [6]: