4 import java.text.DecimalFormat;
8 private String
line = null;
9 private double lower = -1.0;
10 private double upper = 1.0;
16 private double y_max = -Double.MAX_VALUE;
17 private double y_min = Double.MAX_VALUE;
25 "Usage: svm-scale [options] data_filename\n" 27 +
"-l lower : x scaling lower limit (default -1)\n" 28 +
"-u upper : x scaling upper limit (default +1)\n" 29 +
"-y y_lower y_upper : y scaling limits (default: no y scaling)\n" 30 +
"-s save_filename : save scaling parameters to save_filename\n" 31 +
"-r restore_filename : restore scaling parameters from restore_filename\n" 36 private BufferedReader rewind(BufferedReader fp, String filename)
throws IOException
39 return new BufferedReader(
new FileReader(filename));
48 else if(value == y_max)
51 value = y_lower + (y_upper-
y_lower) *
52 (value-y_min) / (y_max-
y_min);
55 System.out.print(value +
" ");
58 private void output(
int index,
double value)
61 if(feature_max[index] == feature_min[index])
64 if(value == feature_min[index])
66 else if(value == feature_max[index])
69 value = lower + (upper-
lower) *
70 (value-feature_min[index])/
71 (feature_max[index]-feature_min[index]);
75 System.out.print(index +
":" + value +
" ");
80 private String
readline(BufferedReader fp)
throws IOException
86 private void run(String []argv)
throws IOException
89 BufferedReader fp = null, fp_restore = null;
90 String save_filename = null;
91 String restore_filename = null;
92 String data_filename = null;
95 for(i=0;i<argv.length;i++)
97 if (argv[i].charAt(0) !=
'-')
break;
99 switch(argv[i-1].charAt(1))
101 case 'l': lower = Double.parseDouble(argv[i]);
break;
102 case 'u': upper = Double.parseDouble(argv[i]);
break;
104 y_lower = Double.parseDouble(argv[i]);
106 y_upper = Double.parseDouble(argv[i]);
109 case 's': save_filename = argv[i];
break;
110 case 'r': restore_filename = argv[i];
break;
112 System.err.println(
"unknown option");
117 if(!(upper > lower) || (y_scaling && !(y_upper > y_lower)))
119 System.err.println(
"inconsistent lower/upper specification");
122 if(restore_filename != null && save_filename != null)
124 System.err.println(
"cannot use -r and -s simultaneously");
128 if(argv.length != i+1)
131 data_filename = argv[i];
133 fp =
new BufferedReader(
new FileReader(data_filename));
134 }
catch (Exception e) {
135 System.err.println(
"can't open file " + data_filename);
143 if(restore_filename != null)
148 fp_restore =
new BufferedReader(
new FileReader(restore_filename));
150 catch (Exception e) {
151 System.err.println(
"can't open file " + restore_filename);
154 if((c = fp_restore.read()) ==
'y')
156 fp_restore.readLine();
157 fp_restore.readLine();
158 fp_restore.readLine();
160 fp_restore.readLine();
161 fp_restore.readLine();
163 String restore_line = null;
164 while((restore_line = fp_restore.readLine())!=null)
166 StringTokenizer st2 =
new StringTokenizer(restore_line);
167 idx = Integer.parseInt(st2.nextToken());
168 max_index = Math.max(max_index, idx);
170 fp_restore = rewind(fp_restore, restore_filename);
175 StringTokenizer st =
new StringTokenizer(line,
" \t\n\r\f:");
177 while(st.hasMoreTokens())
179 index = Integer.parseInt(st.nextToken());
180 max_index = Math.max(max_index, index);
187 feature_max =
new double[(max_index+1)];
188 feature_min =
new double[(max_index+1)];
189 }
catch(OutOfMemoryError e) {
190 System.err.println(
"can't allocate enough memory");
196 feature_max[i] = -Double.MAX_VALUE;
197 feature_min[i] = Double.MAX_VALUE;
200 fp = rewind(fp, data_filename);
209 StringTokenizer st =
new StringTokenizer(line,
" \t\n\r\f:");
210 target = Double.parseDouble(st.nextToken());
211 y_max = Math.max(y_max, target);
212 y_min = Math.min(y_min, target);
214 while (st.hasMoreTokens())
216 index = Integer.parseInt(st.nextToken());
217 value = Double.parseDouble(st.nextToken());
219 for (i = next_index; i<index; i++)
221 feature_max[i] = Math.max(feature_max[i], 0);
222 feature_min[i] = Math.min(feature_min[i], 0);
225 feature_max[index] = Math.max(feature_max[index], value);
226 feature_min[index] = Math.min(feature_min[index], value);
227 next_index = index + 1;
232 feature_max[i] = Math.max(feature_max[i], 0);
233 feature_min[i] = Math.min(feature_min[i], 0);
237 fp = rewind(fp, data_filename);
240 if(restore_filename != null)
247 if((c = fp_restore.read()) ==
'y')
249 fp_restore.readLine();
250 StringTokenizer st =
new StringTokenizer(fp_restore.readLine());
251 y_lower = Double.parseDouble(st.nextToken());
252 y_upper = Double.parseDouble(st.nextToken());
253 st =
new StringTokenizer(fp_restore.readLine());
254 y_min = Double.parseDouble(st.nextToken());
255 y_max = Double.parseDouble(st.nextToken());
261 if(fp_restore.read() ==
'x') {
262 fp_restore.readLine();
263 StringTokenizer st =
new StringTokenizer(fp_restore.readLine());
264 lower = Double.parseDouble(st.nextToken());
265 upper = Double.parseDouble(st.nextToken());
266 String restore_line = null;
267 while((restore_line = fp_restore.readLine())!=null)
269 StringTokenizer st2 =
new StringTokenizer(restore_line);
270 idx = Integer.parseInt(st2.nextToken());
271 fmin = Double.parseDouble(st2.nextToken());
272 fmax = Double.parseDouble(st2.nextToken());
273 if (idx <= max_index)
275 feature_min[idx] = fmin;
276 feature_max[idx] = fmax;
283 if(save_filename != null)
285 Formatter formatter =
new Formatter(
new StringBuilder());
286 BufferedWriter fp_save = null;
289 fp_save =
new BufferedWriter(
new FileWriter(save_filename));
290 }
catch(IOException e) {
291 System.err.println(
"can't open file " + save_filename);
297 formatter.format(
"y\n");
298 formatter.format(
"%.16g %.16g\n", y_lower, y_upper);
299 formatter.format(
"%.16g %.16g\n", y_min, y_max);
301 formatter.format(
"x\n");
302 formatter.format(
"%.16g %.16g\n", lower, upper);
305 if(feature_min[i] != feature_max[i])
306 formatter.format(
"%d %.16g %.16g\n", i, feature_min[i], feature_max[i]);
308 fp_save.write(formatter.toString());
319 StringTokenizer st =
new StringTokenizer(line,
" \t\n\r\f:");
320 target = Double.parseDouble(st.nextToken());
322 while(st.hasMoreElements())
324 index = Integer.parseInt(st.nextToken());
325 value = Double.parseDouble(st.nextToken());
326 for (i = next_index; i<index; i++)
329 next_index = index + 1;
334 System.out.print(
"\n");
336 if (new_num_nonzeros > num_nonzeros)
338 "WARNING: original #nonzeros " + num_nonzeros+
"\n" 339 +
" new #nonzeros " + new_num_nonzeros+
"\n" 340 +
"Use -l 0 if many original feature values are zeros\n");
345 public static void main(String argv[])
throws IOException
347 svm_scale
s =
new svm_scale();
long int new_num_nonzeros
void output_target(double value)
char * readline(FILE *input)
void output(int index, double value)
void run(ClassLoader *loader)
int main(int argc, char **argv)