#include #include #include #include #include void exit_with_help() { printf( "Usage: svm-scale [options] data_filename\n" "options:\n" "-l lower : x scaling lower limit (default -1)\n" "-u upper : x scaling upper limit (default +1)\n" "-y y_lower y_upper : y scaling limits (default: no y scaling)\n" "-s save_filename : save scaling parameters to save_filename\n" "-r restore_filename : restore scaling parameters from restore_filename\n" ); exit(1); } char *line; int max_line_len = 1024; double lower=-1.0,upper=1.0,y_lower,y_upper; int y_scaling = 0; double *feature_max; double *feature_min; double y_max = -DBL_MAX; double y_min = DBL_MAX; int max_index; #define max(x,y) ((x>y)?x:y) #define min(x,y) ((x lower) || (y_scaling && !(y_upper > y_lower))) { fprintf(stderr,"inconsistent lower/upper specification\n"); exit(1); } if(argc != i+1) exit_with_help(); fp=fopen(argv[i],"r"); if(fp==NULL) { fprintf(stderr,"can't open file %s\n", argv[i]); exit(1); } line = (char *) malloc(max_line_len*sizeof(char)); #define SKIP_TARGET\ while(isspace(*p)) ++p;\ while(!isspace(*p)) ++p; #define SKIP_ELEMENT\ while(*p!=':') ++p;\ ++p;\ while(isspace(*p)) ++p;\ while(*p && !isspace(*p)) ++p; /* assumption: min index of attributes is 1 */ /* pass 1: find out max index of attributes */ max_index = 0; while(readline(fp)!=NULL) { char *p=line; SKIP_TARGET while(sscanf(p,"%d:%*f",&index)==1) { max_index = max(max_index, index); SKIP_ELEMENT } } feature_max = (double *)malloc((max_index+1)* sizeof(double)); feature_min = (double *)malloc((max_index+1)* sizeof(double)); if(feature_max == NULL || feature_min == NULL) { fprintf(stderr,"can't allocate enough memory\n"); exit(1); } for(i=0;i<=max_index;i++) { feature_max[i]=-DBL_MAX; feature_min[i]=DBL_MAX; } rewind(fp); /* pass 2: find out min/max value */ while(readline(fp)!=NULL) { char *p=line; int next_index=1; double target; double value; sscanf(p,"%lf",&target); y_max = max(y_max,target); y_min = min(y_min,target); SKIP_TARGET while(sscanf(p,"%d:%lf",&index,&value)==2) { for(i=next_index;i