8 public void print(String s) {}
13 public void print(String s)
21 static void info(String s)
23 svm_print_string.
print(s);
26 private static double atof(String s)
28 return Double.valueOf(s).doubleValue();
31 private static int atoi(String s)
33 return Integer.parseInt(s);
41 double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
43 int svm_type=
svm.svm_get_svm_type(
model);
44 int nr_class=
svm.svm_get_nr_class(
model);
45 double[] prob_estimates=null;
52 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");
56 int[] labels=
new int[nr_class];
58 prob_estimates =
new double[nr_class];
59 output.writeBytes(
"labels");
60 for(
int j=0;j<nr_class;j++)
61 output.writeBytes(
" "+labels[j]);
67 String
line = input.readLine();
68 if(line == null)
break;
70 StringTokenizer st =
new StringTokenizer(line,
" \t\n\r\f:");
72 double target = atof(st.nextToken());
73 int m = st.countTokens()/2;
78 x[j].
index = atoi(st.nextToken());
79 x[j].
value = atof(st.nextToken());
85 v =
svm.svm_predict_probability(
model,x,prob_estimates);
87 for(
int j=0;j<nr_class;j++)
88 output.writeBytes(prob_estimates[j]+
" ");
99 error += (v-target)*(v-target);
103 sumyy += target*target;
110 svm_predict.info(
"Mean squared error = "+error/total+
" (regression)\n");
111 svm_predict.info(
"Squared correlation coefficient = "+
112 ((total*sumvy-sumv*sumy)*(total*sumvy-sumv*sumy))/
113 ((total*sumvv-sumv*sumv)*(total*sumyy-sumy*sumy))+
117 svm_predict.info(
"Accuracy = "+(
double)correct/total*100+
118 "% ("+correct+
"/"+total+
") (classification)\n");
123 System.err.print(
"usage: svm_predict [options] test_file model_file output_file\n" 125 +
"-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet\n" 126 +
"-q : quiet mode (no outputs)\n");
130 public static void main(String argv[])
throws IOException
133 svm_print_string = svm_print_stdout;
136 for(i=0;i<argv.length;i++)
138 if(argv[i].charAt(0) !=
'-')
break;
140 switch(argv[i-1].charAt(1))
143 predict_probability = atoi(argv[i]);
146 svm_print_string = svm_print_null;
150 System.err.
print(
"Unknown option: " + argv[i-1] +
"\n");
158 BufferedReader input =
new BufferedReader(
new FileReader(argv[i]));
159 DataOutputStream
output =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(argv[i+2])));
161 if(predict_probability == 1)
163 if(
svm.svm_check_probability_model(model)==0)
165 System.err.print(
"Model does not support probabiliy estimates\n");
171 if(
svm.svm_check_probability_model(model)!=0)
173 svm_predict.info(
"Model supports probability estimates, but disabled in prediction.\n");
176 predict(input,output,model,predict_probability);
180 catch(FileNotFoundException e)
184 catch(ArrayIndexOutOfBoundsException e)
void predict(mxArray *plhs[], const mxArray *prhs[], struct svm_model *model, const int predict_probability)
ROSCONSOLE_DECL void print(FilterBase *filter, void *logger, Level level, const char *file, int line, const char *function, const char *fmt,...) ROSCONSOLE_PRINTF_ATTRIBUTE(7
def svm_predict(y, x, m, options="")
void output(int index, double value)
static void(* svm_print_string)(const char *)
int(* info)(const char *fmt,...)
int main(int argc, char **argv)
static final int EPSILON_SVR