1 %> @brief Support Vector Machine Classifier
using LibSVM
3 %> Uses the famous LibSVM by Chih-Chung Chang and Chih-Jen Lin [1].
5 %> For documentation on @c c and @c gamma, see [1].
7 %> Also, type
"svmtrain" at MATLAB
's command window to see possible options for the kernel types. However, this binding is not fully
10 %> The possible kernels are shown below
12 %> @arg 0 -- linear: u'*v
13 %> @arg 1 -- polynomial: (gamma*u
'*v + coef0)^degree
14 %> @arg 2 -- radial basis function: exp(-gamma*|u-v|^2)
15 %> @arg 3 -- sigmoid: tanh(gamma*u'*v + coef0)
16 %> @arg 4 -- precomputed kernel (kernel values in training_instance_matrix)
19 %> @sa uip_clssr_svm.m, svmtrain()
21 %> <h3>References:</3>
25 %> =2 (radial basis
function, or
"Gaussian")
31 %> For polynomial kernel
33 %> =0.001 .
"tolerance of termination"
35 %> Coef0
for the sigmoid & polynomial kernel
37 %> =1. LibSVM
's "shrinking"
39 %> Whether to weight the observations (for unbalanced classes)
41 %> weight exponent. If @ref flag_weighted is true, weights will be powered by this exponent before weight normalization
43 %> =100. cache size in MB
47 properties(SetAccess=private)
56 function o = clssr_svm(o)
57 o.classtitle = 'Support Vector Machine
';
62 methods(Access=protected)
64 function o = do_train(o, data)
65 o.classlabels = data.classlabels;
67 s = sprintf('-m %d -e %g -c %g -b %d -h %d -e 1e-2
', o.cachesize, o.epsilon, o.c, o.FLAG_B, o.flag_shrink);
71 s = cat(2, s, '-t 0
');
73 s = cat(2, s, sprintf('-t 1 -d %d -r %g
', o.degree, o.coef0));
74 case 2 % Radial basis Gaussian
75 s = cat(2, s, sprintf('-t 2 -g %g
', o.gamma));
77 s = cat(2, s, sprintf('-t 3 -g %g -r %g
', o.gamma, o.coef0));
79 irerror('Pre-computed kernel not supported!
');
83 % Calculates weights for the classes
84 w = [0:data.nc-1; data.get_weights(o.weightexp)];
85 s = [s ' ' sprintf('-w%d %g
', w)];
89 o.svm = svmtrain(data.classes, data.X, s);
91 o.no_svs = o.svm.totalSV;
93 % disp('ooooooooooooooooooooooooooooooo
');
100 function est = do_use(o, data)
102 est.classlabels = o.classlabels;
103 est = est.copy_from_data(data);
105 if isempty(data.classes)
106 data.classes = zeros(data.no, 1);
109 % Accuracy will not be correct because there may be class shifts in both
110 % the training and testing datasets.
112 [predict_label, accuracy, dec_values] = svmpredict(data.classes, data.X, o.svm, sprintf('-b %d
', o.FLAG_B));
113 % [predict_label, accuracy, dec_values] = svmpredict(data.classes, data.X, o.svm, '-b 0
');
116 % est.classes = predict_label;
120 est.X = classes2boolean(predict_label);
Support Vector Machine Classifier using LibSVM.