svm-predict.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "svm.h"
  6. char* line;
  7. int max_line_len = 1024;
  8. struct svm_node *x;
  9. int max_nr_attr = 64;
  10. struct svm_model* model;
  11. int predict_probability=0;
  12. void predict(FILE *input, FILE *output)
  13. {
  14. int correct = 0;
  15. int total = 0;
  16. double error = 0;
  17. double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
  18. int svm_type=svm_get_svm_type(model);
  19. int nr_class=svm_get_nr_class(model);
  20. double *prob_estimates=NULL;
  21. int j;
  22. if(predict_probability)
  23. {
  24. if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
  25. 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));
  26. else
  27. {
  28. int *labels=(int *) malloc(nr_class*sizeof(int));
  29. svm_get_labels(model,labels);
  30. prob_estimates = (double *) malloc(nr_class*sizeof(double));
  31. fprintf(output,"labels");
  32. for(j=0;j<nr_class;j++)
  33. fprintf(output," %d",labels[j]);
  34. fprintf(output,"\n");
  35. free(labels);
  36. }
  37. }
  38. while(1)
  39. {
  40. int i = 0;
  41. int c;
  42. double target,v;
  43. if (fscanf(input,"%lf",&target)==EOF)
  44. break;
  45. while(1)
  46. {
  47. if(i>=max_nr_attr-1) // need one more for index = -1
  48. {
  49. max_nr_attr *= 2;
  50. x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
  51. }
  52. do {
  53. c = getc(input);
  54. if(c=='\n' || c==EOF) goto out2;
  55. } while(isspace(c));
  56. ungetc(c,input);
  57. if (fscanf(input,"%d:%lf",&x[i].index,&x[i].value) < 2)
  58. {
  59. fprintf(stderr,"Wrong input format at line %d\n", total+1);
  60. exit(1);
  61. }
  62. ++i;
  63. }
  64. out2:
  65. x[i].index = -1;
  66. if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC))
  67. {
  68. v = svm_predict_probability(model,x,prob_estimates);
  69. fprintf(output,"%g ",v);
  70. for(j=0;j<nr_class;j++)
  71. fprintf(output,"%g ",prob_estimates[j]);
  72. fprintf(output,"\n");
  73. }
  74. else
  75. {
  76. v = svm_predict(model,x);
  77. fprintf(output,"%g\n",v);
  78. }
  79. if(v == target)
  80. ++correct;
  81. error += (v-target)*(v-target);
  82. sumv += v;
  83. sumy += target;
  84. sumvv += v*v;
  85. sumyy += target*target;
  86. sumvy += v*target;
  87. ++total;
  88. }
  89. if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
  90. {
  91. printf("Mean squared error = %g (regression)\n",error/total);
  92. printf("Squared correlation coefficient = %g (regression)\n",
  93. ((total*sumvy-sumv*sumy)*(total*sumvy-sumv*sumy))/
  94. ((total*sumvv-sumv*sumv)*(total*sumyy-sumy*sumy))
  95. );
  96. }
  97. else
  98. printf("Accuracy = %g%% (%d/%d) (classification)\n",
  99. (double)correct/total*100,correct,total);
  100. if(predict_probability)
  101. free(prob_estimates);
  102. }
  103. void exit_with_help()
  104. {
  105. printf(
  106. "Usage: svm-predict [options] test_file model_file output_file\n"
  107. "options:\n"
  108. "-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); for one-class SVM only 0 is supported\n"
  109. );
  110. exit(1);
  111. }
  112. int main(int argc, char **argv)
  113. {
  114. FILE *input, *output;
  115. int i;
  116. // parse options
  117. for(i=1;i<argc;i++)
  118. {
  119. if(argv[i][0] != '-') break;
  120. ++i;
  121. switch(argv[i-1][1])
  122. {
  123. case 'b':
  124. predict_probability = atoi(argv[i]);
  125. break;
  126. default:
  127. fprintf(stderr,"unknown option\n");
  128. exit_with_help();
  129. }
  130. }
  131. if(i>=argc)
  132. exit_with_help();
  133. input = fopen(argv[i],"r");
  134. if(input == NULL)
  135. {
  136. fprintf(stderr,"can't open input file %s\n",argv[i]);
  137. exit(1);
  138. }
  139. output = fopen(argv[i+2],"w");
  140. if(output == NULL)
  141. {
  142. fprintf(stderr,"can't open output file %s\n",argv[i+2]);
  143. exit(1);
  144. }
  145. if((model=svm_load_model(argv[i+1]))==0)
  146. {
  147. fprintf(stderr,"can't open model file %s\n",argv[i+1]);
  148. exit(1);
  149. }
  150. line = (char *) malloc(max_line_len*sizeof(char));
  151. x = (struct svm_node *) malloc(max_nr_attr*sizeof(struct svm_node));
  152. if(predict_probability)
  153. {
  154. if(svm_check_probability_model(model)==0)
  155. {
  156. fprintf(stderr,"Model does not support probabiliy estimates\n");
  157. exit(1);
  158. }
  159. }
  160. else
  161. {
  162. if(svm_check_probability_model(model)!=0)
  163. printf("Model supports probability estimates, but disabled in prediction.\n");
  164. }
  165. predict(input,output);
  166. svm_destroy_model(model);
  167. free(line);
  168. free(x);
  169. fclose(input);
  170. fclose(output);
  171. return 0;
  172. }