annotate toolboxes/SVM-light/src/svm_classify.c @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 /***********************************************************************/
Daniel@0 2 /* */
Daniel@0 3 /* svm_classify.c */
Daniel@0 4 /* */
Daniel@0 5 /* Classification module of Support Vector Machine. */
Daniel@0 6 /* */
Daniel@0 7 /* Author: Thorsten Joachims */
Daniel@0 8 /* Date: 02.07.02 */
Daniel@0 9 /* */
Daniel@0 10 /* Copyright (c) 2002 Thorsten Joachims - All rights reserved */
Daniel@0 11 /* */
Daniel@0 12 /* This software is available for non-commercial use only. It must */
Daniel@0 13 /* not be modified and distributed without prior permission of the */
Daniel@0 14 /* author. The author is not responsible for implications from the */
Daniel@0 15 /* use of this software. */
Daniel@0 16 /* */
Daniel@0 17 /************************************************************************/
Daniel@0 18
Daniel@0 19 # include "svm_common.h"
Daniel@0 20
Daniel@0 21 char docfile[200];
Daniel@0 22 char modelfile[200];
Daniel@0 23 char predictionsfile[200];
Daniel@0 24
Daniel@0 25 void read_input_parameters(int, char **, char *, char *, char *, long *,
Daniel@0 26 long *);
Daniel@0 27 void print_help(void);
Daniel@0 28
Daniel@0 29
Daniel@0 30 int main (int argc, char* argv[])
Daniel@0 31 {
Daniel@0 32 DOC *doc; /* test example */
Daniel@0 33 WORD *words;
Daniel@0 34 long max_docs,max_words_doc,lld;
Daniel@0 35 long totdoc=0,queryid,slackid;
Daniel@0 36 long correct=0,incorrect=0,no_accuracy=0;
Daniel@0 37 long res_a=0,res_b=0,res_c=0,res_d=0,wnum,pred_format;
Daniel@0 38 long j;
Daniel@0 39 double t1,runtime=0;
Daniel@0 40 double dist,doc_label,costfactor;
Daniel@0 41 char *line,*comment;
Daniel@0 42 FILE *predfl,*docfl;
Daniel@0 43 MODEL *model;
Daniel@0 44
Daniel@0 45 read_input_parameters(argc,argv,docfile,modelfile,predictionsfile,
Daniel@0 46 &verbosity,&pred_format);
Daniel@0 47
Daniel@0 48 nol_ll(docfile,&max_docs,&max_words_doc,&lld); /* scan size of input file */
Daniel@0 49 max_words_doc+=2;
Daniel@0 50 lld+=2;
Daniel@0 51
Daniel@0 52 line = (char *)my_malloc(sizeof(char)*lld);
Daniel@0 53 words = (WORD *)my_malloc(sizeof(WORD)*(max_words_doc+10));
Daniel@0 54
Daniel@0 55 model=read_model(modelfile);
Daniel@0 56
Daniel@0 57 if(model->kernel_parm.kernel_type == 0) { /* linear kernel */
Daniel@0 58 /* compute weight vector */
Daniel@0 59 add_weight_vector_to_linear_model(model);
Daniel@0 60 }
Daniel@0 61
Daniel@0 62 if(verbosity>=2) {
Daniel@0 63 printf("Classifying test examples.."); fflush(stdout);
Daniel@0 64 }
Daniel@0 65
Daniel@0 66 if ((docfl = fopen (docfile, "r")) == NULL)
Daniel@0 67 { perror (docfile); exit (1); }
Daniel@0 68 if ((predfl = fopen (predictionsfile, "w")) == NULL)
Daniel@0 69 { perror (predictionsfile); exit (1); }
Daniel@0 70
Daniel@0 71 while((!feof(docfl)) && fgets(line,(int)lld,docfl)) {
Daniel@0 72 if(line[0] == '#') continue; /* line contains comments */
Daniel@0 73 parse_document(line,words,&doc_label,&queryid,&slackid,&costfactor,&wnum,
Daniel@0 74 max_words_doc,&comment);
Daniel@0 75 totdoc++;
Daniel@0 76 if(model->kernel_parm.kernel_type == 0) { /* linear kernel */
Daniel@0 77 for(j=0;(words[j]).wnum != 0;j++) { /* Check if feature numbers */
Daniel@0 78 if((words[j]).wnum>model->totwords) /* are not larger than in */
Daniel@0 79 (words[j]).wnum=0; /* model. Remove feature if */
Daniel@0 80 } /* necessary. */
Daniel@0 81 doc = create_example(-1,0,0,0.0,create_svector(words,comment,1.0));
Daniel@0 82 t1=get_runtime();
Daniel@0 83 dist=classify_example_linear(model,doc);
Daniel@0 84 runtime+=(get_runtime()-t1);
Daniel@0 85 free_example(doc,1);
Daniel@0 86 }
Daniel@0 87 else { /* non-linear kernel */
Daniel@0 88 doc = create_example(-1,0,0,0.0,create_svector(words,comment,1.0));
Daniel@0 89 t1=get_runtime();
Daniel@0 90 dist=classify_example(model,doc);
Daniel@0 91 runtime+=(get_runtime()-t1);
Daniel@0 92 free_example(doc,1);
Daniel@0 93 }
Daniel@0 94 if(dist>0) {
Daniel@0 95 if(pred_format==0) { /* old weired output format */
Daniel@0 96 fprintf(predfl,"%.8g:+1 %.8g:-1\n",dist,-dist);
Daniel@0 97 }
Daniel@0 98 if(doc_label>0) correct++; else incorrect++;
Daniel@0 99 if(doc_label>0) res_a++; else res_b++;
Daniel@0 100 }
Daniel@0 101 else {
Daniel@0 102 if(pred_format==0) { /* old weired output format */
Daniel@0 103 fprintf(predfl,"%.8g:-1 %.8g:+1\n",-dist,dist);
Daniel@0 104 }
Daniel@0 105 if(doc_label<0) correct++; else incorrect++;
Daniel@0 106 if(doc_label>0) res_c++; else res_d++;
Daniel@0 107 }
Daniel@0 108 if(pred_format==1) { /* output the value of decision function */
Daniel@0 109 fprintf(predfl,"%.8g\n",dist);
Daniel@0 110 }
Daniel@0 111 if((int)(0.01+(doc_label*doc_label)) != 1)
Daniel@0 112 { no_accuracy=1; } /* test data is not binary labeled */
Daniel@0 113 if(verbosity>=2) {
Daniel@0 114 if(totdoc % 100 == 0) {
Daniel@0 115 printf("%ld..",totdoc); fflush(stdout);
Daniel@0 116 }
Daniel@0 117 }
Daniel@0 118 }
Daniel@0 119 free(line);
Daniel@0 120 free(words);
Daniel@0 121 free_model(model,1);
Daniel@0 122
Daniel@0 123 if(verbosity>=2) {
Daniel@0 124 printf("done\n");
Daniel@0 125
Daniel@0 126 /* Note by Gary Boone Date: 29 April 2000 */
Daniel@0 127 /* o Timing is inaccurate. The timer has 0.01 second resolution. */
Daniel@0 128 /* Because classification of a single vector takes less than */
Daniel@0 129 /* 0.01 secs, the timer was underflowing. */
Daniel@0 130 printf("Runtime (without IO) in cpu-seconds: %.2f\n",
Daniel@0 131 (float)(runtime/100.0));
Daniel@0 132
Daniel@0 133 }
Daniel@0 134 if((!no_accuracy) && (verbosity>=1)) {
Daniel@0 135 printf("Accuracy on test set: %.2f%% (%ld correct, %ld incorrect, %ld total)\n",(float)(correct)*100.0/totdoc,correct,incorrect,totdoc);
Daniel@0 136 printf("Precision/recall on test set: %.2f%%/%.2f%%\n",(float)(res_a)*100.0/(res_a+res_b),(float)(res_a)*100.0/(res_a+res_c));
Daniel@0 137 }
Daniel@0 138
Daniel@0 139 return(0);
Daniel@0 140 }
Daniel@0 141
Daniel@0 142 void read_input_parameters(int argc, char **argv, char *docfile,
Daniel@0 143 char *modelfile, char *predictionsfile,
Daniel@0 144 long int *verbosity, long int *pred_format)
Daniel@0 145 {
Daniel@0 146 long i;
Daniel@0 147
Daniel@0 148 /* set default */
Daniel@0 149 strcpy (modelfile, "svm_model");
Daniel@0 150 strcpy (predictionsfile, "svm_predictions");
Daniel@0 151 (*verbosity)=2;
Daniel@0 152 (*pred_format)=1;
Daniel@0 153
Daniel@0 154 for(i=1;(i<argc) && ((argv[i])[0] == '-');i++) {
Daniel@0 155 switch ((argv[i])[1])
Daniel@0 156 {
Daniel@0 157 case 'h': print_help(); exit(0);
Daniel@0 158 case 'v': i++; (*verbosity)=atol(argv[i]); break;
Daniel@0 159 case 'f': i++; (*pred_format)=atol(argv[i]); break;
Daniel@0 160 default: printf("\nUnrecognized option %s!\n\n",argv[i]);
Daniel@0 161 print_help();
Daniel@0 162 exit(0);
Daniel@0 163 }
Daniel@0 164 }
Daniel@0 165 if((i+1)>=argc) {
Daniel@0 166 printf("\nNot enough input parameters!\n\n");
Daniel@0 167 print_help();
Daniel@0 168 exit(0);
Daniel@0 169 }
Daniel@0 170 strcpy (docfile, argv[i]);
Daniel@0 171 strcpy (modelfile, argv[i+1]);
Daniel@0 172 if((i+2)<argc) {
Daniel@0 173 strcpy (predictionsfile, argv[i+2]);
Daniel@0 174 }
Daniel@0 175 if(((*pred_format) != 0) && ((*pred_format) != 1)) {
Daniel@0 176 printf("\nOutput format can only take the values 0 or 1!\n\n");
Daniel@0 177 print_help();
Daniel@0 178 exit(0);
Daniel@0 179 }
Daniel@0 180 }
Daniel@0 181
Daniel@0 182 void print_help(void)
Daniel@0 183 {
Daniel@0 184 printf("\nSVM-light %s: Support Vector Machine, classification module %s\n",VERSION,VERSION_DATE);
Daniel@0 185 copyright_notice();
Daniel@0 186 printf(" usage: svm_classify [options] example_file model_file output_file\n\n");
Daniel@0 187 printf("options: -h -> this help\n");
Daniel@0 188 printf(" -v [0..3] -> verbosity level (default 2)\n");
Daniel@0 189 printf(" -f [0,1] -> 0: old output format of V1.0\n");
Daniel@0 190 printf(" -> 1: output the value of decision function (default)\n\n");
Daniel@0 191 }
Daniel@0 192
Daniel@0 193
Daniel@0 194
Daniel@0 195