1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
#define INF HUGE_VAL
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);
}
static float strtof_or_exit(const char *str) {
errno = 0;
char *endptr;
float val = strtof(str, &endptr);
if ((errno == ERANGE && (val == HUGE_VALF || val == HUGE_VALL)) || (errno != 0 && val == 0.0)) {
perror("error converting %s to float:");
exit_with_help();
} else if (*endptr != '\0') {
exit_with_help();
}
return val;
}
char *line = NULL;
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;
int min_index;
long int num_nonzeros = 0;
long int new_num_nonzeros = 0;
#define max(x,y) (((x)>(y))?(x):(y))
#define min(x,y) (((x)<(y))?(x):(y))
void output_target(double value);
void output(int index, double value);
char* readline(FILE *input);
int clean_up(FILE *fp_restore, FILE *fp, const char *msg);
int main(int argc,char **argv)
{
int i,index;
FILE *fp, *fp_restore = NULL;
char *save_filename = NULL;
char *restore_filename = NULL;
for(i=1;i<argc;i++)
{
if(argv[i][0] != '-') break;
++i;
switch(argv[i-1][1])
{
case 'l': lower = strtof_or_exit(argv[i]); break;
case 'u': upper = strtof_or_exit(argv[i]); break;
case 'y':
y_lower = strtof_or_exit(argv[i]);
++i;
y_upper = strtof_or_exit(argv[i]);
y_scaling = 1;
break;
case 's': save_filename = argv[i]; break;
case 'r': restore_filename = argv[i]; break;
default:
fprintf(stderr,"unknown option\n");
exit_with_help();
}
}
if(!(upper > lower) || (y_scaling && !(y_upper > y_lower)))
{
fprintf(stderr,"inconsistent lower/upper specification\n");
exit(1);
}
if(restore_filename && save_filename)
{
fprintf(stderr,"cannot use -r and -s simultaneously\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;
min_index = 1;
if(restore_filename)
{
int idx, c;
fp_restore = fopen(restore_filename,"r");
if(fp_restore==NULL)
{
fprintf(stderr,"can't open file %s\n", restore_filename);
exit(1);
}
c = fgetc(fp_restore);
if(c == 'y')
{
readline(fp_restore);
readline(fp_restore);
readline(fp_restore);
}
readline(fp_restore);
readline(fp_restore);
while(fscanf(fp_restore,"%d %*f %*f\n",&idx) == 1)
max_index = max(idx,max_index);
rewind(fp_restore);
}
while(readline(fp)!=NULL)
{
char *p=line;
SKIP_TARGET
while(sscanf(p,"%d:%*f",&index)==1)
{
max_index = max(max_index, index);
min_index = min(min_index, index);
SKIP_ELEMENT
num_nonzeros++;
}
}
if(min_index < 1)
fprintf(stderr,
"WARNING: minimal feature index is %d, but indices should start from 1\n", min_index);
rewind(fp);
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;
}
/* pass 2: find out min/max value */
while(readline(fp)!=NULL)
{
char *p=line;
int next_index=1;
double target;
double value;
if (sscanf(p,"%lf",&target) != 1)
return clean_up(fp_restore, fp, "ERROR: failed to read labels\n");
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<index;i++)
{
feature_max[i]=max(feature_max[i],0);
feature_min[i]=min(feature_min[i],0);
}
feature_max[index]=max(feature_max[index],value);
feature_min[index]=min(feature_min[index],value);
SKIP_ELEMENT
next_index=index+1;
}
for(i=next_index;i<=max_index;i++)
{
feature_max[i]=max(feature_max[i],0);
feature_min[i]=min(feature_min[i],0);
}
}
rewind(fp);
/* pass 2.5: save/restore feature_min/feature_max */
if(restore_filename)
{
/* fp_restore rewinded in finding max_index */
int idx, c;
double fmin, fmax;
int next_index = 1;
if((c = fgetc(fp_restore)) == 'y')
{
if(fscanf(fp_restore, "%lf %lf\n", &y_lower, &y_upper) != 2 ||
fscanf(fp_restore, "%lf %lf\n", &y_min, &y_max) != 2)
return clean_up(fp_restore, fp, "ERROR: failed to read scaling parameters\n");
y_scaling = 1;
}
else
ungetc(c, fp_restore);
if (fgetc(fp_restore) == 'x')
{
if(fscanf(fp_restore, "%lf %lf\n", &lower, &upper) != 2)
return clean_up(fp_restore, fp, "ERROR: failed to read scaling parameters\n");
while(fscanf(fp_restore,"%d %lf %lf\n",&idx,&fmin,&fmax)==3)
{
for(i = next_index;i<idx;i++)
if(feature_min[i] != feature_max[i])
fprintf(stderr,
"WARNING: feature index %d appeared in file %s was not seen in the scaling factor file %s.\n",
i, argv[argc-1], restore_filename);
feature_min[idx] = fmin;
feature_max[idx] = fmax;
next_index = idx + 1;
}
for(i=next_index;i<=max_index;i++)
if(feature_min[i] != feature_max[i])
fprintf(stderr,
"WARNING: feature index %d appeared in file %s was not seen in the scaling factor file %s.\n",
i, argv[argc-1], restore_filename);
}
fclose(fp_restore);
}
if(save_filename)
{
FILE *fp_save = fopen(save_filename,"w");
if(fp_save==NULL)
{
fprintf(stderr,"can't open file %s\n", save_filename);
exit(1);
}
if(y_scaling)
{
fprintf(fp_save, "y\n");
fprintf(fp_save, "%.16g %.16g\n", y_lower, y_upper);
fprintf(fp_save, "%.16g %.16g\n", y_min, y_max);
}
fprintf(fp_save, "x\n");
fprintf(fp_save, "%.16g %.16g\n", lower, upper);
for(i=1;i<=max_index;i++)
{
if(feature_min[i]!=feature_max[i])
fprintf(fp_save,"%d %.16g %.16g\n",i,feature_min[i],feature_max[i]);
}
if(min_index < 1)
fprintf(stderr,
"WARNING: scaling factors with indices smaller than 1 are not stored to the file %s.\n", save_filename);
fclose(fp_save);
}
/* pass 3: scale */
while(readline(fp)!=NULL)
{
char *p=line;
int next_index=1;
double target;
double value;
if (sscanf(p,"%lf",&target) != 1)
return clean_up(NULL, fp, "ERROR: failed to read labels\n");
output_target(target);
SKIP_TARGET
while(sscanf(p,"%d:%lf",&index,&value)==2)
{
for(i=next_index;i<index;i++)
output(i,0);
output(index,value);
SKIP_ELEMENT
next_index=index+1;
}
for(i=next_index;i<=max_index;i++)
output(i,0);
printf("\n");
}
if (new_num_nonzeros > num_nonzeros)
fprintf(stderr,
"WARNING: original #nonzeros %ld\n"
" > new #nonzeros %ld\n"
"If feature values are non-negative and sparse, use -l 0 rather than the default -l -1\n",
num_nonzeros, new_num_nonzeros);
free(line);
free(feature_max);
free(feature_min);
fclose(fp);
return 0;
}
char* readline(FILE *input)
{
int len;
if(fgets(line,max_line_len,input) == NULL)
return NULL;
while(strrchr(line,'\n') == NULL)
{
max_line_len *= 2;
line = (char *) realloc(line, max_line_len);
len = (int) strlen(line);
if(fgets(line+len,max_line_len-len,input) == NULL)
break;
}
return line;
}
void output_target(double value)
{
if(y_scaling)
{
if(value == y_min)
value = y_lower;
else if(value == y_max)
value = y_upper;
else value = y_lower + (y_upper-y_lower) *
(value - y_min)/(y_max-y_min);
}
printf("%g ",value);
}
void output(int index, double value)
{
/* skip single-valued attribute */
if(feature_max[index] == feature_min[index])
return;
if(value == feature_min[index])
value = lower;
else if(value == feature_max[index])
value = upper;
else
value = lower + (upper-lower) *
(value-feature_min[index])/
(feature_max[index]-feature_min[index]);
if(value != 0)
{
printf("%d:%g ",index, value);
new_num_nonzeros++;
}
}
int clean_up(FILE *fp_restore, FILE *fp, const char* msg)
{
fprintf(stderr, "%s", msg);
free(line);
free(feature_max);
free(feature_min);
fclose(fp);
if (fp_restore)
fclose(fp_restore);
return -1;
}
|