liblinear-ruby 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/ext/blasp.h +8 -8
  4. data/ext/daxpy.c +3 -3
  5. data/ext/ddot.c +3 -3
  6. data/ext/dnrm2.c +7 -7
  7. data/ext/dscal.c +4 -4
  8. data/ext/liblinear_wrap.cxx +382 -382
  9. data/ext/linear.cpp +44 -55
  10. data/ext/linear.h +5 -1
  11. data/ext/tron.cpp +13 -5
  12. data/ext/tron.h +1 -1
  13. data/lib/liblinear.rb +2 -0
  14. data/lib/liblinear/version.rb +1 -1
  15. metadata +2 -41
  16. data/liblinear-2.1/COPYRIGHT +0 -31
  17. data/liblinear-2.1/Makefile +0 -37
  18. data/liblinear-2.1/Makefile.win +0 -24
  19. data/liblinear-2.1/README +0 -600
  20. data/liblinear-2.1/blas/Makefile +0 -22
  21. data/liblinear-2.1/blas/blas.h +0 -25
  22. data/liblinear-2.1/blas/blasp.h +0 -438
  23. data/liblinear-2.1/blas/daxpy.c +0 -57
  24. data/liblinear-2.1/blas/ddot.c +0 -58
  25. data/liblinear-2.1/blas/dnrm2.c +0 -70
  26. data/liblinear-2.1/blas/dscal.c +0 -52
  27. data/liblinear-2.1/heart_scale +0 -270
  28. data/liblinear-2.1/linear.cpp +0 -3053
  29. data/liblinear-2.1/linear.def +0 -22
  30. data/liblinear-2.1/linear.h +0 -79
  31. data/liblinear-2.1/matlab/Makefile +0 -49
  32. data/liblinear-2.1/matlab/README +0 -208
  33. data/liblinear-2.1/matlab/libsvmread.c +0 -212
  34. data/liblinear-2.1/matlab/libsvmwrite.c +0 -119
  35. data/liblinear-2.1/matlab/linear_model_matlab.c +0 -176
  36. data/liblinear-2.1/matlab/linear_model_matlab.h +0 -2
  37. data/liblinear-2.1/matlab/make.m +0 -22
  38. data/liblinear-2.1/matlab/predict.c +0 -341
  39. data/liblinear-2.1/matlab/train.c +0 -492
  40. data/liblinear-2.1/predict.c +0 -243
  41. data/liblinear-2.1/python/Makefile +0 -4
  42. data/liblinear-2.1/python/README +0 -380
  43. data/liblinear-2.1/python/liblinear.py +0 -323
  44. data/liblinear-2.1/python/liblinearutil.py +0 -270
  45. data/liblinear-2.1/train.c +0 -449
  46. data/liblinear-2.1/tron.cpp +0 -241
  47. data/liblinear-2.1/tron.h +0 -35
  48. data/liblinear-2.1/windows/liblinear.dll +0 -0
  49. data/liblinear-2.1/windows/libsvmread.mexw64 +0 -0
  50. data/liblinear-2.1/windows/libsvmwrite.mexw64 +0 -0
  51. data/liblinear-2.1/windows/predict.exe +0 -0
  52. data/liblinear-2.1/windows/predict.mexw64 +0 -0
  53. data/liblinear-2.1/windows/train.exe +0 -0
  54. data/liblinear-2.1/windows/train.mexw64 +0 -0
@@ -1,492 +0,0 @@
1
- #include <math.h>
2
- #include <stdlib.h>
3
- #include <string.h>
4
- #include <ctype.h>
5
- #include "linear.h"
6
-
7
- #include "mex.h"
8
- #include "linear_model_matlab.h"
9
-
10
- #ifdef MX_API_VER
11
- #if MX_API_VER < 0x07030000
12
- typedef int mwIndex;
13
- #endif
14
- #endif
15
-
16
- #define CMD_LEN 2048
17
- #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
18
- #define INF HUGE_VAL
19
-
20
- void print_null(const char *s) {}
21
- void print_string_matlab(const char *s) {mexPrintf(s);}
22
-
23
- void exit_with_help()
24
- {
25
- mexPrintf(
26
- "Usage: model = train(training_label_vector, training_instance_matrix, 'liblinear_options', 'col');\n"
27
- "liblinear_options:\n"
28
- "-s type : set type of solver (default 1)\n"
29
- " for multi-class classification\n"
30
- " 0 -- L2-regularized logistic regression (primal)\n"
31
- " 1 -- L2-regularized L2-loss support vector classification (dual)\n"
32
- " 2 -- L2-regularized L2-loss support vector classification (primal)\n"
33
- " 3 -- L2-regularized L1-loss support vector classification (dual)\n"
34
- " 4 -- support vector classification by Crammer and Singer\n"
35
- " 5 -- L1-regularized L2-loss support vector classification\n"
36
- " 6 -- L1-regularized logistic regression\n"
37
- " 7 -- L2-regularized logistic regression (dual)\n"
38
- " for regression\n"
39
- " 11 -- L2-regularized L2-loss support vector regression (primal)\n"
40
- " 12 -- L2-regularized L2-loss support vector regression (dual)\n"
41
- " 13 -- L2-regularized L1-loss support vector regression (dual)\n"
42
- "-c cost : set the parameter C (default 1)\n"
43
- "-p epsilon : set the epsilon in loss function of SVR (default 0.1)\n"
44
- "-e epsilon : set tolerance of termination criterion\n"
45
- " -s 0 and 2\n"
46
- " |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,\n"
47
- " where f is the primal function and pos/neg are # of\n"
48
- " positive/negative data (default 0.01)\n"
49
- " -s 11\n"
50
- " |f'(w)|_2 <= eps*|f'(w0)|_2 (default 0.001)\n"
51
- " -s 1, 3, 4 and 7\n"
52
- " Dual maximal violation <= eps; similar to libsvm (default 0.1)\n"
53
- " -s 5 and 6\n"
54
- " |f'(w)|_1 <= eps*min(pos,neg)/l*|f'(w0)|_1,\n"
55
- " where f is the primal function (default 0.01)\n"
56
- " -s 12 and 13\n"
57
- " |f'(alpha)|_1 <= eps |f'(alpha0)|,\n"
58
- " where f is the dual function (default 0.1)\n"
59
- "-B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1)\n"
60
- "-wi weight: weights adjust the parameter C of different classes (see README for details)\n"
61
- "-v n: n-fold cross validation mode\n"
62
- "-C : find parameter C (only for -s 0 and 2)\n"
63
- "-q : quiet mode (no outputs)\n"
64
- "col:\n"
65
- " if 'col' is setted, training_instance_matrix is parsed in column format, otherwise is in row format\n"
66
- );
67
- }
68
-
69
- // liblinear arguments
70
- struct parameter param; // set by parse_command_line
71
- struct problem prob; // set by read_problem
72
- struct model *model_;
73
- struct feature_node *x_space;
74
- int flag_cross_validation;
75
- int flag_find_C;
76
- int flag_C_specified;
77
- int flag_solver_specified;
78
- int col_format_flag;
79
- int nr_fold;
80
- double bias;
81
-
82
-
83
- void do_find_parameter_C(double *best_C, double *best_rate)
84
- {
85
- double start_C;
86
- double max_C = 1024;
87
- if (flag_C_specified)
88
- start_C = param.C;
89
- else
90
- start_C = -1.0;
91
- find_parameter_C(&prob, &param, nr_fold, start_C, max_C, best_C, best_rate);
92
- mexPrintf("Best C = %lf CV accuracy = %g%%\n", *best_C, 100.0**best_rate);
93
- }
94
-
95
-
96
- double do_cross_validation()
97
- {
98
- int i;
99
- int total_correct = 0;
100
- double total_error = 0;
101
- double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
102
- double *target = Malloc(double, prob.l);
103
- double retval = 0.0;
104
-
105
- cross_validation(&prob,&param,nr_fold,target);
106
- if(param.solver_type == L2R_L2LOSS_SVR ||
107
- param.solver_type == L2R_L1LOSS_SVR_DUAL ||
108
- param.solver_type == L2R_L2LOSS_SVR_DUAL)
109
- {
110
- for(i=0;i<prob.l;i++)
111
- {
112
- double y = prob.y[i];
113
- double v = target[i];
114
- total_error += (v-y)*(v-y);
115
- sumv += v;
116
- sumy += y;
117
- sumvv += v*v;
118
- sumyy += y*y;
119
- sumvy += v*y;
120
- }
121
- mexPrintf("Cross Validation Mean squared error = %g\n",total_error/prob.l);
122
- mexPrintf("Cross Validation Squared correlation coefficient = %g\n",
123
- ((prob.l*sumvy-sumv*sumy)*(prob.l*sumvy-sumv*sumy))/
124
- ((prob.l*sumvv-sumv*sumv)*(prob.l*sumyy-sumy*sumy))
125
- );
126
- retval = total_error/prob.l;
127
- }
128
- else
129
- {
130
- for(i=0;i<prob.l;i++)
131
- if(target[i] == prob.y[i])
132
- ++total_correct;
133
- mexPrintf("Cross Validation Accuracy = %g%%\n",100.0*total_correct/prob.l);
134
- retval = 100.0*total_correct/prob.l;
135
- }
136
-
137
- free(target);
138
- return retval;
139
- }
140
-
141
- // nrhs should be 3
142
- int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name)
143
- {
144
- int i, argc = 1;
145
- char cmd[CMD_LEN];
146
- char *argv[CMD_LEN/2];
147
- void (*print_func)(const char *) = print_string_matlab; // default printing to matlab display
148
-
149
- // default values
150
- param.solver_type = L2R_L2LOSS_SVC_DUAL;
151
- param.C = 1;
152
- param.eps = INF; // see setting below
153
- param.p = 0.1;
154
- param.nr_weight = 0;
155
- param.weight_label = NULL;
156
- param.weight = NULL;
157
- param.init_sol = NULL;
158
- flag_cross_validation = 0;
159
- col_format_flag = 0;
160
- flag_C_specified = 0;
161
- flag_solver_specified = 0;
162
- flag_find_C = 0;
163
- bias = -1;
164
-
165
-
166
- if(nrhs <= 1)
167
- return 1;
168
-
169
- if(nrhs == 4)
170
- {
171
- mxGetString(prhs[3], cmd, mxGetN(prhs[3])+1);
172
- if(strcmp(cmd, "col") == 0)
173
- col_format_flag = 1;
174
- }
175
-
176
- // put options in argv[]
177
- if(nrhs > 2)
178
- {
179
- mxGetString(prhs[2], cmd, mxGetN(prhs[2]) + 1);
180
- if((argv[argc] = strtok(cmd, " ")) != NULL)
181
- while((argv[++argc] = strtok(NULL, " ")) != NULL)
182
- ;
183
- }
184
-
185
- // parse options
186
- for(i=1;i<argc;i++)
187
- {
188
- if(argv[i][0] != '-') break;
189
- ++i;
190
- if(i>=argc && argv[i-1][1] != 'q' && argv[i-1][1] != 'C') // since options -q and -C have no parameter
191
- return 1;
192
- switch(argv[i-1][1])
193
- {
194
- case 's':
195
- param.solver_type = atoi(argv[i]);
196
- flag_solver_specified = 1;
197
- break;
198
- case 'c':
199
- param.C = atof(argv[i]);
200
- flag_C_specified = 1;
201
- break;
202
- case 'p':
203
- param.p = atof(argv[i]);
204
- break;
205
- case 'e':
206
- param.eps = atof(argv[i]);
207
- break;
208
- case 'B':
209
- bias = atof(argv[i]);
210
- break;
211
- case 'v':
212
- flag_cross_validation = 1;
213
- nr_fold = atoi(argv[i]);
214
- if(nr_fold < 2)
215
- {
216
- mexPrintf("n-fold cross validation: n must >= 2\n");
217
- return 1;
218
- }
219
- break;
220
- case 'w':
221
- ++param.nr_weight;
222
- param.weight_label = (int *) realloc(param.weight_label,sizeof(int)*param.nr_weight);
223
- param.weight = (double *) realloc(param.weight,sizeof(double)*param.nr_weight);
224
- param.weight_label[param.nr_weight-1] = atoi(&argv[i-1][2]);
225
- param.weight[param.nr_weight-1] = atof(argv[i]);
226
- break;
227
- case 'q':
228
- print_func = &print_null;
229
- i--;
230
- break;
231
- case 'C':
232
- flag_find_C = 1;
233
- i--;
234
- break;
235
- default:
236
- mexPrintf("unknown option\n");
237
- return 1;
238
- }
239
- }
240
-
241
- set_print_string_function(print_func);
242
-
243
- // default solver for parameter selection is L2R_L2LOSS_SVC
244
- if(flag_find_C)
245
- {
246
- if(!flag_cross_validation)
247
- nr_fold = 5;
248
- if(!flag_solver_specified)
249
- {
250
- mexPrintf("Solver not specified. Using -s 2\n");
251
- param.solver_type = L2R_L2LOSS_SVC;
252
- }
253
- else if(param.solver_type != L2R_LR && param.solver_type != L2R_L2LOSS_SVC)
254
- {
255
- mexPrintf("Warm-start parameter search only available for -s 0 and -s 2\n");
256
- return 1;
257
- }
258
- }
259
-
260
- if(param.eps == INF)
261
- {
262
- switch(param.solver_type)
263
- {
264
- case L2R_LR:
265
- case L2R_L2LOSS_SVC:
266
- param.eps = 0.01;
267
- break;
268
- case L2R_L2LOSS_SVR:
269
- param.eps = 0.001;
270
- break;
271
- case L2R_L2LOSS_SVC_DUAL:
272
- case L2R_L1LOSS_SVC_DUAL:
273
- case MCSVM_CS:
274
- case L2R_LR_DUAL:
275
- param.eps = 0.1;
276
- break;
277
- case L1R_L2LOSS_SVC:
278
- case L1R_LR:
279
- param.eps = 0.01;
280
- break;
281
- case L2R_L1LOSS_SVR_DUAL:
282
- case L2R_L2LOSS_SVR_DUAL:
283
- param.eps = 0.1;
284
- break;
285
- }
286
- }
287
- return 0;
288
- }
289
-
290
- static void fake_answer(int nlhs, mxArray *plhs[])
291
- {
292
- int i;
293
- for(i=0;i<nlhs;i++)
294
- plhs[i] = mxCreateDoubleMatrix(0, 0, mxREAL);
295
- }
296
-
297
- int read_problem_sparse(const mxArray *label_vec, const mxArray *instance_mat)
298
- {
299
- mwIndex *ir, *jc, low, high, k;
300
- // using size_t due to the output type of matlab functions
301
- size_t i, j, l, elements, max_index, label_vector_row_num;
302
- mwSize num_samples;
303
- double *samples, *labels;
304
- mxArray *instance_mat_col; // instance sparse matrix in column format
305
-
306
- prob.x = NULL;
307
- prob.y = NULL;
308
- x_space = NULL;
309
-
310
- if(col_format_flag)
311
- instance_mat_col = (mxArray *)instance_mat;
312
- else
313
- {
314
- // transpose instance matrix
315
- mxArray *prhs[1], *plhs[1];
316
- prhs[0] = mxDuplicateArray(instance_mat);
317
- if(mexCallMATLAB(1, plhs, 1, prhs, "transpose"))
318
- {
319
- mexPrintf("Error: cannot transpose training instance matrix\n");
320
- return -1;
321
- }
322
- instance_mat_col = plhs[0];
323
- mxDestroyArray(prhs[0]);
324
- }
325
-
326
- // the number of instance
327
- l = mxGetN(instance_mat_col);
328
- label_vector_row_num = mxGetM(label_vec);
329
- prob.l = (int) l;
330
-
331
- if(label_vector_row_num!=l)
332
- {
333
- mexPrintf("Length of label vector does not match # of instances.\n");
334
- return -1;
335
- }
336
-
337
- // each column is one instance
338
- labels = mxGetPr(label_vec);
339
- samples = mxGetPr(instance_mat_col);
340
- ir = mxGetIr(instance_mat_col);
341
- jc = mxGetJc(instance_mat_col);
342
-
343
- num_samples = mxGetNzmax(instance_mat_col);
344
-
345
- elements = num_samples + l*2;
346
- max_index = mxGetM(instance_mat_col);
347
-
348
- prob.y = Malloc(double, l);
349
- prob.x = Malloc(struct feature_node*, l);
350
- x_space = Malloc(struct feature_node, elements);
351
-
352
- prob.bias=bias;
353
-
354
- j = 0;
355
- for(i=0;i<l;i++)
356
- {
357
- prob.x[i] = &x_space[j];
358
- prob.y[i] = labels[i];
359
- low = jc[i], high = jc[i+1];
360
- for(k=low;k<high;k++)
361
- {
362
- x_space[j].index = (int) ir[k]+1;
363
- x_space[j].value = samples[k];
364
- j++;
365
- }
366
- if(prob.bias>=0)
367
- {
368
- x_space[j].index = (int) max_index+1;
369
- x_space[j].value = prob.bias;
370
- j++;
371
- }
372
- x_space[j++].index = -1;
373
- }
374
-
375
- if(prob.bias>=0)
376
- prob.n = (int) max_index+1;
377
- else
378
- prob.n = (int) max_index;
379
-
380
- return 0;
381
- }
382
-
383
- // Interface function of matlab
384
- // now assume prhs[0]: label prhs[1]: features
385
- void mexFunction( int nlhs, mxArray *plhs[],
386
- int nrhs, const mxArray *prhs[] )
387
- {
388
- const char *error_msg;
389
- // fix random seed to have same results for each run
390
- // (for cross validation)
391
- srand(1);
392
-
393
- if(nlhs > 1)
394
- {
395
- exit_with_help();
396
- fake_answer(nlhs, plhs);
397
- return;
398
- }
399
-
400
- // Transform the input Matrix to libsvm format
401
- if(nrhs > 1 && nrhs < 5)
402
- {
403
- int err=0;
404
-
405
- if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1]))
406
- {
407
- mexPrintf("Error: label vector and instance matrix must be double\n");
408
- fake_answer(nlhs, plhs);
409
- return;
410
- }
411
-
412
- if(mxIsSparse(prhs[0]))
413
- {
414
- mexPrintf("Error: label vector should not be in sparse format");
415
- fake_answer(nlhs, plhs);
416
- return;
417
- }
418
-
419
- if(parse_command_line(nrhs, prhs, NULL))
420
- {
421
- exit_with_help();
422
- destroy_param(&param);
423
- fake_answer(nlhs, plhs);
424
- return;
425
- }
426
-
427
- if(mxIsSparse(prhs[1]))
428
- err = read_problem_sparse(prhs[0], prhs[1]);
429
- else
430
- {
431
- mexPrintf("Training_instance_matrix must be sparse; "
432
- "use sparse(Training_instance_matrix) first\n");
433
- destroy_param(&param);
434
- fake_answer(nlhs, plhs);
435
- return;
436
- }
437
-
438
- // train's original code
439
- error_msg = check_parameter(&prob, &param);
440
-
441
- if(err || error_msg)
442
- {
443
- if (error_msg != NULL)
444
- mexPrintf("Error: %s\n", error_msg);
445
- destroy_param(&param);
446
- free(prob.y);
447
- free(prob.x);
448
- free(x_space);
449
- fake_answer(nlhs, plhs);
450
- return;
451
- }
452
-
453
- if (flag_find_C)
454
- {
455
- double best_C, best_rate, *ptr;
456
-
457
- do_find_parameter_C(&best_C, &best_rate);
458
-
459
- plhs[0] = mxCreateDoubleMatrix(2, 1, mxREAL);
460
- ptr = mxGetPr(plhs[0]);
461
- ptr[0] = best_C;
462
- ptr[1] = best_rate;
463
- }
464
- else if(flag_cross_validation)
465
- {
466
- double *ptr;
467
- plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
468
- ptr = mxGetPr(plhs[0]);
469
- ptr[0] = do_cross_validation();
470
- }
471
- else
472
- {
473
- const char *error_msg;
474
-
475
- model_ = train(&prob, &param);
476
- error_msg = model_to_matlab_structure(plhs, model_);
477
- if(error_msg)
478
- mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg);
479
- free_and_destroy_model(&model_);
480
- }
481
- destroy_param(&param);
482
- free(prob.y);
483
- free(prob.x);
484
- free(x_space);
485
- }
486
- else
487
- {
488
- exit_with_help();
489
- fake_answer(nlhs, plhs);
490
- return;
491
- }
492
- }