6 private static double atof(String s)
8 return Double.valueOf(s).doubleValue();
11 private static int atoi(String s)
13 return Integer.parseInt(s);
21 double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
23 int svm_type=
svm.svm_get_svm_type(
model);
24 int nr_class=
svm.svm_get_nr_class(
model);
25 double[] prob_estimates=null;
32 System.out.print(
"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");
36 int[] labels=
new int[nr_class];
38 prob_estimates =
new double[nr_class];
39 output.writeBytes(
"labels");
40 for(
int j=0;j<nr_class;j++)
41 output.writeBytes(
" "+labels[j]);
47 String
line = input.readLine();
48 if(line == null)
break;
50 StringTokenizer st =
new StringTokenizer(line,
" \t\n\r\f:");
52 double target = atof(st.nextToken());
53 int m = st.countTokens()/2;
58 x[j].
index = atoi(st.nextToken());
59 x[j].
value = atof(st.nextToken());
65 v =
svm.svm_predict_probability(
model,x,prob_estimates);
67 for(
int j=0;j<nr_class;j++)
68 output.writeBytes(prob_estimates[j]+
" ");
79 error += (v-target)*(v-target);
83 sumyy += target*target;
90 System.out.print(
"Mean squared error = "+error/total+
" (regression)\n");
91 System.out.print(
"Squared correlation coefficient = "+
92 ((total*sumvy-sumv*sumy)*(total*sumvy-sumv*sumy))/
93 ((total*sumvv-sumv*sumv)*(total*sumyy-sumy*sumy))+
97 System.out.print(
"Accuracy = "+(
double)correct/total*100+
98 "% ("+correct+
"/"+total+
") (classification)\n");
103 System.err.print(
"usage: svm_predict [options] test_file model_file output_file\n" 105 +
"-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet\n");
109 public static void main(String argv[])
throws IOException
114 for(i=0;i<argv.length;i++)
116 if(argv[i].charAt(0) !=
'-')
break;
118 switch(argv[i-1].charAt(1))
121 predict_probability = atoi(argv[i]);
124 System.err.print(
"Unknown option: " + argv[i-1] +
"\n");
132 BufferedReader input =
new BufferedReader(
new FileReader(argv[i]));
133 DataOutputStream
output =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(argv[i+2])));
135 if(predict_probability == 1)
137 if(
svm.svm_check_probability_model(model)==0)
139 System.err.print(
"Model does not support probabiliy estimates\n");
145 if(
svm.svm_check_probability_model(model)!=0)
147 System.out.print(
"Model supports probability estimates, but disabled in prediction.\n");
150 predict(input,output,model,predict_probability);
154 catch(FileNotFoundException e)
158 catch(ArrayIndexOutOfBoundsException e)
void predict(mxArray *plhs[], const mxArray *prhs[], struct svm_model *model, const int predict_probability)
def svm_predict(y, x, m, options="")
void output(int index, double value)
int main(int argc, char **argv)
static final int EPSILON_SVR