svm_predict.java
Go to the documentation of this file.
1 import libsvm.*;
2 import java.io.*;
3 import java.util.*;
4 
5 class svm_predict {
6  private static double atof(String s)
7  {
8  return Double.valueOf(s).doubleValue();
9  }
10 
11  private static int atoi(String s)
12  {
13  return Integer.parseInt(s);
14  }
15 
16  private static void predict(BufferedReader input, DataOutputStream output, svm_model model, int predict_probability) throws IOException
17  {
18  int correct = 0;
19  int total = 0;
20  double error = 0;
21  double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
22 
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;
26 
27  if(predict_probability == 1)
28  {
29  if(svm_type == svm_parameter.EPSILON_SVR ||
30  svm_type == svm_parameter.NU_SVR)
31  {
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");
33  }
34  else
35  {
36  int[] labels=new int[nr_class];
37  svm.svm_get_labels(model,labels);
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]);
42  output.writeBytes("\n");
43  }
44  }
45  while(true)
46  {
47  String line = input.readLine();
48  if(line == null) break;
49 
50  StringTokenizer st = new StringTokenizer(line," \t\n\r\f:");
51 
52  double target = atof(st.nextToken());
53  int m = st.countTokens()/2;
54  svm_node[] x = new svm_node[m];
55  for(int j=0;j<m;j++)
56  {
57  x[j] = new svm_node();
58  x[j].index = atoi(st.nextToken());
59  x[j].value = atof(st.nextToken());
60  }
61 
62  double v;
63  if (predict_probability==1 && (svm_type==svm_parameter.C_SVC || svm_type==svm_parameter.NU_SVC))
64  {
65  v = svm.svm_predict_probability(model,x,prob_estimates);
66  output.writeBytes(v+" ");
67  for(int j=0;j<nr_class;j++)
68  output.writeBytes(prob_estimates[j]+" ");
69  output.writeBytes("\n");
70  }
71  else
72  {
73  v = svm.svm_predict(model,x);
74  output.writeBytes(v+"\n");
75  }
76 
77  if(v == target)
78  ++correct;
79  error += (v-target)*(v-target);
80  sumv += v;
81  sumy += target;
82  sumvv += v*v;
83  sumyy += target*target;
84  sumvy += v*target;
85  ++total;
86  }
87  if(svm_type == svm_parameter.EPSILON_SVR ||
88  svm_type == svm_parameter.NU_SVR)
89  {
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))+
94  " (regression)\n");
95  }
96  else
97  System.out.print("Accuracy = "+(double)correct/total*100+
98  "% ("+correct+"/"+total+") (classification)\n");
99  }
100 
101  private static void exit_with_help()
102  {
103  System.err.print("usage: svm_predict [options] test_file model_file output_file\n"
104  +"options:\n"
105  +"-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet\n");
106  System.exit(1);
107  }
108 
109  public static void main(String argv[]) throws IOException
110  {
111  int i, predict_probability=0;
112 
113  // parse options
114  for(i=0;i<argv.length;i++)
115  {
116  if(argv[i].charAt(0) != '-') break;
117  ++i;
118  switch(argv[i-1].charAt(1))
119  {
120  case 'b':
121  predict_probability = atoi(argv[i]);
122  break;
123  default:
124  System.err.print("Unknown option: " + argv[i-1] + "\n");
125  exit_with_help();
126  }
127  }
128  if(i>=argv.length-2)
129  exit_with_help();
130  try
131  {
132  BufferedReader input = new BufferedReader(new FileReader(argv[i]));
133  DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(argv[i+2])));
134  svm_model model = svm.svm_load_model(argv[i+1]);
135  if(predict_probability == 1)
136  {
137  if(svm.svm_check_probability_model(model)==0)
138  {
139  System.err.print("Model does not support probabiliy estimates\n");
140  System.exit(1);
141  }
142  }
143  else
144  {
145  if(svm.svm_check_probability_model(model)!=0)
146  {
147  System.out.print("Model supports probability estimates, but disabled in prediction.\n");
148  }
149  }
150  predict(input,output,model,predict_probability);
151  input.close();
152  output.close();
153  }
154  catch(FileNotFoundException e)
155  {
156  exit_with_help();
157  }
158  catch(ArrayIndexOutOfBoundsException e)
159  {
160  exit_with_help();
161  }
162  }
163 }
static final int C_SVC
static final int NU_SVR
Definition: svm.py:1
void predict(mxArray *plhs[], const mxArray *prhs[], struct svm_model *model, const int predict_probability)
Definition: svmpredict.c:46
void exit_with_help()
Definition: libsvmread.c:21
Definition: svm.java:5
double value
Definition: svm_node.java:5
def svm_predict(y, x, m, options="")
Definition: svmutil.py:164
struct svm_node * x
Definition: svm-predict.c:8
void output(int index, double value)
Definition: svm-scale.c:333
int main(int argc, char **argv)
Definition: svm-predict.c:165
int predict_probability
Definition: svm-predict.c:12
char * line
Definition: svm-scale.c:21
struct svm_model * model
Definition: svmtrain.c:58
static final int EPSILON_SVR
static final int NU_SVC


haf_grasping
Author(s): David Fischinger
autogenerated on Mon Jun 10 2019 13:28:43