svm-predict.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <errno.h>
6 #include "svm.h"
7 
8 struct svm_node *x;
9 int max_nr_attr = 64;
10 
11 struct svm_model* model;
13 
14 static char *line = NULL;
15 static int max_line_len;
16 
17 static char* readline(FILE *input)
18 {
19  int len;
20 
21  if(fgets(line,max_line_len,input) == NULL)
22  return NULL;
23 
24  while(strrchr(line,'\n') == NULL)
25  {
26  max_line_len *= 2;
27  line = (char *) realloc(line,max_line_len);
28  len = (int) strlen(line);
29  if(fgets(line+len,max_line_len-len,input) == NULL)
30  break;
31  }
32  return line;
33 }
34 
35 void exit_input_error(int line_num)
36 {
37  fprintf(stderr,"Wrong input format at line %d\n", line_num);
38  exit(1);
39 }
40 
41 void predict(FILE *input, FILE *output)
42 {
43  int correct = 0;
44  int total = 0;
45  double error = 0;
46  double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0;
47 
48  int svm_type=svm_get_svm_type(model);
49  int nr_class=svm_get_nr_class(model);
50  double *prob_estimates=NULL;
51  int j;
52 
54  {
55  if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
56  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));
57  else
58  {
59  int *labels=(int *) malloc(nr_class*sizeof(int));
60  svm_get_labels(model,labels);
61  prob_estimates = (double *) malloc(nr_class*sizeof(double));
62  fprintf(output,"labels");
63  for(j=0;j<nr_class;j++)
64  fprintf(output," %d",labels[j]);
65  fprintf(output,"\n");
66  free(labels);
67  }
68  }
69 
70  max_line_len = 1024;
71  line = (char *)malloc(max_line_len*sizeof(char));
72  while(readline(input) != NULL)
73  {
74  int i = 0;
75  double target_label, predict_label;
76  char *idx, *val, *label, *endptr;
77  int inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0
78 
79  label = strtok(line," \t\n");
80  if(label == NULL) // empty line
81  exit_input_error(total+1);
82 
83  target_label = strtod(label,&endptr);
84  if(endptr == label || *endptr != '\0')
85  exit_input_error(total+1);
86 
87  while(1)
88  {
89  if(i>=max_nr_attr-1) // need one more for index = -1
90  {
91  max_nr_attr *= 2;
92  x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
93  }
94 
95  idx = strtok(NULL,":");
96  val = strtok(NULL," \t");
97 
98  if(val == NULL)
99  break;
100  errno = 0;
101  x[i].index = (int) strtol(idx,&endptr,10);
102  if(endptr == idx || errno != 0 || *endptr != '\0' || x[i].index <= inst_max_index)
103  exit_input_error(total+1);
104  else
105  inst_max_index = x[i].index;
106 
107  errno = 0;
108  x[i].value = strtod(val,&endptr);
109  if(endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr)))
110  exit_input_error(total+1);
111 
112  ++i;
113  }
114  x[i].index = -1;
115 
116  if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC))
117  {
118  predict_label = svm_predict_probability(model,x,prob_estimates);
119  fprintf(output,"%g",predict_label);
120  for(j=0;j<nr_class;j++)
121  fprintf(output," %g",prob_estimates[j]);
122  fprintf(output,"\n");
123  }
124  else
125  {
126  predict_label = svm_predict(model,x);
127  fprintf(output,"%g\n",predict_label);
128  }
129 
130  if(predict_label == target_label)
131  ++correct;
132  error += (predict_label-target_label)*(predict_label-target_label);
133  sump += predict_label;
134  sumt += target_label;
135  sumpp += predict_label*predict_label;
136  sumtt += target_label*target_label;
137  sumpt += predict_label*target_label;
138  ++total;
139  }
140  if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
141  {
142  printf("Mean squared error = %g (regression)\n",error/total);
143  printf("Squared correlation coefficient = %g (regression)\n",
144  ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/
145  ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt))
146  );
147  }
148  else
149  printf("Accuracy = %g%% (%d/%d) (classification)\n",
150  (double)correct/total*100,correct,total);
152  free(prob_estimates);
153 }
154 
156 {
157  printf(
158  "Usage: svm-predict [options] test_file model_file output_file\n"
159  "options:\n"
160  "-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); for one-class SVM only 0 is supported\n"
161  );
162  exit(1);
163 }
164 
165 int main(int argc, char **argv)
166 {
167  FILE *input, *output;
168  int i;
169 
170  // parse options
171  for(i=1;i<argc;i++)
172  {
173  if(argv[i][0] != '-') break;
174  ++i;
175  switch(argv[i-1][1])
176  {
177  case 'b':
178  predict_probability = atoi(argv[i]);
179  break;
180  default:
181  fprintf(stderr,"Unknown option: -%c\n", argv[i-1][1]);
182  exit_with_help();
183  }
184  }
185  if(i>=argc-2)
186  exit_with_help();
187 
188  input = fopen(argv[i],"r");
189  if(input == NULL)
190  {
191  fprintf(stderr,"can't open input file %s\n",argv[i]);
192  exit(1);
193  }
194 
195  output = fopen(argv[i+2],"w");
196  if(output == NULL)
197  {
198  fprintf(stderr,"can't open output file %s\n",argv[i+2]);
199  exit(1);
200  }
201 
202  if((model=svm_load_model(argv[i+1]))==0)
203  {
204  fprintf(stderr,"can't open model file %s\n",argv[i+1]);
205  exit(1);
206  }
207 
208  x = (struct svm_node *) malloc(max_nr_attr*sizeof(struct svm_node));
210  {
211  if(svm_check_probability_model(model)==0)
212  {
213  fprintf(stderr,"Model does not support probabiliy estimates\n");
214  exit(1);
215  }
216  }
217  else
218  {
219  if(svm_check_probability_model(model)!=0)
220  printf("Model supports probability estimates, but disabled in prediction.\n");
221  }
222  predict(input,output);
224  free(x);
225  free(line);
226  fclose(input);
227  fclose(output);
228  return 0;
229 }
static int max_line_len
Definition: svm-predict.c:15
void exit_with_help()
Definition: svm-predict.c:155
static char * readline(FILE *input)
Definition: svm-predict.c:17
double value
Definition: svm.h:15
def svm_load_model(model_file_name)
Definition: svmutil.py:27
void exit_input_error(int line_num)
Definition: svm-predict.c:35
Definition: svm.h:25
double svm_get_svr_probability(const svm_model *model)
Definition: svm.cpp:2447
int nr_class
Definition: svm.h:55
Definition: svm.h:52
def svm_predict(y, x, m, options="")
Definition: svmutil.py:164
struct svm_node * x
Definition: svm-predict.c:8
Definition: svm.h:25
int svm_get_nr_class(const svm_model *model)
Definition: svm.cpp:2435
int svm_check_probability_model(const svm_model *model)
Definition: svm.cpp:3098
void output(int index, double value)
Definition: svm-scale.c:333
index
Definition: subset.py:58
int main(int argc, char **argv)
Definition: svm-predict.c:165
static char * line
Definition: svm-predict.c:14
int predict_probability
Definition: svm-predict.c:12
struct svm_model * model
Definition: svm-predict.c:11
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
Definition: svm.cpp:2961
double svm_predict_probability(const svm_model *model, const svm_node *x, double *prob_estimates)
Definition: svm.cpp:2550
int index
Definition: svm.h:14
int max_nr_attr
Definition: svm-predict.c:9
int svm_get_svm_type(const svm_model *model)
Definition: svm.cpp:2430
Definition: svm.h:25
Definition: svm.h:12
void predict(FILE *input, FILE *output)
Definition: svm-predict.c:41
label
Definition: subset.py:57
void svm_get_labels(const svm_model *model, int *label)
Definition: svm.cpp:2440


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