You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

svm_predict.java 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import libsvm.*;
  2. import java.io.*;
  3. import java.util.*;
  4. class svm_predict {
  5. private static svm_print_interface svm_print_null = new svm_print_interface()
  6. {
  7. public void print(String s) {}
  8. };
  9. private static svm_print_interface svm_print_stdout = new svm_print_interface()
  10. {
  11. public void print(String s)
  12. {
  13. System.out.print(s);
  14. }
  15. };
  16. private static svm_print_interface svm_print_string = svm_print_stdout;
  17. static void info(String s)
  18. {
  19. svm_print_string.print(s);
  20. }
  21. private static double atof(String s)
  22. {
  23. return Double.valueOf(s).doubleValue();
  24. }
  25. private static int atoi(String s)
  26. {
  27. return Integer.parseInt(s);
  28. }
  29. private static void predict(BufferedReader input, DataOutputStream output, svm_model model, int predict_probability) throws IOException
  30. {
  31. int correct = 0;
  32. int total = 0;
  33. double error = 0;
  34. double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
  35. int svm_type=svm.svm_get_svm_type(model);
  36. int nr_class=svm.svm_get_nr_class(model);
  37. double[] prob_estimates=null;
  38. if(predict_probability == 1)
  39. {
  40. if(svm_type == svm_parameter.EPSILON_SVR ||
  41. svm_type == svm_parameter.NU_SVR)
  42. {
  43. svm_predict.info("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma="+svm.svm_get_svr_probability(model)+"\n");
  44. }
  45. else
  46. {
  47. int[] labels=new int[nr_class];
  48. svm.svm_get_labels(model,labels);
  49. prob_estimates = new double[nr_class];
  50. output.writeBytes("labels");
  51. for(int j=0;j<nr_class;j++)
  52. output.writeBytes(" "+labels[j]);
  53. output.writeBytes("\n");
  54. }
  55. }
  56. while(true)
  57. {
  58. String line = input.readLine();
  59. if(line == null) break;
  60. StringTokenizer st = new StringTokenizer(line," \t\n\r\f:");
  61. double target = atof(st.nextToken());
  62. int m = st.countTokens()/2;
  63. svm_node[] x = new svm_node[m];
  64. for(int j=0;j<m;j++)
  65. {
  66. x[j] = new svm_node();
  67. x[j].index = atoi(st.nextToken());
  68. x[j].value = atof(st.nextToken());
  69. }
  70. double v;
  71. if (predict_probability==1 && (svm_type==svm_parameter.C_SVC || svm_type==svm_parameter.NU_SVC))
  72. {
  73. v = svm.svm_predict_probability(model,x,prob_estimates);
  74. output.writeBytes(v+" ");
  75. for(int j=0;j<nr_class;j++)
  76. output.writeBytes(prob_estimates[j]+" ");
  77. output.writeBytes("\n");
  78. }
  79. else
  80. {
  81. v = svm.svm_predict(model,x);
  82. output.writeBytes(v+"\n");
  83. }
  84. if(v == target)
  85. ++correct;
  86. error += (v-target)*(v-target);
  87. sumv += v;
  88. sumy += target;
  89. sumvv += v*v;
  90. sumyy += target*target;
  91. sumvy += v*target;
  92. ++total;
  93. }
  94. if(svm_type == svm_parameter.EPSILON_SVR ||
  95. svm_type == svm_parameter.NU_SVR)
  96. {
  97. svm_predict.info("Mean squared error = "+error/total+" (regression)\n");
  98. svm_predict.info("Squared correlation coefficient = "+
  99. ((total*sumvy-sumv*sumy)*(total*sumvy-sumv*sumy))/
  100. ((total*sumvv-sumv*sumv)*(total*sumyy-sumy*sumy))+
  101. " (regression)\n");
  102. }
  103. else
  104. svm_predict.info("Accuracy = "+(double)correct/total*100+
  105. "% ("+correct+"/"+total+") (classification)\n");
  106. }
  107. private static void exit_with_help()
  108. {
  109. System.err.print("usage: svm_predict [options] test_file model_file output_file\n"
  110. +"options:\n"
  111. +"-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet\n"
  112. +"-q : quiet mode (no outputs)\n");
  113. System.exit(1);
  114. }
  115. public static void main(String argv[]) throws IOException
  116. {
  117. int i, predict_probability=0;
  118. svm_print_string = svm_print_stdout;
  119. // parse options
  120. for(i=0;i<argv.length;i++)
  121. {
  122. if(argv[i].charAt(0) != '-') break;
  123. ++i;
  124. switch(argv[i-1].charAt(1))
  125. {
  126. case 'b':
  127. predict_probability = atoi(argv[i]);
  128. break;
  129. case 'q':
  130. svm_print_string = svm_print_null;
  131. i--;
  132. break;
  133. default:
  134. System.err.print("Unknown option: " + argv[i-1] + "\n");
  135. exit_with_help();
  136. }
  137. }
  138. if(i>=argv.length-2)
  139. exit_with_help();
  140. try
  141. {
  142. BufferedReader input = new BufferedReader(new FileReader(argv[i]));
  143. DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(argv[i+2])));
  144. svm_model model = svm.svm_load_model(argv[i+1]);
  145. if (model == null)
  146. {
  147. System.err.print("can't open model file "+argv[i+1]+"\n");
  148. System.exit(1);
  149. }
  150. if(predict_probability == 1)
  151. {
  152. if(svm.svm_check_probability_model(model)==0)
  153. {
  154. System.err.print("Model does not support probabiliy estimates\n");
  155. System.exit(1);
  156. }
  157. }
  158. else
  159. {
  160. if(svm.svm_check_probability_model(model)!=0)
  161. {
  162. svm_predict.info("Model supports probability estimates, but disabled in prediction.\n");
  163. }
  164. }
  165. predict(input,output,model,predict_probability);
  166. input.close();
  167. output.close();
  168. }
  169. catch(FileNotFoundException e)
  170. {
  171. exit_with_help();
  172. }
  173. catch(ArrayIndexOutOfBoundsException e)
  174. {
  175. exit_with_help();
  176. }
  177. }
  178. }

A Python package for graph kernels, graph edit distances and graph pre-image problem.