-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsvm.py
76 lines (64 loc) · 2.51 KB
/
svm.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
#!/bin/python2
# coding: utf-8
"""
This code implements SVM using the features from the DNN.
Copyright 2017 by Branislav Gerazov.
See the file LICENSE for the licence associated with this software.
Author:
Branislav Gerazov, April 2017
"""
from __future__ import absolute_import, division, print_function
import numpy as np
import cPickle as pickle
import time
from sklearn.svm import SVC
#%% time it
t0 = time.time()
#%% load data
feats = []
targets = []
for fold in range(10):
with open('best_feats_shape/best_feats_fold{}.pkl'.format(fold),'rb') as f:
best_feats, target = pickle.load(f)
rand_sample, feats_all, feats_train, feats_val, feats_test = best_feats
target_train, target_val, target_test = target
feats.append([feats_train, feats_val, feats_test])
targets.append([target_train, target_val, target_test])
#%% master loop
Cs = np.array([300000])
gammas = np.array([0.0005, .001, .003, .01, .03, .1, .3, 1, 3])
accs_master = np.zeros((Cs.size, gammas.size, 1))
for i_C, C in enumerate(Cs[::-1]):
print()
print()
print('############################################################')
print('############################################################')
print('C = {}'.format(C))
for i_gamma, gamma in enumerate(gammas):
print()
print('****************************************')
print('****************************************')
print('Gamma = {}'.format(gamma))
print()
svm = SVC(C=C, kernel='rbf', gamma=gamma,
probability=False, cache_size=1000, random_state=42)
accs = []
for fold in range(10):
feats_train = np.vstack(feats[fold][0:2])
targets_train = np.concatenate(targets[fold][0:2])
feats_test = feats[fold][-1]
targets_test = targets[fold][-1]
svm.fit(feats_train, targets_train)
acc = svm.score(feats_test, targets_test)
print('Fold : {}, accuracy : {:.4f}'.format(fold, acc))
accs.append(acc)
#%
print('Mean accuracy for all folds : {:.4f}'.format(np.mean(accs)))
accs_master[i_C, i_gamma] = np.mean(accs)
#%% print out
print('############################################################')
print('############################################################')
print('final results')
for i_C, C in enumerate(Cs[::-1]):
for i_gamma, gamma in enumerate(gammas):
print('{},{},{}'.format(C, gamma, accs_master[i_C, i_gamma]))