@@ -22,7 +22,7 @@ def __init__(self, sigma):
22
22
self .gamma = 1 / (2.0 * sigma ** 2 )
23
23
24
24
def eval (self , x1 , x2 ):
25
- return np .exp (- gamma * lin .norm (x1 - x2 )** 2 )
25
+ return np .exp (- self . gamma * lin .norm (x1 - x2 )** 2 )
26
26
27
27
class SVM :
28
28
# Support Vector Machine Classifier
@@ -67,6 +67,13 @@ def test(self, X, y):
67
67
error [guess != y ] = 1
68
68
return np .float (np .sum (error )) / len (X )
69
69
70
+ def countSupVectors (self ):
71
+ count = 0
72
+ for a in self .alphas :
73
+ if a == 0 :
74
+ count += 1
75
+ return count
76
+
70
77
def findC (self , X , y , count = 50 , kfolds = 5 ):
71
78
# find a good estimate of C with kfold cross validation
72
79
@@ -75,11 +82,14 @@ def findC(self, X, y, count=50, kfolds=5):
75
82
np .random .shuffle (data )
76
83
partitions = np .array_split (data , kfolds )
77
84
candidates = np .logspace (0 , 5 , num = count , base = np .e )
85
+ err = np .zeros (count )
86
+ svec = err .copy ()
78
87
79
88
minErr = np .inf
80
89
C = candidates [0 ]
81
- for c in candidates :
90
+ for index , c in enumerate ( candidates ) :
82
91
errors = np .zeros (kfolds )
92
+ supvec_count = errors .copy ()
83
93
for i in range (kfolds ):
84
94
test = partitions [i ]
85
95
train = np .vstack ([partitions [x ] for x in range (kfolds ) if x != i ])
@@ -90,12 +100,16 @@ def findC(self, X, y, count=50, kfolds=5):
90
100
temp = SVM (c , self .k )
91
101
temp .train (trainx , trainy )
92
102
errors [i ] = temp .test (testx , testy )
93
- err = np .mean (errors )
94
- print c , err , minErr
95
- if err < minErr :
103
+ supvec_count [i ] = temp .countSupVectors ()
104
+ err [index ] = np .mean (errors )
105
+ svec [index ] = np .mean (supvec_count )
106
+ print c , err
107
+ if err [index ] < minErr :
96
108
C = c
97
- minErr = err
109
+ minErr = err [ index ]
98
110
99
- print candidates
111
+ print "C, err, #svec"
112
+ for i in range (count ):
113
+ print candidates [i ], err [i ], svec [i ]
100
114
print "Final value: " , C
101
115
return C
0 commit comments