00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "Component/ModelManager.H"
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <string.h>
00043 #include <ctype.h>
00044 #include "svm.h"
00045
00046 void read_problem(const char *filename,
00047 struct svm_problem &prob,
00048 struct svm_parameter ¶m)
00049 {
00050 int elements, max_index, i, j;
00051 FILE *fp = fopen(filename,"r");
00052
00053 if(fp == NULL)
00054 {
00055 fprintf(stderr,"can't open input file %s\n",filename);
00056 exit(1);
00057 }
00058
00059 prob.l = 0;
00060 elements = 0;
00061 while(1)
00062 {
00063 int c = fgetc(fp);
00064 switch(c)
00065 {
00066 case '\n':
00067 ++prob.l;
00068
00069
00070 case ':':
00071 ++elements;
00072 break;
00073 case EOF:
00074 goto out;
00075 default:
00076 ;
00077 }
00078 }
00079 out:
00080 rewind(fp);
00081
00082 prob.y = new double[prob.l];
00083 prob.x = new struct svm_node *[prob.l];
00084 struct svm_node *x_space = new struct svm_node[elements];
00085
00086 max_index = 0;
00087 j=0;
00088 for(i=0;i<prob.l;i++)
00089 {
00090 double label;
00091 prob.x[i] = &x_space[j];
00092 fscanf(fp,"%lf",&label);
00093 prob.y[i] = label;
00094
00095 while(1)
00096 {
00097 int c;
00098 do {
00099 c = getc(fp);
00100 if(c=='\n') goto out2;
00101 } while(isspace(c));
00102 ungetc(c,fp);
00103 if (fscanf(fp,"%d:%lf",&(x_space[j].index),&(x_space[j].value)) < 2)
00104 {
00105 fprintf(stderr,"Wrong input format at line %d\n", i+1);
00106 exit(1);
00107 }
00108 ++j;
00109 }
00110 out2:
00111 if(j>=1 && x_space[j-1].index > max_index)
00112 max_index = x_space[j-1].index;
00113 x_space[j++].index = -1;
00114 }
00115
00116 if(param.gamma == 0)
00117 param.gamma = 1.0/max_index;
00118
00119 if(param.kernel_type == PRECOMPUTED)
00120 for(i=0;i<prob.l;i++)
00121 {
00122 if (prob.x[i][0].index != 0)
00123 {
00124 fprintf(stderr,"Wrong input format: first column must be 0:sample_serial_number\n");
00125 exit(1);
00126 }
00127 if ((int)prob.x[i][0].value <= 0 || (int)prob.x[i][0].value > max_index)
00128 {
00129 fprintf(stderr,"Wrong input format: sample_serial_number out of range\n");
00130 exit(1);
00131 }
00132 }
00133
00134 fclose(fp);
00135 }
00136
00137 void predict(const char* filename, struct svm_model *model)
00138 {
00139 int correct = 0;
00140 int total = 0;
00141 double error = 0;
00142 double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
00143 int max_nr_attr = 64;
00144 struct svm_node *x = new struct svm_node[max_nr_attr];
00145
00146 FILE *input = fopen(filename, "r");
00147 if(input == NULL)
00148 {
00149 fprintf(stderr,"can't open testing file %s\n",filename);
00150 exit(1);
00151 }
00152 while(1)
00153 {
00154 int i = 0;
00155 int c;
00156 double target,v;
00157
00158 if (fscanf(input,"%lf",&target)==EOF)
00159 break;
00160
00161 while(1)
00162 {
00163 if(i>=max_nr_attr-1)
00164 {
00165 max_nr_attr *= 2;
00166 x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
00167 }
00168
00169 do {
00170 c = getc(input);
00171 if(c=='\n' || c==EOF) goto out2;
00172 } while(isspace(c));
00173 ungetc(c,input);
00174 if (fscanf(input,"%d:%lf",&x[i].index,&x[i].value) < 2)
00175 {
00176 fprintf(stderr,"Wrong input format at line %d\n", total+1);
00177 exit(1);
00178 }
00179 ++i;
00180 }
00181
00182 out2:
00183 x[i].index = -1;
00184
00185 v = svm_predict(model,x);
00186
00187 if(v == target)
00188 ++correct;
00189 error += (v-target)*(v-target);
00190 sumv += v;
00191 sumy += target;
00192 sumvv += v*v;
00193 sumyy += target*target;
00194 sumvy += v*target;
00195 ++total;
00196 }
00197 printf("Accuracy = %g%% (%d/%d) (classification)\n",
00198 (double)correct/total*100,correct,total);
00199 }
00200
00201
00202 int main(int argc, char** argv)
00203 {
00204 struct svm_parameter param;
00205 struct svm_problem prob;
00206
00207
00208 ModelManager manager("Test SVM");
00209
00210
00211 if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 0) == false)
00212 return(1);
00213
00214 manager.start();
00215
00216
00217 param.svm_type = C_SVC;
00218 param.kernel_type = RBF;
00219 param.degree = 3;
00220 param.gamma = 0;
00221 param.coef0 = 0;
00222 param.nu = 0.5;
00223 param.cache_size = 100;
00224 param.C = 1;
00225 param.eps = 1e-3;
00226 param.p = 0.1;
00227 param.shrinking = 1;
00228 param.probability = 0;
00229 param.nr_weight = 0;
00230 param.weight_label = NULL;
00231 param.weight = NULL;
00232
00233
00234 read_problem("tests/train.1.scale", prob, param);
00235
00236 struct svm_model *model = svm_train(&prob,¶m);
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 LINFO("Predicting");
00249 predict("tests/test.1.scale", model);
00250 LINFO("Done");
00251
00252 svm_destroy_param(¶m);
00253 free(prob.y);
00254 free(prob.x);
00255
00256
00257
00258
00259 manager.stop();
00260
00261
00262 return 0;
00263 }
00264
00265
00266
00267
00268
00269