#include #include #include #include #include "svm.h" char* line; int max_line_len = 1024; struct svm_node *x; int max_nr_attr = 64; struct svm_model* model; int predict_probability=0; void predict(FILE *input, FILE *output) { int correct = 0; int total = 0; double error = 0; double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; int svm_type=svm_get_svm_type(model); int nr_class=svm_get_nr_class(model); double *prob_estimates=NULL; int j; if(predict_probability) { if (svm_type==NU_SVR || svm_type==EPSILON_SVR) 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)); else { int *labels=(int *) malloc(nr_class*sizeof(int)); svm_get_labels(model,labels); prob_estimates = (double *) malloc(nr_class*sizeof(double)); fprintf(output,"labels"); for(j=0;j=max_nr_attr-1) // need one more for index = -1 { max_nr_attr *= 2; x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node)); } do { c = getc(input); if(c=='\n' || c==EOF) goto out2; } while(isspace(c)); ungetc(c,input); if (fscanf(input,"%d:%lf",&x[i].index,&x[i].value) < 2) { fprintf(stderr,"Wrong input format at line %d\n", total+1); exit(1); } ++i; } out2: x[i].index = -1; if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC)) { v = svm_predict_probability(model,x,prob_estimates); fprintf(output,"%g ",v); for(j=0;j=argc) exit_with_help(); input = fopen(argv[i],"r"); if(input == NULL) { fprintf(stderr,"can't open input file %s\n",argv[i]); exit(1); } output = fopen(argv[i+2],"w"); if(output == NULL) { fprintf(stderr,"can't open output file %s\n",argv[i+2]); exit(1); } if((model=svm_load_model(argv[i+1]))==0) { fprintf(stderr,"can't open model file %s\n",argv[i+1]); exit(1); } line = (char *) malloc(max_line_len*sizeof(char)); x = (struct svm_node *) malloc(max_nr_attr*sizeof(struct svm_node)); if(predict_probability) { if(svm_check_probability_model(model)==0) { fprintf(stderr,"Model does not support probabiliy estimates\n"); exit(1); } } else { if(svm_check_probability_model(model)!=0) printf("Model supports probability estimates, but disabled in prediction.\n"); } predict(input,output); svm_destroy_model(model); free(line); free(x); fclose(input); fclose(output); return 0; }