IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
clssr_svm.m
Go to the documentation of this file.
1 %> @brief Support Vector Machine Classifier using LibSVM
2 %>
3 %> Uses the famous LibSVM by Chih-Chung Chang and Chih-Jen Lin [1].
4 %>
5 %> For documentation on @c c and @c gamma, see [1].
6 %>
7 %> Also, type "svmtrain" at MATLAB's command window to see possible options for the kernel types. However, this binding is not fully
8 %> implemented yet.
9 %>
10 %> The possible kernels are shown below
11 %>
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)
17 %>
18 %>
19 %> @sa uip_clssr_svm.m, svmtrain()
20 %>
21 %> <h3>References:</3>
22 %> [1] http://www.csie.ntu.edu.tw/~cjlin/libsvm/
23 classdef clssr_svm < clssr
24  properties
25  %> =2 (radial basis function, or "Gaussian")
26  kernel = 2;
27  %> Cost parameter "C"
28  c = 4;
29  %> For radial basis,
30  gamma = 1;
31  %> For polynomial kernel
32  degree = 3;
33  %> =0.001 . "tolerance of termination"
34  epsilon = 0.001;
35  %> Coef0 for the sigmoid & polynomial kernel
36  coef0 = 0;
37  %> =1. LibSVM's "shrinking"
38  flag_shrink = 1;
39  %> Whether to weight the observations (for unbalanced classes)
40  flag_weighted = 0;
41  %> weight exponent. If @ref flag_weighted is true, weights will be powered by this exponent before weight normalization
42  weightexp = 1;
43  %> =100. cache size in MB
44  cachesize = 100;
45  end;
46 
47  properties(SetAccess=private)
48  no_svs = 0;
49  svm;
50 
51  FLAG_B = 0;
52  end;
53 
54 
55  methods
56  function o = clssr_svm(o)
57  o.classtitle = 'Support Vector Machine';
58  o.short = 'SVM';
59  end;
60  end;
61 
62  methods(Access=protected)
63 
64  function o = do_train(o, data)
65  o.classlabels = data.classlabels;
66 
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);
68 
69  switch o.kernel
70  case 0
71  s = cat(2, s, '-t 0');
72  case 1 % Polynomial
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));
76  case 3 % Sigmoid
77  s = cat(2, s, sprintf('-t 3 -g %g -r %g', o.gamma, o.coef0));
78  case 4 % Sigmoid
79  irerror('Pre-computed kernel not supported!');
80  end;
81 
82  if o.flag_weighted
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)];
86  end;
87 
88  tic;
89  o.svm = svmtrain(data.classes, data.X, s);
90 % try
91  o.no_svs = o.svm.totalSV;
92 % catch ME
93 % disp('ooooooooooooooooooooooooooooooo');
94 % end;
95  o.time_train = toc;
96  end;
97 
98 
99 
100  function est = do_use(o, data)
101  est = estimato();
102  est.classlabels = o.classlabels;
103  est = est.copy_from_data(data);
104 
105  if isempty(data.classes)
106  data.classes = zeros(data.no, 1);
107  end;
108 
109  % Accuracy will not be correct because there may be class shifts in both
110  % the training and testing datasets.
111  tic();
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');
114  o.time_use = toc();
115 
116 % est.classes = predict_label;
117  if o.FLAG_B
118  est.X = dec_values;
119  else
120  est.X = classes2boolean(predict_label);
121  end;
122  end;
123  end;
124 end
Classifiers base class.
Definition: clssr.m:6
Support Vector Machine Classifier using LibSVM.
Definition: clssr_svm.m:23