10 "Usage: svm-scale [options] data_filename\n" 12 "-l lower : x scaling lower limit (default -1)\n" 13 "-u upper : x scaling upper limit (default +1)\n" 14 "-y y_lower y_upper : y scaling limits (default: no y scaling)\n" 15 "-s save_filename : save scaling parameters to save_filename\n" 16 "-r restore_filename : restore scaling parameters from restore_filename\n" 34 #define max(x,y) (((x)>(y))?(x):(y)) 35 #define min(x,y) (((x)<(y))?(x):(y)) 41 int main(
int argc,
char **argv)
44 FILE *fp, *fp_restore = NULL;
45 char *save_filename = NULL;
46 char *restore_filename = NULL;
48 for (i = 1; i < argc; i++)
50 if (argv[i][0] !=
'-')
break;
52 switch (argv[i - 1][1])
55 lower = atof(argv[i]);
58 upper = atof(argv[i]);
67 save_filename = argv[i];
70 restore_filename = argv[i];
73 fprintf(stderr,
"unknown option\n");
80 fprintf(stderr,
"inconsistent lower/upper specification\n");
84 if (restore_filename && save_filename)
86 fprintf(stderr,
"cannot use -r and -s simultaneously\n");
93 fp = fopen(argv[i],
"r");
97 fprintf(stderr,
"can't open file %s\n", argv[i]);
104 while(isspace(*p)) ++p;\ 105 while(!isspace(*p)) ++p; 107 #define SKIP_ELEMENT\ 110 while(isspace(*p)) ++p;\ 111 while(*p && !isspace(*p)) ++p; 118 if (restore_filename)
122 fp_restore = fopen(restore_filename,
"r");
123 if (fp_restore == NULL)
125 fprintf(stderr,
"can't open file %s\n", restore_filename);
129 c = fgetc(fp_restore);
139 while (fscanf(fp_restore,
"%d %*f %*f\n", &idx) == 1)
150 while (sscanf(p,
"%d:%*f", &index) == 1)
161 "WARNING: minimal feature index is %d, but indices should start from 1\n",
min_index);
170 fprintf(stderr,
"can't allocate enough memory\n");
188 sscanf(p,
"%lf", &target);
194 while (sscanf(p,
"%d:%lf", &index, &value) == 2)
196 for (i = next_index; i <
index; i++)
206 next_index = index + 1;
209 for (i = next_index; i <=
max_index; i++)
220 if (restore_filename)
227 if ((c = fgetc(fp_restore)) ==
'y')
230 fscanf(fp_restore,
"%lf %lf\n", &
y_min, &
y_max);
234 ungetc(c, fp_restore);
236 if (fgetc(fp_restore) ==
'x')
238 fscanf(fp_restore,
"%lf %lf\n", &
lower, &
upper);
239 while (fscanf(fp_restore,
"%d %lf %lf\n", &idx, &fmin, &fmax) == 3)
241 for (i = next_index; i < idx; i++)
244 "WARNING: feature index %d appeared in file %s was not seen in the scaling factor file %s.\n",
245 i, argv[argc - 1], restore_filename);
250 next_index = idx + 1;
253 for (i = next_index; i <=
max_index; i++)
256 "WARNING: feature index %d appeared in file %s was not seen in the scaling factor file %s.\n",
257 i, argv[argc - 1], restore_filename);
264 FILE *fp_save = fopen(save_filename,
"w");
267 fprintf(stderr,
"can't open file %s\n", save_filename);
272 fprintf(fp_save,
"y\n");
274 fprintf(fp_save,
"%.16g %.16g\n",
y_min,
y_max);
276 fprintf(fp_save,
"x\n");
277 fprintf(fp_save,
"%.16g %.16g\n",
lower,
upper);
286 "WARNING: scaling factors with indices smaller than 1 are not stored to the file %s.\n", save_filename);
299 sscanf(p,
"%lf", &target);
304 while (sscanf(p,
"%d:%lf", &index, &value) == 2)
306 for (i = next_index; i <
index; i++)
312 next_index = index + 1;
315 for (i = next_index; i <=
max_index; i++)
323 "WARNING: original #nonzeros %ld\n" 324 " new #nonzeros %ld\n" 325 "Use -l 0 if many original feature values are zeros\n",
342 while (strrchr(
line,
'\n') == NULL)
346 len = (int) strlen(
line);
359 else if (value ==
y_max)
364 printf(
"%g ", value);
384 printf(
"%d:%g ", index, value);
long int new_num_nonzeros
void output_target(double value)
char * readline(FILE *input)
void output(int index, double value)
int main(int argc, char **argv)