7 #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 14 "Usage: svm-train [options] training_set_file [model_file]\n" 16 "-s svm_type : set type of SVM (default 0)\n" 17 " 0 -- C-SVC (multi-class classification)\n" 18 " 1 -- nu-SVC (multi-class classification)\n" 19 " 2 -- one-class SVM\n" 20 " 3 -- epsilon-SVR (regression)\n" 21 " 4 -- nu-SVR (regression)\n" 22 "-t kernel_type : set type of kernel function (default 2)\n" 23 " 0 -- linear: u'*v\n" 24 " 1 -- polynomial: (gamma*u'*v + coef0)^degree\n" 25 " 2 -- radial basis function: exp(-gamma*|u-v|^2)\n" 26 " 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n" 27 " 4 -- precomputed kernel (kernel values in training_set_file)\n" 28 "-d degree : set degree in kernel function (default 3)\n" 29 "-g gamma : set gamma in kernel function (default 1/num_features)\n" 30 "-r coef0 : set coef0 in kernel function (default 0)\n" 31 "-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n" 32 "-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n" 33 "-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n" 34 "-m cachesize : set cache memory size in MB (default 100)\n" 35 "-e epsilon : set tolerance of termination criterion (default 0.001)\n" 36 "-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n" 37 "-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n" 38 "-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n" 39 "-v n: n-fold cross validation mode\n" 40 "-q : quiet mode (no outputs)\n" 47 fprintf(stderr,
"Wrong input format at line %d\n", line_num);
51 void parse_command_line(
int argc,
char **argv,
char *input_file_name,
char *model_file_name);
72 while (strrchr(
line,
'\n') == NULL)
76 len = (int) strlen(
line);
83 int main(
int argc,
char **argv)
85 char input_file_name[1024];
86 char model_file_name[1024];
87 const char *error_msg;
95 fprintf(stderr,
"ERROR: %s\n", error_msg);
108 fprintf(stderr,
"can't save model to file %s\n", model_file_name);
125 int total_correct = 0;
126 double total_error = 0;
127 double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
134 for (i = 0; i <
prob.
l; i++)
136 double y =
prob.
y[i];
137 double v = target[i];
138 total_error += (v - y) * (v - y);
145 printf(
"Cross Validation Mean squared error = %g\n", total_error /
prob.
l);
146 printf(
"Cross Validation Squared correlation coefficient = %g\n",
147 ((
prob.
l * sumvy - sumv * sumy) * (
prob.
l * sumvy - sumv * sumy)) /
148 ((
prob.
l * sumvv - sumv * sumv) * (
prob.
l * sumyy - sumy * sumy))
153 for (i = 0; i <
prob.
l; i++)
154 if (target[i] ==
prob.
y[i])
156 printf(
"Cross Validation Accuracy = %g%%\n", 100.0 * total_correct /
prob.
l);
164 void (*print_func)(
const char*) = NULL;
185 for (i = 1; i < argc; i++)
187 if (argv[i][0] !=
'-')
break;
190 switch (argv[i - 1][1])
237 fprintf(stderr,
"n-fold cross validation: n must >= 2\n");
249 fprintf(stderr,
"Unknown option: -%c\n", argv[i - 1][1]);
261 strcpy(input_file_name, argv[i]);
264 strcpy(model_file_name, argv[i + 1]);
267 char *p = strrchr(argv[i],
'/');
272 sprintf(model_file_name,
"%s.model", p);
280 int elements,
max_index, inst_max_index, i, j;
281 FILE *fp = fopen(filename,
"r");
283 char *idx, *val, *label;
287 fprintf(stderr,
"can't open input file %s\n", filename);
298 char *p = strtok(
line,
" \t");
303 p = strtok(NULL,
" \t");
304 if (p == NULL || *p ==
'\n')
319 for (i = 0; i <
prob.
l; i++)
323 prob.
x[i] = &x_space[j];
324 label = strtok(
line,
" \t\n");
328 prob.
y[i] = strtod(label, &endptr);
329 if (endptr == label || *endptr !=
'\0')
334 idx = strtok(NULL,
":");
335 val = strtok(NULL,
" \t");
341 x_space[j].
index = (int) strtol(idx, &endptr, 10);
342 if (endptr == idx || errno != 0 || *endptr !=
'\0' || x_space[j].
index <= inst_max_index)
345 inst_max_index = x_space[j].
index;
348 x_space[j].
value = strtod(val, &endptr);
349 if (endptr == val || errno != 0 || (*endptr !=
'\0' && !isspace(*endptr)))
355 if (inst_max_index > max_index)
356 max_index = inst_max_index;
357 x_space[j++].
index = -1;
364 for (i = 0; i <
prob.
l; i++)
368 fprintf(stderr,
"Wrong input format: first column must be 0:sample_serial_number\n");
373 fprintf(stderr,
"Wrong input format: sample_serial_number out of range\n");
const char * svm_check_parameter(const svm_problem *prob, const svm_parameter *param)
def svm_train(arg1, arg2=None, arg3=None)
int main(int argc, char **argv)
void svm_cross_validation(const svm_problem *prob, const svm_parameter *param, int nr_fold, double *target)
def svm_save_model(model_file_name, model)
void print_null(const char *s)
void do_cross_validation()
struct svm_node * x_space
struct svm_parameter param
static char * readline(FILE *input)
void exit_input_error(int line_num)
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
void parse_command_line(int argc, char **argv, char *input_file_name, char *model_file_name)
void svm_destroy_param(svm_parameter *param)
void read_problem(const char *filename)
void svm_set_print_string_function(void(*print_func)(const char *))