9 private String input_file_name;
10 private String model_file_name;
11 private String error_msg;
17 public void print(String s) {}
23 "Usage: svm_train [options] training_set_file [model_file]\n" 25 +
"-s svm_type : set type of SVM (default 0)\n" 26 +
" 0 -- C-SVC (multi-class classification)\n" 27 +
" 1 -- nu-SVC (multi-class classification)\n" 28 +
" 2 -- one-class SVM\n" 29 +
" 3 -- epsilon-SVR (regression)\n" 30 +
" 4 -- nu-SVR (regression)\n" 31 +
"-t kernel_type : set type of kernel function (default 2)\n" 32 +
" 0 -- linear: u'*v\n" 33 +
" 1 -- polynomial: (gamma*u'*v + coef0)^degree\n" 34 +
" 2 -- radial basis function: exp(-gamma*|u-v|^2)\n" 35 +
" 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n" 36 +
" 4 -- precomputed kernel (kernel values in training_set_file)\n" 37 +
"-d degree : set degree in kernel function (default 3)\n" 38 +
"-g gamma : set gamma in kernel function (default 1/num_features)\n" 39 +
"-r coef0 : set coef0 in kernel function (default 0)\n" 40 +
"-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n" 41 +
"-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n" 42 +
"-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n" 43 +
"-m cachesize : set cache memory size in MB (default 100)\n" 44 +
"-e epsilon : set tolerance of termination criterion (default 0.001)\n" 45 +
"-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n" 46 +
"-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n" 47 +
"-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n" 48 +
"-v n : n-fold cross validation mode\n" 49 +
"-q : quiet mode (no outputs)\n" 57 int total_correct = 0;
58 double total_error = 0;
59 double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
60 double[] target =
new double[prob.
l];
62 svm.svm_cross_validation(prob,param,nr_fold,target);
70 total_error += (v-y)*(v-y);
77 System.out.print(
"Cross Validation Mean squared error = "+total_error/prob.
l+
"\n");
78 System.out.print(
"Cross Validation Squared correlation coefficient = "+
79 ((prob.
l*sumvy-sumv*sumy)*(prob.
l*sumvy-sumv*sumy))/
80 ((prob.
l*sumvv-sumv*sumv)*(prob.
l*sumyy-sumy*sumy))+
"\n" 86 if(target[i] == prob.
y[i])
88 System.out.print(
"Cross Validation Accuracy = "+100.0*total_correct/prob.
l+
"%\n");
92 private void run(String argv[])
throws IOException
96 error_msg =
svm.svm_check_parameter(prob,param);
100 System.err.print(
"ERROR: "+error_msg+
"\n");
104 if(cross_validation != 0)
110 model =
svm.svm_train(prob,param);
111 svm.svm_save_model(model_file_name,model);
115 public static void main(String argv[])
throws IOException
121 private static double atof(String s)
123 double d = Double.valueOf(s).doubleValue();
124 if (Double.isNaN(d) || Double.isInfinite(d))
126 System.err.print(
"NaN or Infinity in input\n");
132 private static int atoi(String s)
134 return Integer.parseInt(s);
158 param.
weight =
new double[0];
159 cross_validation = 0;
162 for(i=0;i<argv.length;i++)
164 if(argv[i].charAt(0) !=
'-')
break;
167 switch(argv[i-1].charAt(1))
176 param.
degree = atoi(argv[i]);
179 param.
gamma = atof(argv[i]);
182 param.
coef0 = atof(argv[i]);
185 param.
nu = atof(argv[i]);
191 param.
C = atof(argv[i]);
194 param.
eps = atof(argv[i]);
197 param.
p = atof(argv[i]);
206 print_func = svm_print_null;
210 cross_validation = 1;
211 nr_fold = atoi(argv[i]);
214 System.err.print(
"n-fold cross validation: n must >= 2\n");
227 double[] old = param.
weight;
236 System.err.print(
"Unknown option: " + argv[i-1] +
"\n");
241 svm.svm_set_print_string_function(print_func);
248 input_file_name = argv[i];
251 model_file_name = argv[i+1];
254 int p = argv[i].lastIndexOf(
'/');
256 model_file_name = argv[i].substring(p)+
".model";
264 BufferedReader fp =
new BufferedReader(
new FileReader(input_file_name));
265 Vector<Double> vy =
new Vector<Double>();
266 Vector<svm_node[]> vx =
new Vector<svm_node[]>();
271 String
line = fp.readLine();
272 if(line == null)
break;
274 StringTokenizer st =
new StringTokenizer(line,
" \t\n\r\f:");
276 vy.addElement(atof(st.nextToken()));
277 int m = st.countTokens()/2;
282 x[j].
index = atoi(st.nextToken());
283 x[j].
value = atof(st.nextToken());
285 if(m>0) max_index = Math.max(max_index, x[m-1].index);
292 for(
int i=0;i<prob.
l;i++)
293 prob.
x[i] = vx.elementAt(i);
294 prob.
y =
new double[prob.
l];
295 for(
int i=0;i<prob.
l;i++)
296 prob.
y[i] = vy.elementAt(i);
298 if(param.
gamma == 0 && max_index > 0)
302 for(
int i=0;i<prob.
l;i++)
304 if (prob.
x[i][0].
index != 0)
306 System.err.print(
"Wrong kernel matrix: first column must be 0:sample_serial_number\n");
309 if ((
int)prob.
x[i][0].
value <= 0 || (
int)prob.
x[i][0].
value > max_index)
311 System.err.print(
"Wrong input format: sample_serial_number out of range\n");
void run(class_loader::ClassLoader *loader)
struct svm_parameter param
def svm_train(arg1, arg2=None, arg3=None)
void read_problem(const char *filename, mxArray *plhs[])
int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name)
ROSCONSOLE_DECL void print(FilterBase *filter, void *logger, Level level, const char *file, int line, const char *function, const char *fmt,...) ROSCONSOLE_PRINTF_ATTRIBUTE(7
static final int PRECOMPUTED
int main(int argc, char **argv)
double do_cross_validation()
static final int EPSILON_SVR