24 while(strrchr(
line,
'\n') == NULL)
28 len = (int) strlen(
line);
37 fprintf(stderr,
"Wrong input format at line %d\n", line_num);
46 double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0;
50 double *prob_estimates=NULL;
56 printf(
"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));
59 int *labels=(
int *) malloc(nr_class*
sizeof(
int));
61 prob_estimates = (
double *) malloc(nr_class*
sizeof(
double));
62 fprintf(output,
"labels");
64 fprintf(output,
" %d",labels[j]);
75 double target_label, predict_label;
76 char *idx, *val, *
label, *endptr;
77 int inst_max_index = -1;
79 label = strtok(
line,
" \t\n");
83 target_label = strtod(label,&endptr);
84 if(endptr == label || *endptr !=
'\0')
95 idx = strtok(NULL,
":");
96 val = strtok(NULL,
" \t");
101 x[i].
index = (int) strtol(idx,&endptr,10);
102 if(endptr == idx || errno != 0 || *endptr !=
'\0' || x[i].
index <= inst_max_index)
105 inst_max_index = x[i].
index;
108 x[i].
value = strtod(val,&endptr);
109 if(endptr == val || errno != 0 || (*endptr !=
'\0' && !isspace(*endptr)))
119 fprintf(output,
"%g",predict_label);
120 for(j=0;j<nr_class;j++)
121 fprintf(output,
" %g",prob_estimates[j]);
122 fprintf(output,
"\n");
127 fprintf(output,
"%g\n",predict_label);
130 if(predict_label == target_label)
132 error += (predict_label-target_label)*(predict_label-target_label);
133 sump += predict_label;
134 sumt += target_label;
135 sumpp += predict_label*predict_label;
136 sumtt += target_label*target_label;
137 sumpt += predict_label*target_label;
142 printf(
"Mean squared error = %g (regression)\n",error/total);
143 printf(
"Squared correlation coefficient = %g (regression)\n",
144 ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/
145 ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt))
149 printf(
"Accuracy = %g%% (%d/%d) (classification)\n",
150 (
double)correct/total*100,correct,total);
152 free(prob_estimates);
158 "Usage: svm-predict [options] test_file model_file output_file\n" 160 "-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); for one-class SVM only 0 is supported\n" 165 int main(
int argc,
char **argv)
173 if(argv[i][0] !=
'-')
break;
181 fprintf(stderr,
"Unknown option: -%c\n", argv[i-1][1]);
188 input = fopen(argv[i],
"r");
191 fprintf(stderr,
"can't open input file %s\n",argv[i]);
195 output = fopen(argv[i+2],
"w");
198 fprintf(stderr,
"can't open output file %s\n",argv[i+2]);
204 fprintf(stderr,
"can't open model file %s\n",argv[i+1]);
213 fprintf(stderr,
"Model does not support probabiliy estimates\n");
220 printf(
"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)
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)