10 static int (*
info)(
const char *fmt, ...) = &printf;
28 while (strrchr(
line,
'\n') == NULL)
32 len = (int) strlen(
line);
41 fprintf(stderr,
"Wrong input format at line %d\n", line_num);
50 double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0;
54 double *prob_estimates = NULL;
60 info(
"Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",
svm_get_svr_probability(model));
63 int *labels = (
int *) malloc(nr_class *
sizeof(
int));
65 prob_estimates = (
double *) malloc(nr_class *
sizeof(
double));
66 fprintf(output,
"labels");
68 fprintf(output,
" %d", labels[j]);
69 fprintf(output,
"\n");
79 double target_label, predict_label;
80 char *idx, *val, *
label, *endptr;
81 int inst_max_index = -1;
83 label = strtok(
line,
" \t\n");
87 target_label = strtod(label, &endptr);
88 if (endptr == label || *endptr !=
'\0')
99 idx = strtok(NULL,
":");
100 val = strtok(NULL,
" \t");
105 x[i].
index = (int) strtol(idx, &endptr, 10);
106 if (endptr == idx || errno != 0 || *endptr !=
'\0' || x[i].
index <= inst_max_index)
109 inst_max_index = x[i].
index;
112 x[i].
value = strtod(val, &endptr);
113 if (endptr == val || errno != 0 || (*endptr !=
'\0' && !isspace(*endptr)))
123 fprintf(output,
"%g", predict_label);
124 for (j = 0; j < nr_class; j++)
125 fprintf(output,
" %g", prob_estimates[j]);
126 fprintf(output,
"\n");
131 fprintf(output,
"%g\n", predict_label);
134 if (predict_label == target_label)
136 error += (predict_label - target_label) * (predict_label - target_label);
137 sump += predict_label;
138 sumt += target_label;
139 sumpp += predict_label * predict_label;
140 sumtt += target_label * target_label;
141 sumpt += predict_label * target_label;
146 info(
"Mean squared error = %g (regression)\n", error / total);
147 info(
"Squared correlation coefficient = %g (regression)\n",
148 ((total * sumpt - sump * sumt) * (total * sumpt - sump * sumt)) /
149 ((total * sumpp - sump * sump) * (total * sumtt - sumt * sumt))
153 info(
"Accuracy = %g%% (%d/%d) (classification)\n",
154 (
double)correct / total * 100, correct, total);
156 free(prob_estimates);
162 "Usage: svm-predict [options] test_file model_file output_file\n" 164 "-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); for one-class SVM only 0 is supported\n" 165 "-q : quiet mode (no outputs)\n" 170 int main(
int argc,
char **argv)
175 for (i = 1; i < argc; i++)
177 if (argv[i][0] !=
'-')
break;
179 switch (argv[i - 1][1])
189 fprintf(stderr,
"Unknown option: -%c\n", argv[i - 1][1]);
197 input = fopen(argv[i],
"r");
200 fprintf(stderr,
"can't open input file %s\n", argv[i]);
204 output = fopen(argv[i + 2],
"w");
207 fprintf(stderr,
"can't open output file %s\n", argv[i + 2]);
213 fprintf(stderr,
"can't open model file %s\n", argv[i + 1]);
222 fprintf(stderr,
"Model does not support probabiliy estimates\n");
229 info(
"Model supports probability estimates, but disabled in prediction.\n");
static char * readline(FILE *input)
def svm_load_model(model_file_name)
void exit_input_error(int line_num)
double svm_get_svr_probability(const svm_model *model)
def svm_predict(y, x, m, options="")
int svm_get_nr_class(const svm_model *model)
int svm_check_probability_model(const svm_model *model)
void output(int index, double value)
int main(int argc, char **argv)
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
double svm_predict_probability(const svm_model *model, const svm_node *x, double *prob_estimates)
static int(* info)(const char *fmt,...)
int svm_get_svm_type(const svm_model *model)
void predict(FILE *input, FILE *output)
void svm_get_labels(const svm_model *model, int *label)
int print_null(const char *s,...)