Module: Numo::Libsvm

Defined in:
ext/numo/libsvm/libsvmext.c,
lib/numo/libsvm/version.rb,
ext/numo/libsvm/libsvmext.c

Overview

Numo::Libsvm is a binding library for LIBSVM that handles dataset with Numo::NArray.

Defined Under Namespace

Modules: KernelType, SvmType

Constant Summary collapse

VERSION =

The version of Numo::Libsvm you are using.

'0.4.0'
LIBSVM_VERSION =

The version of LIBSVM used in backgroud library.

INT2NUM(LIBSVM_VERSION)

Class Method Summary collapse

Class Method Details

.cv(x, y, param, n_folds) ⇒ Numo::DFloat

Perform cross validation under given parameters. The given samples are separated to n_fols folds. The predicted labels or values in the validation process are returned.

Examples:

require 'numo/libsvm'

# x: samples
# y: labels

# Define parameters of C-SVC with RBF Kernel.
param = {
  svm_type: Numo::Libsvm::SvmType::C_SVC,
  kernel_type: Numo::Libsvm::KernelType::RBF,
  gamma: 1.0,
  C: 1,
  random_seed: 1,
  verbose: true
}

# Perform 5-cross validation.
n_folds = 5
res = Numo::Libsvm.cv(x, y, param, n_folds)

# Print mean accuracy.
mean_accuracy = y.eq(res).count.fdiv(y.size)
puts "Accuracy: %.1f %%" % (100 * mean_accuracy)

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to be used for training the model.

  • y (Numo::DFloat)

    (shape: [n_samples]) The labels or target values for samples.

  • param (Hash)

    The parameters of an SVM model.

  • n_folds (Integer)

    The number of folds.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples]) The predicted class label or value of each sample.

Raises:

  • (ArgumentError)

    If the sample array is not 2-dimensional, the label array is not 1-dimensional, the sample array and label array do not have the same number of samples, or the hyperparameter has an invalid value, this error is raised.



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
# File 'ext/numo/libsvm/libsvmext.c', line 159

static
VALUE cross_validation(VALUE self, VALUE x_val, VALUE y_val, VALUE param_hash, VALUE nr_folds)
{
  const int n_folds = NUM2INT(nr_folds);
  size_t t_shape[1];
  VALUE t_val;
  double* t_pt;
  narray_t* x_nary;
  narray_t* y_nary;
  char* err_msg;
  VALUE random_seed;
  VALUE verbose;
  struct svm_problem* problem;
  struct svm_parameter* param;

  if (CLASS_OF(x_val) != numo_cDFloat) {
    x_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, x_val);
  }
  if (CLASS_OF(y_val) != numo_cDFloat) {
    y_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, y_val);
  }
  if (!RTEST(nary_check_contiguous(x_val))) {
    x_val = nary_dup(x_val);
  }
  if (!RTEST(nary_check_contiguous(y_val))) {
    y_val = nary_dup(y_val);
  }

  GetNArray(x_val, x_nary);
  GetNArray(y_val, y_nary);
  if (NA_NDIM(x_nary) != 2) {
    rb_raise(rb_eArgError, "Expect samples to be 2-D array.");
    return Qnil;
  }
  if (NA_NDIM(y_nary) != 1) {
    rb_raise(rb_eArgError, "Expect label or target values to be 1-D arrray.");
    return Qnil;
  }
  if (NA_SHAPE(x_nary)[0] != NA_SHAPE(y_nary)[0]) {
    rb_raise(rb_eArgError, "Expect to have the same number of samples for samples and labels.");
    return Qnil;
  }

  random_seed = rb_hash_aref(param_hash, ID2SYM(rb_intern("random_seed")));
  if (!NIL_P(random_seed)) {
    srand(NUM2UINT(random_seed));
  }

  param = rb_hash_to_svm_parameter(param_hash);
  problem = dataset_to_svm_problem(x_val, y_val);

  err_msg = svm_check_parameter(problem, param);
  if (err_msg) {
    xfree_svm_problem(problem);
    xfree_svm_parameter(param);
    rb_raise(rb_eArgError, "Invalid LIBSVM parameter is given: %s", err_msg);
    return Qnil;
  }

  t_shape[0] = problem->l;
  t_val = rb_narray_new(numo_cDFloat, 1, t_shape);
  t_pt = (double*)na_get_pointer_for_write(t_val);

  verbose = rb_hash_aref(param_hash, ID2SYM(rb_intern("verbose")));
  if (verbose != Qtrue) {
    svm_set_print_string_function(print_null);
  }

  svm_cross_validation(problem, param, n_folds, t_pt);

  xfree_svm_problem(problem);
  xfree_svm_parameter(param);

  return t_val;
}

.decision_function(x, param, model) ⇒ Numo::DFloat

Calculate decision values for given samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate the scores.

  • param (Hash)

    The parameters of the trained SVM model.

  • model (Hash)

    The model obtained from the training procedure.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes * (n_classes - 1) / 2]) The decision value of each sample.

Raises:

  • (ArgumentError)

    If the sample array is not 2-dimensional, this error is raised.



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
# File 'ext/numo/libsvm/libsvmext.c', line 317

static
VALUE decision_function(VALUE self, VALUE x_val, VALUE param_hash, VALUE model_hash)
{
  struct svm_parameter* param;
  struct svm_model* model;
  struct svm_node* x_nodes;
  narray_t* x_nary;
  double* x_pt;
  size_t y_shape[2];
  VALUE y_val;
  double* y_pt;
  double* dec_values;
  int y_cols;
  int i, j;
  int n_samples;
  int n_features;

  /* Obtain C data structures. */
  if (CLASS_OF(x_val) != numo_cDFloat) {
    x_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, x_val);
  }
  if (!RTEST(nary_check_contiguous(x_val))) {
    x_val = nary_dup(x_val);
  }

  GetNArray(x_val, x_nary);
  if (NA_NDIM(x_nary) != 2) {
    rb_raise(rb_eArgError, "Expect samples to be 2-D array.");
    return Qnil;
  }

  param = rb_hash_to_svm_parameter(param_hash);
  model = rb_hash_to_svm_model(model_hash);
  model->param = *param;

  /* Initialize some variables. */
  n_samples = (int)NA_SHAPE(x_nary)[0];
  n_features = (int)NA_SHAPE(x_nary)[1];

  if (model->param.svm_type == ONE_CLASS || model->param.svm_type == EPSILON_SVR || model->param.svm_type == NU_SVR) {
    y_shape[0] = n_samples;
    y_shape[1] = 1;
    y_val = rb_narray_new(numo_cDFloat, 1, y_shape);
  } else {
    y_shape[0] = n_samples;
    y_shape[1] = model->nr_class * (model->nr_class - 1) / 2;
    y_val = rb_narray_new(numo_cDFloat, 2, y_shape);
  }

  x_pt = (double*)na_get_pointer_for_read(x_val);
  y_pt = (double*)na_get_pointer_for_write(y_val);

  /* Predict values. */
  if (model->param.svm_type == ONE_CLASS || model->param.svm_type == EPSILON_SVR || model->param.svm_type == NU_SVR) {
    x_nodes = ALLOC_N(struct svm_node, n_features + 1);
    x_nodes[n_features].index = -1;
    x_nodes[n_features].value = 0.0;
    for (i = 0; i < n_samples; i++) {
      for (j = 0; j < n_features; j++) {
        x_nodes[j].index = j + 1;
        x_nodes[j].value = (double)x_pt[i * n_features + j];
      }
      svm_predict_values(model, x_nodes, &y_pt[i]);
    }
    xfree(x_nodes);
  } else {
    y_cols = (int)y_shape[1];
    dec_values = ALLOC_N(double, y_cols);
    x_nodes = ALLOC_N(struct svm_node, n_features + 1);
    x_nodes[n_features].index = -1;
    x_nodes[n_features].value = 0.0;
    for (i = 0; i < n_samples; i++) {
      for (j = 0; j < n_features; j++) {
        x_nodes[j].index = j + 1;
        x_nodes[j].value = (double)x_pt[i * n_features + j];
      }
      svm_predict_values(model, x_nodes, dec_values);
      for (j = 0; j < y_cols; j++) {
        y_pt[i * y_cols + j] = dec_values[j];
      }
    }
    xfree(x_nodes);
    xfree(dec_values);
  }

  xfree_svm_model(model);
  xfree_svm_parameter(param);

  return y_val;
}

.load_svm_model(filename) ⇒ Array

Load the SVM parameters and model from a text file with LIBSVM format.

Parameters:

  • filename (String)

    The path to a file to load.

Returns:

  • (Array)

    Array contains the SVM parameters and model.

Raises:

  • (IOError)

    This error raises when failed to load the model file.



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'ext/numo/libsvm/libsvmext.c', line 496

static
VALUE load_svm_model(VALUE self, VALUE filename)
{
  char* filename_ = StringValuePtr(filename);
  struct svm_model* model = svm_load_model(filename_);
  VALUE res = rb_ary_new2(2);
  VALUE param_hash = Qnil;
  VALUE model_hash = Qnil;

  if (model == NULL) {
    rb_raise(rb_eIOError, "Failed to load file '%s'", filename_);
    return Qnil;
  }

  if (model) {
    param_hash = svm_parameter_to_rb_hash(&(model->param));
    model_hash = svm_model_to_rb_hash(model);
    svm_free_and_destroy_model(&model);
  }

  rb_ary_store(res, 0, param_hash);
  rb_ary_store(res, 1, model_hash);

  return res;
}

.predict(x, param, model) ⇒ Numo::DFloat

Predict class labels or values for given samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate the scores.

  • param (Hash)

    The parameters of the trained SVM model.

  • model (Hash)

    The model obtained from the training procedure.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples]) The predicted class label or value of each sample.

Raises:

  • (ArgumentError)

    If the sample array is not 2-dimensional, this error is raised.



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
# File 'ext/numo/libsvm/libsvmext.c', line 246

static
VALUE predict(VALUE self, VALUE x_val, VALUE param_hash, VALUE model_hash)
{
  struct svm_parameter* param;
  struct svm_model* model;
  struct svm_node* x_nodes;
  narray_t* x_nary;
  double* x_pt;
  size_t y_shape[1];
  VALUE y_val;
  double* y_pt;
  int i, j;
  int n_samples;
  int n_features;

  /* Obtain C data structures. */
  if (CLASS_OF(x_val) != numo_cDFloat) {
    x_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, x_val);
  }
  if (!RTEST(nary_check_contiguous(x_val))) {
    x_val = nary_dup(x_val);
  }

  GetNArray(x_val, x_nary);
  if (NA_NDIM(x_nary) != 2) {
    rb_raise(rb_eArgError, "Expect samples to be 2-D array.");
    return Qnil;
  }

  param = rb_hash_to_svm_parameter(param_hash);
  model = rb_hash_to_svm_model(model_hash);
  model->param = *param;

  /* Initialize some variables. */
  n_samples = (int)NA_SHAPE(x_nary)[0];
  n_features = (int)NA_SHAPE(x_nary)[1];
  y_shape[0] = n_samples;
  y_val = rb_narray_new(numo_cDFloat, 1, y_shape);
  y_pt = (double*)na_get_pointer_for_write(y_val);
  x_pt = (double*)na_get_pointer_for_read(x_val);

  /* Predict values. */
  x_nodes = ALLOC_N(struct svm_node, n_features + 1);
  x_nodes[n_features].index = -1;
  x_nodes[n_features].value = 0.0;
  for (i = 0; i < n_samples; i++) {
    for (j = 0; j < n_features; j++) {
      x_nodes[j].index = j + 1;
      x_nodes[j].value = (double)x_pt[i * n_features + j];
    }
    y_pt[i] = svm_predict(model, x_nodes);
  }

  xfree(x_nodes);
  xfree_svm_model(model);
  xfree_svm_parameter(param);

  return y_val;
}

.predict_proba(x, param, model) ⇒ Numo::DFloat

Predict class probability for given samples. The model must have probability information calcualted in training procedure. The parameter ‘:probability’ set to 1 in training procedure.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to predict the class probabilities.

  • param (Hash)

    The parameters of the trained SVM model.

  • model (Hash)

    The model obtained from the training procedure.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes]) Predicted probablity of each class per sample.

Raises:

  • (ArgumentError)

    If the sample array is not 2-dimensional, this error is raised.



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'ext/numo/libsvm/libsvmext.c', line 420

static
VALUE predict_proba(VALUE self, VALUE x_val, VALUE param_hash, VALUE model_hash)
{
  struct svm_parameter* param;
  struct svm_model* model;
  struct svm_node* x_nodes;
  narray_t* x_nary;
  double* x_pt;
  size_t y_shape[2];
  VALUE y_val = Qnil;
  double* y_pt;
  double* probs;
  int i, j;
  int n_samples;
  int n_features;

  GetNArray(x_val, x_nary);
  if (NA_NDIM(x_nary) != 2) {
    rb_raise(rb_eArgError, "Expect samples to be 2-D array.");
    return Qnil;
  }

  param = rb_hash_to_svm_parameter(param_hash);
  model = rb_hash_to_svm_model(model_hash);
  model->param = *param;

  if ((model->param.svm_type == C_SVC || model->param.svm_type == NU_SVC) && model->probA != NULL && model->probB != NULL) {
    /* Obtain C data structures. */
    if (CLASS_OF(x_val) != numo_cDFloat) {
      x_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, x_val);
    }
    if (!RTEST(nary_check_contiguous(x_val))) {
      x_val = nary_dup(x_val);
    }

    /* Initialize some variables. */
    n_samples = (int)NA_SHAPE(x_nary)[0];
    n_features = (int)NA_SHAPE(x_nary)[1];
    y_shape[0] = n_samples;
    y_shape[1] = model->nr_class;
    y_val = rb_narray_new(numo_cDFloat, 2, y_shape);
    x_pt = (double*)na_get_pointer_for_read(x_val);
    y_pt = (double*)na_get_pointer_for_write(y_val);

    /* Predict values. */
    probs = ALLOC_N(double, model->nr_class);
    x_nodes = ALLOC_N(struct svm_node, n_features + 1);
    x_nodes[n_features].index = -1;
    x_nodes[n_features].value = 0.0;
    for (i = 0; i < n_samples; i++) {
      for (j = 0; j < n_features; j++) {
        x_nodes[j].index = j + 1;
        x_nodes[j].value = (double)x_pt[i * n_features + j];
      }
      svm_predict_probability(model, x_nodes, probs);
      for (j = 0; j < model->nr_class; j++) {
        y_pt[i * model->nr_class + j] = probs[j];
      }
    }
    xfree(x_nodes);
    xfree(probs);
  }

  xfree_svm_model(model);
  xfree_svm_parameter(param);

  return y_val;
}

.save_svm_model(filename, param, model) ⇒ Boolean

Save the SVM parameters and model as a text file with LIBSVM format. The saved file can be used with the libsvm tools. Note that the svm_save_model saves only the parameters necessary for estimation with the trained model.

Parameters:

  • filename (String)

    The path to a file to save.

  • param (Hash)

    The parameters of the trained SVM model.

  • model (Hash)

    The model obtained from the training procedure.

Returns:

  • (Boolean)

    true on success, or false if an error occurs.

Raises:

  • (IOError)

    This error raises when failed to save the model file.



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'ext/numo/libsvm/libsvmext.c', line 534

static
VALUE save_svm_model(VALUE self, VALUE filename, VALUE param_hash, VALUE model_hash)
{
  char* filename_ = StringValuePtr(filename);
  struct svm_parameter* param = rb_hash_to_svm_parameter(param_hash);
  struct svm_model* model = rb_hash_to_svm_model(model_hash);
  int res;

  model->param = *param;
  res = svm_save_model(filename_, model);

  xfree_svm_model(model);
  xfree_svm_parameter(param);

  if (res < 0) {
    rb_raise(rb_eIOError, "Failed to save file '%s'", filename_);
    return Qfalse;
  }

  return Qtrue;
}

.train(x, y, param) ⇒ Hash

Train the SVM model according to the given training data.

Examples:

require 'numo/libsvm'

# Prepare XOR data.
x = Numo::DFloat[[-0.8, -0.7], [0.9, 0.8], [-0.7, 0.9], [0.8, -0.9]]
y = Numo::Int32[-1, -1, 1, 1]

# Train C-Support Vector Classifier with RBF kernel.
param = {
  svm_type: Numo::Libsvm::SvmType::C_SVC,
  kernel_type: Numo::Libsvm::KernelType::RBF,
  gamma: 2.0,
  C: 1,
  random_seed: 1
}
model = Numo::Libsvm.train(x, y, param)

# Predict labels of test data.
x_test = Numo::DFloat[[-0.4, -0.5], [0.5, -0.4]]
result = Numo::Libsvm.predict(x_test, param, model)
p result
# Numo::DFloat#shape=[2]
# [-1, 1]

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to be used for training the model.

  • y (Numo::DFloat)

    (shape: [n_samples]) The labels or target values for samples.

  • param (Hash)

    The parameters of an SVM model.

Returns:

  • (Hash)

    The model obtained from the training procedure.

Raises:

  • (ArgumentError)

    If the sample array is not 2-dimensional, the label array is not 1-dimensional, the sample array and label array do not have the same number of samples, or the hyperparameter has an invalid value, this error is raised.



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
# File 'ext/numo/libsvm/libsvmext.c', line 48

static
VALUE train(VALUE self, VALUE x_val, VALUE y_val, VALUE param_hash)
{
  struct svm_problem* problem;
  struct svm_parameter* param;
  struct svm_model* model;
  narray_t* x_nary;
  narray_t* y_nary;
  char* err_msg;
  VALUE random_seed;
  VALUE verbose;
  VALUE model_hash;

  if (CLASS_OF(x_val) != numo_cDFloat) {
    x_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, x_val);
  }
  if (CLASS_OF(y_val) != numo_cDFloat) {
    y_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, y_val);
  }
  if (!RTEST(nary_check_contiguous(x_val))) {
    x_val = nary_dup(x_val);
  }
  if (!RTEST(nary_check_contiguous(y_val))) {
    y_val = nary_dup(y_val);
  }

  GetNArray(x_val, x_nary);
  GetNArray(y_val, y_nary);
  if (NA_NDIM(x_nary) != 2) {
    rb_raise(rb_eArgError, "Expect samples to be 2-D array.");
    return Qnil;
  }
  if (NA_NDIM(y_nary) != 1) {
    rb_raise(rb_eArgError, "Expect label or target values to be 1-D arrray.");
    return Qnil;
  }
  if (NA_SHAPE(x_nary)[0] != NA_SHAPE(y_nary)[0]) {
    rb_raise(rb_eArgError, "Expect to have the same number of samples for samples and labels.");
    return Qnil;
  }

  random_seed = rb_hash_aref(param_hash, ID2SYM(rb_intern("random_seed")));
  if (!NIL_P(random_seed)) {
    srand(NUM2UINT(random_seed));
  }

  param = rb_hash_to_svm_parameter(param_hash);
  problem = dataset_to_svm_problem(x_val, y_val);

  err_msg = svm_check_parameter(problem, param);
  if (err_msg) {
    xfree_svm_problem(problem);
    xfree_svm_parameter(param);
    rb_raise(rb_eArgError, "Invalid LIBSVM parameter is given: %s", err_msg);
    return Qnil;
  }

  verbose = rb_hash_aref(param_hash, ID2SYM(rb_intern("verbose")));
  if (verbose != Qtrue) {
    svm_set_print_string_function(print_null);
  }

  model = svm_train(problem, param);
  model_hash = svm_model_to_rb_hash(model);
  svm_free_and_destroy_model(&model);

  xfree_svm_problem(problem);
  xfree_svm_parameter(param);

  return model_hash;
}