liblinear-ruby 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +141 -10
  3. data/{liblinear-1.93/ruby → ext}/liblinear.i +0 -0
  4. data/ext/linear.cpp +30 -7
  5. data/ext/tron.cpp +4 -4
  6. data/lib/liblinear/model.rb +3 -3
  7. data/lib/liblinear/parameter.rb +2 -2
  8. data/lib/liblinear/problem.rb +4 -17
  9. data/lib/liblinear/version.rb +1 -1
  10. data/lib/liblinear.rb +46 -13
  11. data/{liblinear-1.93 → liblinear-1.94}/COPYRIGHT +0 -0
  12. data/{liblinear-1.93 → liblinear-1.94}/Makefile +0 -0
  13. data/{liblinear-1.93 → liblinear-1.94}/Makefile.win +0 -0
  14. data/{liblinear-1.93 → liblinear-1.94}/README +0 -0
  15. data/{liblinear-1.93 → liblinear-1.94}/blas/Makefile +0 -0
  16. data/{liblinear-1.93 → liblinear-1.94}/blas/blas.h +0 -0
  17. data/{liblinear-1.93 → liblinear-1.94}/blas/blasp.h +0 -0
  18. data/{liblinear-1.93 → liblinear-1.94}/blas/daxpy.c +0 -0
  19. data/{liblinear-1.93 → liblinear-1.94}/blas/ddot.c +0 -0
  20. data/{liblinear-1.93 → liblinear-1.94}/blas/dnrm2.c +0 -0
  21. data/{liblinear-1.93 → liblinear-1.94}/blas/dscal.c +0 -0
  22. data/{liblinear-1.93 → liblinear-1.94}/heart_scale +0 -0
  23. data/{liblinear-1.93 → liblinear-1.94}/linear.cpp +30 -7
  24. data/{liblinear-1.93 → liblinear-1.94}/linear.def +0 -0
  25. data/{liblinear-1.93 → liblinear-1.94}/linear.h +0 -0
  26. data/{liblinear-1.93 → liblinear-1.94}/matlab/Makefile +0 -0
  27. data/{liblinear-1.93 → liblinear-1.94}/matlab/README +2 -1
  28. data/{liblinear-1.93 → liblinear-1.94}/matlab/libsvmread.c +25 -24
  29. data/{liblinear-1.93 → liblinear-1.94}/matlab/libsvmwrite.c +15 -1
  30. data/{liblinear-1.93 → liblinear-1.94}/matlab/linear_model_matlab.c +0 -0
  31. data/{liblinear-1.93 → liblinear-1.94}/matlab/linear_model_matlab.h +0 -0
  32. data/{liblinear-1.93 → liblinear-1.94}/matlab/make.m +0 -0
  33. data/{liblinear-1.93 → liblinear-1.94}/matlab/predict.c +37 -25
  34. data/{liblinear-1.93 → liblinear-1.94}/matlab/train.c +16 -7
  35. data/{liblinear-1.93 → liblinear-1.94}/predict.c +0 -0
  36. data/{liblinear-1.93 → liblinear-1.94}/python/Makefile +0 -0
  37. data/{liblinear-1.93 → liblinear-1.94}/python/README +1 -1
  38. data/{liblinear-1.93 → liblinear-1.94}/python/liblinear.py +1 -1
  39. data/{liblinear-1.93 → liblinear-1.94}/python/liblinearutil.py +0 -0
  40. data/{liblinear-1.93 → liblinear-1.94}/train.c +0 -0
  41. data/{liblinear-1.93 → liblinear-1.94}/tron.cpp +4 -4
  42. data/{liblinear-1.93 → liblinear-1.94}/tron.h +0 -0
  43. data/{liblinear-1.93 → liblinear-1.94}/windows/liblinear.dll +0 -0
  44. data/{liblinear-1.93 → liblinear-1.94}/windows/libsvmread.mexw64 +0 -0
  45. data/{liblinear-1.93 → liblinear-1.94}/windows/libsvmwrite.mexw64 +0 -0
  46. data/{liblinear-1.93 → liblinear-1.94}/windows/predict.exe +0 -0
  47. data/liblinear-1.94/windows/predict.mexw64 +0 -0
  48. data/{liblinear-1.93 → liblinear-1.94}/windows/train.exe +0 -0
  49. data/liblinear-1.94/windows/train.mexw64 +0 -0
  50. data/spec/liblinear/model_spec.rb +56 -0
  51. data/spec/liblinear/parameter_spec.rb +57 -0
  52. data/spec/liblinear/problem_spec.rb +23 -0
  53. data/spec/liblinear_spec.rb +104 -0
  54. metadata +51 -56
  55. data/ext/linear.rb +0 -357
  56. data/liblinear-1.93/blas/blas.a +0 -0
  57. data/liblinear-1.93/blas/daxpy.o +0 -0
  58. data/liblinear-1.93/blas/ddot.o +0 -0
  59. data/liblinear-1.93/blas/dnrm2.o +0 -0
  60. data/liblinear-1.93/blas/dscal.o +0 -0
  61. data/liblinear-1.93/linear.o +0 -0
  62. data/liblinear-1.93/predict +0 -0
  63. data/liblinear-1.93/ruby/liblinear_wrap.cxx +0 -4646
  64. data/liblinear-1.93/ruby/linear.h +0 -74
  65. data/liblinear-1.93/ruby/linear.o +0 -0
  66. data/liblinear-1.93/train +0 -0
  67. data/liblinear-1.93/tron.o +0 -0
  68. data/liblinear-1.93/windows/predict.mexw64 +0 -0
  69. data/liblinear-1.93/windows/train.mexw64 +0 -0
@@ -25,10 +25,11 @@ void exit_with_help()
25
25
  );
26
26
  }
27
27
 
28
- static void fake_answer(mxArray *plhs[])
28
+ static void fake_answer(int nlhs, mxArray *plhs[])
29
29
  {
30
- plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
31
- plhs[1] = mxCreateDoubleMatrix(0, 0, mxREAL);
30
+ int i;
31
+ for(i=0;i<nlhs;i++)
32
+ plhs[i] = mxCreateDoubleMatrix(0, 0, mxREAL);
32
33
  }
33
34
 
34
35
  static char *line;
@@ -53,7 +54,7 @@ static char* readline(FILE *input)
53
54
  }
54
55
 
55
56
  // read in a problem (in libsvm format)
56
- void read_problem(const char *filename, mxArray *plhs[])
57
+ void read_problem(const char *filename, int nlhs, mxArray *plhs[])
57
58
  {
58
59
  int max_index, min_index, inst_max_index, i;
59
60
  long elements, k;
@@ -66,7 +67,7 @@ void read_problem(const char *filename, mxArray *plhs[])
66
67
  if(fp == NULL)
67
68
  {
68
69
  mexPrintf("can't open input file %s\n",filename);
69
- fake_answer(plhs);
70
+ fake_answer(nlhs, plhs);
70
71
  return;
71
72
  }
72
73
 
@@ -96,7 +97,7 @@ void read_problem(const char *filename, mxArray *plhs[])
96
97
  if(endptr == idx || errno != 0 || *endptr != '\0' || index <= inst_max_index)
97
98
  {
98
99
  mexPrintf("Wrong input format at line %d\n",l+1);
99
- fake_answer(plhs);
100
+ fake_answer(nlhs, plhs);
100
101
  return;
101
102
  }
102
103
  else
@@ -135,14 +136,14 @@ void read_problem(const char *filename, mxArray *plhs[])
135
136
  if(label == NULL)
136
137
  {
137
138
  mexPrintf("Empty line at line %d\n",i+1);
138
- fake_answer(plhs);
139
+ fake_answer(nlhs, plhs);
139
140
  return;
140
141
  }
141
142
  labels[i] = strtod(label,&endptr);
142
143
  if(endptr == label || *endptr != '\0')
143
144
  {
144
145
  mexPrintf("Wrong input format at line %d\n",i+1);
145
- fake_answer(plhs);
146
+ fake_answer(nlhs, plhs);
146
147
  return;
147
148
  }
148
149
 
@@ -161,7 +162,7 @@ void read_problem(const char *filename, mxArray *plhs[])
161
162
  if (endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr)))
162
163
  {
163
164
  mexPrintf("Wrong input format at line %d\n",i+1);
164
- fake_answer(plhs);
165
+ fake_answer(nlhs, plhs);
165
166
  return;
166
167
  }
167
168
  ++k;
@@ -178,7 +179,7 @@ void read_problem(const char *filename, mxArray *plhs[])
178
179
  if(mexCallMATLAB(1, lhs, 1, rhs, "transpose"))
179
180
  {
180
181
  mexPrintf("Error: cannot transpose problem\n");
181
- fake_answer(plhs);
182
+ fake_answer(nlhs, plhs);
182
183
  return;
183
184
  }
184
185
  plhs[1] = lhs[0];
@@ -188,25 +189,25 @@ void read_problem(const char *filename, mxArray *plhs[])
188
189
  void mexFunction( int nlhs, mxArray *plhs[],
189
190
  int nrhs, const mxArray *prhs[] )
190
191
  {
191
- if(nrhs == 1)
192
+ char filename[256];
193
+
194
+ if(nrhs != 1 || nlhs != 2)
192
195
  {
193
- char filename[256];
196
+ exit_with_help();
197
+ fake_answer(nlhs, plhs);
198
+ return;
199
+ }
194
200
 
195
- mxGetString(prhs[0], filename, mxGetN(prhs[0]) + 1);
201
+ mxGetString(prhs[0], filename, mxGetN(prhs[0]) + 1);
196
202
 
197
- if(filename == NULL)
198
- {
199
- mexPrintf("Error: filename is NULL\n");
200
- return;
201
- }
202
-
203
- read_problem(filename, plhs);
204
- }
205
- else
203
+ if(filename == NULL)
206
204
  {
207
- exit_with_help();
208
- fake_answer(plhs);
205
+ mexPrintf("Error: filename is NULL\n");
209
206
  return;
210
207
  }
208
+
209
+ read_problem(filename, nlhs, plhs);
210
+
211
+ return;
211
212
  }
212
213
 
@@ -16,6 +16,13 @@ void exit_with_help()
16
16
  );
17
17
  }
18
18
 
19
+ static void fake_answer(int nlhs, mxArray *plhs[])
20
+ {
21
+ int i;
22
+ for(i=0;i<nlhs;i++)
23
+ plhs[i] = mxCreateDoubleMatrix(0, 0, mxREAL);
24
+ }
25
+
19
26
  void libsvmwrite(const char *filename, const mxArray *label_vec, const mxArray *instance_mat)
20
27
  {
21
28
  FILE *fp = fopen(filename,"w");
@@ -77,7 +84,14 @@ void libsvmwrite(const char *filename, const mxArray *label_vec, const mxArray *
77
84
 
78
85
  void mexFunction( int nlhs, mxArray *plhs[],
79
86
  int nrhs, const mxArray *prhs[] )
80
- {
87
+ {
88
+ if(nlhs > 0)
89
+ {
90
+ exit_with_help();
91
+ fake_answer(nlhs, plhs);
92
+ return;
93
+ }
94
+
81
95
  // Transform the input Matrix to libsvm format
82
96
  if(nrhs == 3)
83
97
  {
File without changes
@@ -49,14 +49,14 @@ void read_sparse_instance(const mxArray *prhs, int index, struct feature_node *x
49
49
  x[j].index = -1;
50
50
  }
51
51
 
52
- static void fake_answer(mxArray *plhs[])
52
+ static void fake_answer(int nlhs, mxArray *plhs[])
53
53
  {
54
- plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
55
- plhs[1] = mxCreateDoubleMatrix(0, 0, mxREAL);
56
- plhs[2] = mxCreateDoubleMatrix(0, 0, mxREAL);
54
+ int i;
55
+ for(i=0;i<nlhs;i++)
56
+ plhs[i] = mxCreateDoubleMatrix(0, 0, mxREAL);
57
57
  }
58
58
 
59
- void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, const int predict_probability_flag)
59
+ void do_predict(int nlhs, mxArray *plhs[], const mxArray *prhs[], struct model *model_, const int predict_probability_flag)
60
60
  {
61
61
  int label_vector_row_num, label_vector_col_num;
62
62
  int feature_number, testing_instance_number;
@@ -65,6 +65,7 @@ void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, co
65
65
  double *ptr_prob_estimates, *ptr_dec_values, *ptr;
66
66
  struct feature_node *x;
67
67
  mxArray *pplhs[1]; // instance sparse matrix in row format
68
+ mxArray *tplhs[3]; // temporary storage for plhs[]
68
69
 
69
70
  int correct = 0;
70
71
  int total = 0;
@@ -95,13 +96,13 @@ void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, co
95
96
  if(label_vector_row_num!=testing_instance_number)
96
97
  {
97
98
  mexPrintf("Length of label vector does not match # of instances.\n");
98
- fake_answer(plhs);
99
+ fake_answer(nlhs, plhs);
99
100
  return;
100
101
  }
101
102
  if(label_vector_col_num!=1)
102
103
  {
103
104
  mexPrintf("label (1st argument) should be a vector (# of column is 1).\n");
104
- fake_answer(plhs);
105
+ fake_answer(nlhs, plhs);
105
106
  return;
106
107
  }
107
108
 
@@ -117,7 +118,7 @@ void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, co
117
118
  if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose"))
118
119
  {
119
120
  mexPrintf("Error: cannot transpose testing instance matrix\n");
120
- fake_answer(plhs);
121
+ fake_answer(nlhs, plhs);
121
122
  return;
122
123
  }
123
124
  }
@@ -125,15 +126,15 @@ void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, co
125
126
 
126
127
  prob_estimates = Malloc(double, nr_class);
127
128
 
128
- plhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL);
129
+ tplhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL);
129
130
  if(predict_probability_flag)
130
- plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL);
131
+ tplhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL);
131
132
  else
132
- plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_w, mxREAL);
133
+ tplhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_w, mxREAL);
133
134
 
134
- ptr_predict_label = mxGetPr(plhs[0]);
135
- ptr_prob_estimates = mxGetPr(plhs[2]);
136
- ptr_dec_values = mxGetPr(plhs[2]);
135
+ ptr_predict_label = mxGetPr(tplhs[0]);
136
+ ptr_prob_estimates = mxGetPr(tplhs[2]);
137
+ ptr_dec_values = mxGetPr(tplhs[2]);
137
138
  x = Malloc(struct feature_node, feature_number+2);
138
139
  for(instance_index=0;instance_index<testing_instance_number;instance_index++)
139
140
  {
@@ -189,8 +190,8 @@ void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, co
189
190
  info("Accuracy = %g%% (%d/%d)\n", (double) correct/total*100,correct,total);
190
191
 
191
192
  // return accuracy, mean squared error, squared correlation coefficient
192
- plhs[1] = mxCreateDoubleMatrix(3, 1, mxREAL);
193
- ptr = mxGetPr(plhs[1]);
193
+ tplhs[1] = mxCreateDoubleMatrix(3, 1, mxREAL);
194
+ ptr = mxGetPr(tplhs[1]);
194
195
  ptr[0] = (double)correct/total*100;
195
196
  ptr[1] = error/total;
196
197
  ptr[2] = ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/
@@ -199,12 +200,23 @@ void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, co
199
200
  free(x);
200
201
  if(prob_estimates != NULL)
201
202
  free(prob_estimates);
203
+
204
+ switch(nlhs)
205
+ {
206
+ case 3:
207
+ plhs[2] = tplhs[2];
208
+ plhs[1] = tplhs[1];
209
+ case 1:
210
+ case 0:
211
+ plhs[0] = tplhs[0];
212
+ }
202
213
  }
203
214
 
204
215
  void exit_with_help()
205
216
  {
206
217
  mexPrintf(
207
218
  "Usage: [predicted_label, accuracy, decision_values/prob_estimates] = predict(testing_label_vector, testing_instance_matrix, model, 'liblinear_options','col')\n"
219
+ " [predicted_label] = predict(testing_label_vector, testing_instance_matrix, model, 'liblinear_options','col')\n"
208
220
  "liblinear_options:\n"
209
221
  "-b probability_estimates: whether to output probability estimates, 0 or 1 (default 0); currently for logistic regression only\n"
210
222
  "-q quiet mode (no outputs)\n"
@@ -225,10 +237,10 @@ void mexFunction( int nlhs, mxArray *plhs[],
225
237
  info = &mexPrintf;
226
238
  col_format_flag = 0;
227
239
 
228
- if(nrhs > 5 || nrhs < 3)
240
+ if(nlhs == 2 || nlhs > 3 || nrhs > 5 || nrhs < 3)
229
241
  {
230
242
  exit_with_help();
231
- fake_answer(plhs);
243
+ fake_answer(nlhs, plhs);
232
244
  return;
233
245
  }
234
246
  if(nrhs == 5)
@@ -242,7 +254,7 @@ void mexFunction( int nlhs, mxArray *plhs[],
242
254
 
243
255
  if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) {
244
256
  mexPrintf("Error: label vector and instance matrix must be double\n");
245
- fake_answer(plhs);
257
+ fake_answer(nlhs, plhs);
246
258
  return;
247
259
  }
248
260
 
@@ -269,7 +281,7 @@ void mexFunction( int nlhs, mxArray *plhs[],
269
281
  if(i>=argc && argv[i-1][1] != 'q')
270
282
  {
271
283
  exit_with_help();
272
- fake_answer(plhs);
284
+ fake_answer(nlhs, plhs);
273
285
  return;
274
286
  }
275
287
  switch(argv[i-1][1])
@@ -284,7 +296,7 @@ void mexFunction( int nlhs, mxArray *plhs[],
284
296
  default:
285
297
  mexPrintf("unknown option\n");
286
298
  exit_with_help();
287
- fake_answer(plhs);
299
+ fake_answer(nlhs, plhs);
288
300
  return;
289
301
  }
290
302
  }
@@ -296,7 +308,7 @@ void mexFunction( int nlhs, mxArray *plhs[],
296
308
  {
297
309
  mexPrintf("Error: can't read model: %s\n", error_msg);
298
310
  free_and_destroy_model(&model_);
299
- fake_answer(plhs);
311
+ fake_answer(nlhs, plhs);
300
312
  return;
301
313
  }
302
314
 
@@ -310,12 +322,12 @@ void mexFunction( int nlhs, mxArray *plhs[],
310
322
  }
311
323
 
312
324
  if(mxIsSparse(prhs[1]))
313
- do_predict(plhs, prhs, model_, prob_estimate_flag);
325
+ do_predict(nlhs, plhs, prhs, model_, prob_estimate_flag);
314
326
  else
315
327
  {
316
328
  mexPrintf("Testing_instance_matrix must be sparse; "
317
329
  "use sparse(Testing_instance_matrix) first\n");
318
- fake_answer(plhs);
330
+ fake_answer(nlhs, plhs);
319
331
  }
320
332
 
321
333
  // destroy model_
@@ -324,7 +336,7 @@ void mexFunction( int nlhs, mxArray *plhs[],
324
336
  else
325
337
  {
326
338
  mexPrintf("model file should be a struct array\n");
327
- fake_answer(plhs);
339
+ fake_answer(nlhs, plhs);
328
340
  }
329
341
 
330
342
  return;
@@ -243,9 +243,11 @@ int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name)
243
243
  return 0;
244
244
  }
245
245
 
246
- static void fake_answer(mxArray *plhs[])
246
+ static void fake_answer(int nlhs, mxArray *plhs[])
247
247
  {
248
- plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
248
+ int i;
249
+ for(i=0;i<nlhs;i++)
250
+ plhs[i] = mxCreateDoubleMatrix(0, 0, mxREAL);
249
251
  }
250
252
 
251
253
  int read_problem_sparse(const mxArray *label_vec, const mxArray *instance_mat)
@@ -342,6 +344,13 @@ void mexFunction( int nlhs, mxArray *plhs[],
342
344
  // (for cross validation)
343
345
  srand(1);
344
346
 
347
+ if(nlhs > 1)
348
+ {
349
+ exit_with_help();
350
+ fake_answer(nlhs, plhs);
351
+ return;
352
+ }
353
+
345
354
  // Transform the input Matrix to libsvm format
346
355
  if(nrhs > 1 && nrhs < 5)
347
356
  {
@@ -349,7 +358,7 @@ void mexFunction( int nlhs, mxArray *plhs[],
349
358
 
350
359
  if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) {
351
360
  mexPrintf("Error: label vector and instance matrix must be double\n");
352
- fake_answer(plhs);
361
+ fake_answer(nlhs, plhs);
353
362
  return;
354
363
  }
355
364
 
@@ -357,7 +366,7 @@ void mexFunction( int nlhs, mxArray *plhs[],
357
366
  {
358
367
  exit_with_help();
359
368
  destroy_param(&param);
360
- fake_answer(plhs);
369
+ fake_answer(nlhs, plhs);
361
370
  return;
362
371
  }
363
372
 
@@ -368,7 +377,7 @@ void mexFunction( int nlhs, mxArray *plhs[],
368
377
  mexPrintf("Training_instance_matrix must be sparse; "
369
378
  "use sparse(Training_instance_matrix) first\n");
370
379
  destroy_param(&param);
371
- fake_answer(plhs);
380
+ fake_answer(nlhs, plhs);
372
381
  return;
373
382
  }
374
383
 
@@ -383,7 +392,7 @@ void mexFunction( int nlhs, mxArray *plhs[],
383
392
  free(prob.y);
384
393
  free(prob.x);
385
394
  free(x_space);
386
- fake_answer(plhs);
395
+ fake_answer(nlhs, plhs);
387
396
  return;
388
397
  }
389
398
 
@@ -412,7 +421,7 @@ void mexFunction( int nlhs, mxArray *plhs[],
412
421
  else
413
422
  {
414
423
  exit_with_help();
415
- fake_answer(plhs);
424
+ fake_answer(nlhs, plhs);
416
425
  return;
417
426
  }
418
427
  }
File without changes
File without changes
@@ -56,7 +56,7 @@ in liblinearutil.py and the usage is the same as the LIBLINEAR MATLAB interface.
56
56
  # Sparse data
57
57
  >>> y, x = [1,-1], [{1:1, 3:1}, {1:-1,3:-1}]
58
58
  >>> prob = problem(y, x)
59
- >>> param = parameter('-c 4 -B 1')
59
+ >>> param = parameter('-s 0 -c 4 -B 1')
60
60
  >>> m = train(prob, param)
61
61
 
62
62
  # Other utility functions
@@ -147,7 +147,7 @@ class parameter(Structure):
147
147
  self.bias = -1
148
148
  self.cross_validation = False
149
149
  self.nr_fold = 0
150
- self.print_func = None
150
+ self.print_func = cast(None, PRINT_STRING_FUN)
151
151
 
152
152
  def parse_options(self, options):
153
153
  if isinstance(options, list):
File without changes
@@ -74,7 +74,7 @@ void TRON::tron(double *w)
74
74
  for (i=0; i<n; i++)
75
75
  w[i] = 0;
76
76
 
77
- f = fun_obj->fun(w);
77
+ f = fun_obj->fun(w);
78
78
  fun_obj->grad(w, g);
79
79
  delta = dnrm2_(&n, g, &inc);
80
80
  double gnorm1 = delta;
@@ -94,10 +94,10 @@ void TRON::tron(double *w)
94
94
 
95
95
  gs = ddot_(&n, g, &inc, s, &inc);
96
96
  prered = -0.5*(gs-ddot_(&n, s, &inc, r, &inc));
97
- fnew = fun_obj->fun(w_new);
97
+ fnew = fun_obj->fun(w_new);
98
98
 
99
99
  // Compute the actual reduction.
100
- actred = f - fnew;
100
+ actred = f - fnew;
101
101
 
102
102
  // On the first iteration, adjust the initial step bound.
103
103
  snorm = dnrm2_(&n, s, &inc);
@@ -127,7 +127,7 @@ void TRON::tron(double *w)
127
127
  iter++;
128
128
  memcpy(w, w_new, sizeof(double)*n);
129
129
  f = fnew;
130
- fun_obj->grad(w, g);
130
+ fun_obj->grad(w, g);
131
131
 
132
132
  gnorm = dnrm2_(&n, g, &inc);
133
133
  if (gnorm <= eps*gnorm1)
File without changes
Binary file
@@ -0,0 +1,56 @@
1
+ $: << File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
2
+ require 'liblinear'
3
+
4
+ describe Liblinear::Model do
5
+ before do
6
+ @prob = Liblinear::Problem.new([1, 2], [[1],[2]])
7
+ @param = Liblinear::Parameter.new
8
+ @model = Liblinear::Model.new(@prob, @param)
9
+ end
10
+
11
+ describe '#initialize' do
12
+ it 'raise ArgumentError when arg_1 not equal [Liblinear::Problem] or arg_2 not equal [Liblinear::Parameter]' do
13
+ expect{
14
+ Liblinear::Model.new(1, 2)
15
+ }.to raise_error(ArgumentError, 'arguments must be [Liblinear::Problem] and [Liblinear::Parameter]')
16
+ end
17
+
18
+ it 'raise Liblinear::InvalidParameter when parameter is invalid' do
19
+ param = Liblinear::Parameter.new
20
+ param.C = -1
21
+ expect{
22
+ Liblinear::Model.new(@prob, param)
23
+ }.to raise_error(Liblinear::InvalidParameter, 'C <= 0')
24
+ end
25
+
26
+ it 'raise ArgumentError when argument is not [String]' do
27
+ expect{
28
+ Liblinear::Model.new(1)
29
+ }.to raise_error(ArgumentError, 'argument must be [String]')
30
+ end
31
+ end
32
+
33
+ describe '#nr_class' do
34
+ it 'returns the number of classes' do
35
+ expect(@model.nr_class).to eq(2)
36
+ end
37
+ end
38
+
39
+ describe '#labels' do
40
+ it 'returns labels' do
41
+ expect(@model.labels).to eq([1, 2])
42
+ end
43
+ end
44
+
45
+ describe '#predict' do
46
+ it 'return predicted class' do
47
+ expect(@model.predict([1]).class).to eq(Float)
48
+ end
49
+ end
50
+
51
+ describe '#predict_probability' do
52
+ it 'return predict_probability' do
53
+ expect(@model.predict_probability([1]).class).to eq(Hash)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,57 @@
1
+ $: << File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
2
+ require 'liblinear'
3
+
4
+ describe Liblinear::Parameter do
5
+ before do
6
+ @param = Liblinear::Parameter.new
7
+ end
8
+
9
+ describe '#solver_type' do
10
+ it 'returns solver type' do
11
+ @param.solver_type = 0
12
+ expect(@param.solver_type).to eq(Liblinear::L2R_LR)
13
+ end
14
+ end
15
+
16
+ describe '#c' do
17
+ it 'returns C' do
18
+ @param.C = 2
19
+ expect(@param.C).to eq(2)
20
+ end
21
+ end
22
+
23
+ describe '#eps' do
24
+ it 'returns eps' do
25
+ @param.eps = 0.0
26
+ expect(@param.eps).to eq(0.0)
27
+ end
28
+ end
29
+
30
+ describe '#p' do
31
+ it 'returns p' do
32
+ @param.p = 0.0
33
+ expect(@param.p).to eq(0.0)
34
+ end
35
+ end
36
+
37
+ describe '#nr_weight' do
38
+ it 'returns nr_weight' do
39
+ @param.nr_weight = 3
40
+ expect(@param.nr_weight).to eq(3)
41
+ end
42
+ end
43
+
44
+ describe '#weight_label' do
45
+ it 'returns weight_label' do
46
+ @param.weight_label = [1, 2, 3]
47
+ expect(@param.weight_label.class).to eq(SWIG::TYPE_p_int)
48
+ end
49
+ end
50
+
51
+ describe '#weight' do
52
+ it 'returns weight' do
53
+ @param.weight = [1.0, 2.0, 3.0]
54
+ expect(@param.weight.class).to eq(SWIG::TYPE_p_double)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,23 @@
1
+ $: << File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
2
+ require 'liblinear'
3
+
4
+ describe Liblinear::Problem do
5
+ include Liblinearswig
6
+ before do
7
+ @problem = Liblinear::Problem.new([1, 2], [[1],[2]])
8
+ end
9
+
10
+ describe '#initialize' do
11
+ it 'raise ArgumentError when size of labels is different from that of examples' do
12
+ expect{
13
+ Liblinear::Problem.new([1, 2], [[1]])
14
+ }.to raise_error(ArgumentError, 'labels and examples must be same size')
15
+ end
16
+ end
17
+
18
+ describe '#prob' do
19
+ it 'returns an instance of [Liblinearswig::Problem]' do
20
+ expect(@problem.prob.class).to eq(Liblinearswig::Problem)
21
+ end
22
+ end
23
+ end