numo-liblinear 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ #ifndef NUMO_LIBLINEAR_CONVERTER_H
2
+ #define NUMO_LIBLINEAR_CONVERTER_H 1
3
+
4
+ #include <string.h>
5
+ #include <ruby.h>
6
+ #include <numo/narray.h>
7
+ #include <numo/template.h>
8
+
9
+ VALUE int_vec_to_nary(int* const arr, int const size);
10
+ int* nary_to_int_vec(VALUE vec_val);
11
+ VALUE dbl_vec_to_nary(double* const arr, int const size);
12
+ double* nary_to_dbl_vec(VALUE vec_val);
13
+ VALUE dbl_mat_to_nary(double** const mat, int const n_rows, int const n_cols);
14
+ double** nary_to_dbl_mat(VALUE mat_val);
15
+
16
+ #endif /* NUMO_LIBLINEAR_CONVERTER_H */
@@ -0,0 +1,39 @@
1
+ require 'mkmf'
2
+ require 'numo/narray'
3
+
4
+ $LOAD_PATH.each do |lp|
5
+ if File.exist?(File.join(lp, 'numo/numo/narray.h'))
6
+ $INCFLAGS = "-I#{lp}/numo #{$INCFLAGS}"
7
+ break
8
+ end
9
+ end
10
+
11
+ unless have_header('numo/narray.h')
12
+ puts 'numo/narray.h not found.'
13
+ exit(1)
14
+ end
15
+
16
+ if RUBY_PLATFORM =~ /mswin|cygwin|mingw/
17
+ $LOAD_PATH.each do |lp|
18
+ if File.exist?(File.join(lp, 'numo/libnarray.a'))
19
+ $LDFLAGS = "-L#{lp}/numo #{$LDFLAGS}"
20
+ break
21
+ end
22
+ end
23
+ unless have_library('narray', 'nary_new')
24
+ puts 'libnarray.a not found.'
25
+ exit(1)
26
+ end
27
+ end
28
+
29
+ unless have_header('linear.h')
30
+ puts 'linear.h not found.'
31
+ exit(1)
32
+ end
33
+
34
+ unless have_library('linear')
35
+ puts 'liblinear not found.'
36
+ exit(1)
37
+ end
38
+
39
+ create_makefile('numo/liblinear/liblinearext')
@@ -0,0 +1,421 @@
1
+ /**
2
+ * LIBLINEAR interface for Numo::NArray
3
+ */
4
+ #include "liblinearext.h"
5
+
6
+ VALUE mNumo;
7
+ VALUE mLiblinear;
8
+
9
+ void print_null(const char *s) {}
10
+
11
+ /**
12
+ * Train the model according to the given training data.
13
+ *
14
+ * @overload train(x, y, param) -> Hash
15
+ *
16
+ * @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to be used for training the model.
17
+ * @param y [Numo::DFloat] (shape: [n_samples]) The labels or target values for samples.
18
+ * @param param [Hash] The parameters of a model.
19
+ * @return [Hash] The model obtained from the training procedure.
20
+ */
21
+ static
22
+ VALUE numo_liblinear_train(VALUE self, VALUE x_val, VALUE y_val, VALUE param_hash)
23
+ {
24
+ struct problem* problem;
25
+ struct parameter* param;
26
+ struct model* model;
27
+ VALUE model_hash;
28
+
29
+ if (CLASS_OF(x_val) != numo_cDFloat) {
30
+ x_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, x_val);
31
+ }
32
+ if (CLASS_OF(y_val) != numo_cDFloat) {
33
+ y_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, y_val);
34
+ }
35
+ if (!RTEST(nary_check_contiguous(x_val))) {
36
+ x_val = nary_dup(x_val);
37
+ }
38
+ if (!RTEST(nary_check_contiguous(y_val))) {
39
+ y_val = nary_dup(y_val);
40
+ }
41
+
42
+ param = rb_hash_to_parameter(param_hash);
43
+ problem = dataset_to_problem(x_val, y_val);
44
+
45
+ set_print_string_function(print_null);
46
+ model = train(problem, param);
47
+ model_hash = model_to_rb_hash(model);
48
+ free_and_destroy_model(&model);
49
+
50
+ xfree_problem(problem);
51
+ xfree_parameter(param);
52
+
53
+ return model_hash;
54
+ }
55
+
56
+ /**
57
+ * Perform cross validation under given parameters. The given samples are separated to n_fols folds.
58
+ * The predicted labels or values in the validation process are returned.
59
+ *
60
+ * @overload cv(x, y, param, n_folds) -> Numo::DFloat
61
+ *
62
+ * @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to be used for training the model.
63
+ * @param y [Numo::DFloat] (shape: [n_samples]) The labels or target values for samples.
64
+ * @param param [Hash] The parameters of a model.
65
+ * @param n_folds [Integer] The number of folds.
66
+ * @return [Numo::DFloat] (shape: [n_samples]) The predicted class label or value of each sample.
67
+ */
68
+ static
69
+ VALUE numo_liblinear_cross_validation(VALUE self, VALUE x_val, VALUE y_val, VALUE param_hash, VALUE nr_folds)
70
+ {
71
+ const int n_folds = NUM2INT(nr_folds);
72
+ size_t t_shape[1];
73
+ VALUE t_val;
74
+ double* t_pt;
75
+ struct problem* problem;
76
+ struct parameter* param;
77
+
78
+ if (CLASS_OF(x_val) != numo_cDFloat) {
79
+ x_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, x_val);
80
+ }
81
+ if (CLASS_OF(y_val) != numo_cDFloat) {
82
+ y_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, y_val);
83
+ }
84
+ if (!RTEST(nary_check_contiguous(x_val))) {
85
+ x_val = nary_dup(x_val);
86
+ }
87
+ if (!RTEST(nary_check_contiguous(y_val))) {
88
+ y_val = nary_dup(y_val);
89
+ }
90
+
91
+ param = rb_hash_to_parameter(param_hash);
92
+ problem = dataset_to_problem(x_val, y_val);
93
+
94
+ t_shape[0] = problem->l;
95
+ t_val = rb_narray_new(numo_cDFloat, 1, t_shape);
96
+ t_pt = (double*)na_get_pointer_for_write(t_val);
97
+
98
+ set_print_string_function(print_null);
99
+ cross_validation(problem, param, n_folds, t_pt);
100
+
101
+ xfree_problem(problem);
102
+ xfree_parameter(param);
103
+
104
+ return t_val;
105
+ }
106
+
107
+
108
+ /**
109
+ * Predict class labels or values for given samples.
110
+ *
111
+ * @overload predict(x, param, model) -> Numo::DFloat
112
+ *
113
+ * @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to calculate the scores.
114
+ * @param param [Hash] The parameters of the trained model.
115
+ * @param model [Hash] The model obtained from the training procedure.
116
+ * @return [Numo::DFloat] (shape: [n_samples]) The predicted class label or value of each sample.
117
+ */
118
+ static
119
+ VALUE numo_liblinear_predict(VALUE self, VALUE x_val, VALUE param_hash, VALUE model_hash)
120
+ {
121
+ struct parameter* param;
122
+ struct model* model;
123
+ struct feature_node* x_nodes;
124
+ narray_t* x_nary;
125
+ double* x_pt;
126
+ size_t y_shape[1];
127
+ VALUE y_val;
128
+ double* y_pt;
129
+ int i, j;
130
+ int n_samples;
131
+ int n_features;
132
+
133
+ /* Obtain C data structures. */
134
+ if (CLASS_OF(x_val) != numo_cDFloat) {
135
+ x_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, x_val);
136
+ }
137
+ if (!RTEST(nary_check_contiguous(x_val))) {
138
+ x_val = nary_dup(x_val);
139
+ }
140
+ GetNArray(x_val, x_nary);
141
+ param = rb_hash_to_parameter(param_hash);
142
+ model = rb_hash_to_model(model_hash);
143
+ model->param = *param;
144
+
145
+ /* Initialize some variables. */
146
+ n_samples = (int)NA_SHAPE(x_nary)[0];
147
+ n_features = (int)NA_SHAPE(x_nary)[1];
148
+ y_shape[0] = n_samples;
149
+ y_val = rb_narray_new(numo_cDFloat, 1, y_shape);
150
+ y_pt = (double*)na_get_pointer_for_write(y_val);
151
+ x_pt = (double*)na_get_pointer_for_read(x_val);
152
+
153
+ /* Predict values. */
154
+ x_nodes = ALLOC_N(struct feature_node, n_features + 1);
155
+ x_nodes[n_features].index = -1;
156
+ x_nodes[n_features].value = 0.0;
157
+ for (i = 0; i < n_samples; i++) {
158
+ for (j = 0; j < n_features; j++) {
159
+ x_nodes[j].index = j + 1;
160
+ x_nodes[j].value = (double)x_pt[i * n_features + j];
161
+ }
162
+ y_pt[i] = predict(model, x_nodes);
163
+ }
164
+
165
+ xfree(x_nodes);
166
+ xfree_model(model);
167
+ xfree_parameter(param);
168
+
169
+ return y_val;
170
+ }
171
+
172
+ /**
173
+ * Calculate decision values for given samples.
174
+ *
175
+ * @overload decision_function(x, param, model) -> Numo::DFloat
176
+ *
177
+ * @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to calculate the scores.
178
+ * @param param [Hash] The parameters of the trained model.
179
+ * @param model [Hash] The model obtained from the training procedure.
180
+ * @return [Numo::DFloat] (shape: [n_samples, n_classes]) The decision value of each sample.
181
+ */
182
+ static
183
+ VALUE numo_liblinear_decision_function(VALUE self, VALUE x_val, VALUE param_hash, VALUE model_hash)
184
+ {
185
+ struct parameter* param;
186
+ struct model* model;
187
+ struct feature_node* x_nodes;
188
+ narray_t* x_nary;
189
+ double* x_pt;
190
+ size_t y_shape[2];
191
+ VALUE y_val;
192
+ double* y_pt;
193
+ double* dec_values;
194
+ int y_cols;
195
+ int i, j;
196
+ int n_samples;
197
+ int n_features;
198
+
199
+ /* Obtain C data structures. */
200
+ if (CLASS_OF(x_val) != numo_cDFloat) {
201
+ x_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, x_val);
202
+ }
203
+ if (!RTEST(nary_check_contiguous(x_val))) {
204
+ x_val = nary_dup(x_val);
205
+ }
206
+ GetNArray(x_val, x_nary);
207
+ param = rb_hash_to_parameter(param_hash);
208
+ model = rb_hash_to_model(model_hash);
209
+ model->param = *param;
210
+
211
+ /* Initialize some variables. */
212
+ n_samples = (int)NA_SHAPE(x_nary)[0];
213
+ n_features = (int)NA_SHAPE(x_nary)[1];
214
+
215
+ if (model->nr_class == 2 && model->param.solver_type != MCSVM_CS) {
216
+ y_shape[0] = n_samples;
217
+ y_shape[1] = 1;
218
+ y_val = rb_narray_new(numo_cDFloat, 1, y_shape);
219
+ } else {
220
+ y_shape[0] = n_samples;
221
+ y_shape[1] = model->nr_class;
222
+ y_val = rb_narray_new(numo_cDFloat, 2, y_shape);
223
+ }
224
+
225
+ x_pt = (double*)na_get_pointer_for_read(x_val);
226
+ y_pt = (double*)na_get_pointer_for_write(y_val);
227
+
228
+ /* Predict values. */
229
+ if (model->nr_class == 2 && model->param.solver_type != MCSVM_CS) {
230
+ x_nodes = ALLOC_N(struct feature_node, n_features + 1);
231
+ x_nodes[n_features].index = -1;
232
+ x_nodes[n_features].value = 0.0;
233
+ for (i = 0; i < n_samples; i++) {
234
+ for (j = 0; j < n_features; j++) {
235
+ x_nodes[j].index = j + 1;
236
+ x_nodes[j].value = (double)x_pt[i * n_features + j];
237
+ }
238
+ predict_values(model, x_nodes, &y_pt[i]);
239
+ }
240
+ xfree(x_nodes);
241
+ } else {
242
+ y_cols = (int)y_shape[1];
243
+ dec_values = ALLOC_N(double, y_cols);
244
+ x_nodes = ALLOC_N(struct feature_node, n_features + 1);
245
+ x_nodes[n_features].index = -1;
246
+ x_nodes[n_features].value = 0.0;
247
+ for (i = 0; i < n_samples; i++) {
248
+ for (j = 0; j < n_features; j++) {
249
+ x_nodes[j].index = j + 1;
250
+ x_nodes[j].value = (double)x_pt[i * n_features + j];
251
+ }
252
+ predict_values(model, x_nodes, dec_values);
253
+ for (j = 0; j < y_cols; j++) {
254
+ y_pt[i * y_cols + j] = dec_values[j];
255
+ }
256
+ }
257
+ xfree(x_nodes);
258
+ xfree(dec_values);
259
+ }
260
+
261
+ xfree_model(model);
262
+ xfree_parameter(param);
263
+
264
+ return y_val;
265
+ }
266
+
267
+ /**
268
+ * Predict class probability for given samples.
269
+ * The model must have probability information calcualted in training procedure.
270
+ * The method supports only the logistic regression.
271
+ *
272
+ * @overload predict_proba(x, param, model) -> Numo::DFloat
273
+ *
274
+ * @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the class probabilities.
275
+ * @param param [Hash] The parameters of the trained Logistic Regression model.
276
+ * @param model [Hash] The model obtained from the training procedure.
277
+ * @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probablity of each class per sample.
278
+ */
279
+ static
280
+ VALUE numo_liblinear_predict_proba(VALUE self, VALUE x_val, VALUE param_hash, VALUE model_hash)
281
+ {
282
+ struct parameter* param;
283
+ struct model* model;
284
+ struct feature_node* x_nodes;
285
+ narray_t* x_nary;
286
+ double* x_pt;
287
+ size_t y_shape[2];
288
+ VALUE y_val = Qnil;
289
+ double* y_pt;
290
+ double* probs;
291
+ int i, j;
292
+ int n_samples;
293
+ int n_features;
294
+
295
+ param = rb_hash_to_parameter(param_hash);
296
+ model = rb_hash_to_model(model_hash);
297
+ model->param = *param;
298
+
299
+ if (model->param.solver_type == L2R_LR || model->param.solver_type == L1R_LR || model->param.solver_type == L2R_LR_DUAL) {
300
+ /* Obtain C data structures. */
301
+ if (CLASS_OF(x_val) != numo_cDFloat) {
302
+ x_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, x_val);
303
+ }
304
+ if (!RTEST(nary_check_contiguous(x_val))) {
305
+ x_val = nary_dup(x_val);
306
+ }
307
+ GetNArray(x_val, x_nary);
308
+
309
+ /* Initialize some variables. */
310
+ n_samples = (int)NA_SHAPE(x_nary)[0];
311
+ n_features = (int)NA_SHAPE(x_nary)[1];
312
+ y_shape[0] = n_samples;
313
+ y_shape[1] = model->nr_class;
314
+ y_val = rb_narray_new(numo_cDFloat, 2, y_shape);
315
+ x_pt = (double*)na_get_pointer_for_read(x_val);
316
+ y_pt = (double*)na_get_pointer_for_write(y_val);
317
+
318
+ /* Predict values. */
319
+ probs = ALLOC_N(double, model->nr_class);
320
+ x_nodes = ALLOC_N(struct feature_node, n_features + 1);
321
+ x_nodes[n_features].index = -1;
322
+ x_nodes[n_features].value = 0.0;
323
+ for (i = 0; i < n_samples; i++) {
324
+ for (j = 0; j < n_features; j++) {
325
+ x_nodes[j].index = j + 1;
326
+ x_nodes[j].value = (double)x_pt[i * n_features + j];
327
+ }
328
+ predict_probability(model, x_nodes, probs);
329
+ for (j = 0; j < model->nr_class; j++) {
330
+ y_pt[i * model->nr_class + j] = probs[j];
331
+ }
332
+ }
333
+ xfree(x_nodes);
334
+ xfree(probs);
335
+ }
336
+
337
+ xfree_model(model);
338
+ xfree_parameter(param);
339
+
340
+ return y_val;
341
+ }
342
+
343
+ /**
344
+ * Load the parameters and model from a text file with LIBLINEAR format.
345
+ *
346
+ * @param filename [String] The path to a file to load.
347
+ * @return [Array] Array contains the parameters and model.
348
+ */
349
+ static
350
+ VALUE numo_liblinear_load_model(VALUE self, VALUE filename)
351
+ {
352
+ struct model* model = load_model(StringValuePtr(filename));
353
+ VALUE res = rb_ary_new2(2);
354
+ VALUE param_hash = Qnil;
355
+ VALUE model_hash = Qnil;
356
+
357
+ if (model) {
358
+ param_hash = parameter_to_rb_hash(&(model->param));
359
+ model_hash = model_to_rb_hash(model);
360
+ free_and_destroy_model(&model);
361
+ }
362
+
363
+ rb_ary_store(res, 0, param_hash);
364
+ rb_ary_store(res, 1, model_hash);
365
+
366
+ return res;
367
+ }
368
+
369
+ /**
370
+ * Save the parameters and model as a text file with LIBLINEAR format. The saved file can be used with the liblinear tools.
371
+ * Note that the save_model saves only the parameters necessary for estimation with the trained model.
372
+ *
373
+ * @overload save_model(filename, param, model) -> Boolean
374
+ *
375
+ * @param filename [String] The path to a file to save.
376
+ * @param param [Hash] The parameters of the trained model.
377
+ * @param model [Hash] The model obtained from the training procedure.
378
+ * @return [Boolean] true on success, or false if an error occurs.
379
+ */
380
+ static
381
+ VALUE numo_liblinear_save_model(VALUE self, VALUE filename, VALUE param_hash, VALUE model_hash)
382
+ {
383
+ struct parameter* param = rb_hash_to_parameter(param_hash);
384
+ struct model* model = rb_hash_to_model(model_hash);
385
+ int res;
386
+
387
+ model->param = *param;
388
+ res = save_model(StringValuePtr(filename), model);
389
+
390
+ xfree_model(model);
391
+ xfree_parameter(param);
392
+
393
+ return res < 0 ? Qfalse : Qtrue;
394
+ }
395
+
396
+ void Init_liblinearext()
397
+ {
398
+ rb_require("numo/narray");
399
+
400
+ /**
401
+ * Document-module: Numo
402
+ * Numo is the top level namespace of NUmerical MOdules for Ruby.
403
+ */
404
+ mNumo = rb_define_module("Numo");
405
+
406
+ /**
407
+ * Document-module: Numo::Liblinear
408
+ * Numo::Liblinear is a binding library for LIBLINEAR that handles dataset with Numo::NArray.
409
+ */
410
+ mLiblinear = rb_define_module_under(mNumo, "Liblinear");
411
+
412
+ rb_define_module_function(mLiblinear, "train", numo_liblinear_train, 3);
413
+ rb_define_module_function(mLiblinear, "cv", numo_liblinear_cross_validation, 4);
414
+ rb_define_module_function(mLiblinear, "predict", numo_liblinear_predict, 3);
415
+ rb_define_module_function(mLiblinear, "decision_function", numo_liblinear_decision_function, 3);
416
+ rb_define_module_function(mLiblinear, "predict_proba", numo_liblinear_predict_proba, 3);
417
+ rb_define_module_function(mLiblinear, "load_model", numo_liblinear_load_model, 1);
418
+ rb_define_module_function(mLiblinear, "save_model", numo_liblinear_save_model, 3);
419
+
420
+ rb_init_solver_type_module();
421
+ }