Class: Libsvm::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/libsvm/model.rb,
ext/libsvm/libsvm.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cross_validation(problem, parameter, num_fold) ⇒ Array<Integer>

Perform a cross-validation with num_fold split of the training data contained in problem.

Parameters:

  • problem (Libsvm::Problem)

    the training set

  • parameter (Libsvm::SvmParameter)

    training parameters object

  • num_fold (Integer)

    the number of splits to devide the traning set into

Returns:

  • (Array<Integer>)

    the labels for each instance in training set


520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'ext/libsvm/libsvm.c', line 520

static VALUE cModel_class_cross_validation(VALUE cls, VALUE problem, VALUE parameter, VALUE num_fold)
{
  const struct svm_problem *prob;
  const struct svm_parameter *param;
  int nr_fold, i;
  double *target_ptr;
  VALUE target;

  Data_Get_Struct(problem, struct svm_problem, prob);
  Data_Get_Struct(parameter, struct svm_parameter, param);

  nr_fold = NUM2INT(num_fold);

  target = rb_ary_new2(prob->l);
  target_ptr = calloc(prob->l, sizeof(double));
  if(target_ptr == 0) {
    rb_raise(rb_eNoMemError, "on cross-validation result allocation" " %s:%i", __FILE__,__LINE__);
  }

  svm_cross_validation(prob, param, nr_fold, target_ptr);

  for(i = 0; i < prob->l; ++i) {
    rb_ary_push(target, rb_float_new(*(target_ptr+i)));
  }

  free(target_ptr);

  return target;
}

.load(filename) ⇒ Libsvm::Model

Load a Libsvm::Model from a file.

This load a model from file `filename`. Format is the LIBSVM internal file representation of the model.

Parameters:

  • filename (String)

    name of the model file

Returns:

Raises:

  • (IOError)

    if the model can't be loaded, e.g. because the model path doesn't point to a model


497
498
499
500
501
502
503
504
505
506
507
# File 'ext/libsvm/libsvm.c', line 497

static VALUE cModel_class_load(VALUE cls, VALUE filename)
{
  struct svm_model *model;
  char *path;
  path = StringValueCStr(filename);
  model = svm_load_model(path);
  if(model == NULL) {
    rb_raise(rb_eIOError, "unable to load model from file: \"%s\"", path);
  }
  return Data_Wrap_Struct(cModel, 0, model_free, model);
}

.train(problem, parameter) ⇒ Libsvm::Model

Train a model with given training set (problem) and training parameter object.

Library function svm_train

Parameters:

Returns:


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'ext/libsvm/libsvm.c', line 322

static VALUE cModel_class_train(VALUE obj,VALUE problem,VALUE parameter) {
  const struct svm_problem *prob;
  const struct svm_parameter *param;
  struct svm_model *model;
  const char *check_error;

  Data_Get_Struct(problem, struct svm_problem, prob);
  Data_Get_Struct(parameter, struct svm_parameter, param);

  check_error = svm_check_parameter(prob, param);
  if(check_error != NULL) {
    rb_raise(rb_eArgError, "Parameters not valid for Problem: '%s'", check_error);
  }
  model = svm_train(prob,param);

  return Data_Wrap_Struct(cModel, 0, model_free, model);
}

Instance Method Details

#classes_countInteger Also known as: classes

Number of classes the model is configured and trained to predict. For single-class or regression models 2 is returned.

Library function svm_get_nr_class.

Returns:

  • (Integer)

    the number of classes


462
463
464
465
466
467
# File 'ext/libsvm/libsvm.c', line 462

static VALUE cModel_classes_count(VALUE obj)
{
  const struct svm_model *model;
  Data_Get_Struct(obj, struct svm_model, model);
  return INT2NUM(svm_get_nr_class(model));
}

#predict(example) ⇒ Float

Classify an example and return the class (label).

This is the class (label) value for a classifier model or the funtion value for a regression model. 1 or -1 for one-class model.

Library function svm_predict.

Returns:

  • (Float)

    predicted class label


353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'ext/libsvm/libsvm.c', line 353

static VALUE cModel_predict(VALUE obj,VALUE example) {
  struct svm_node *x;
  struct svm_model *model;
  double class;

  x = example_to_internal(example);
  Data_Get_Struct(obj, struct svm_model, model);
  class = svm_predict(model, x);

  free(x);

  return rb_float_new(class);
}

#predict_probability(example) ⇒ Array<Float, Array<Float>>

Classify an example and return both the label (or regression value), as well as the array of probability found for each class.

The first element of the returned array contains the label. The second element is another array which contains the probability value found for each model class.

Library function svm_predict_probability.

Returns:

  • (Array<Float, Array<Float>>)

    predicted label and probability per class


383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'ext/libsvm/libsvm.c', line 383

static VALUE cModel_predict_probability(VALUE obj,VALUE example) {
  struct svm_node *x;
  struct svm_model *model;
  double class;
  double *c_estimates;
  VALUE estimates;
  VALUE target;
  int i;

  x = example_to_internal(example);
  Data_Get_Struct(obj, struct svm_model, model);
  c_estimates = calloc(model->nr_class, sizeof(double));
  if(c_estimates == 0) {
    rb_raise(rb_eNoMemError, "on predict probability estimates allocation" " %s:%i", __FILE__,__LINE__);
  }

  class = svm_predict_probability(model, x, c_estimates);

  estimates = rb_ary_new();
  for (i = 0; i < model->nr_class; i++)
    rb_ary_push(estimates, rx_from_double(c_estimates[i]));

  free(c_estimates);
  free(x);

  target = rb_ary_new();
  rb_ary_push(target, rb_float_new(class));
  rb_ary_push(target, estimates);
  return target;
}

#save(filename) ⇒ Object

Saves the model to file `filename`. The format is the LIBSVM internal model representation.

Library function svm_save_model.


423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'ext/libsvm/libsvm.c', line 423

static VALUE cModel_save(VALUE obj, VALUE filename)
{
  const struct svm_model *model;
  const char *path;
  int rc;

  Data_Get_Struct(obj, struct svm_model, model);
  path = StringValueCStr(filename);

  if((rc = svm_save_model(path, model))) {
    rb_raise(rb_eStandardError, "Error on saving model, code: %i", rc);
  }

  return Qnil;
}

#support_vectors_countInteger Also known as: support_vectors

Number of the support vectors the model contains.

This method binds to the function svm_get_nr_sv.

Returns:

  • (Integer)

    the number of support vectors


477
478
479
480
481
482
# File 'ext/libsvm/libsvm.c', line 477

static VALUE cModel_support_vectors_count(VALUE obj)
{
  const struct svm_model *model;
  Data_Get_Struct(obj, struct svm_model, model);
  return INT2NUM(svm_get_nr_sv(model));
}

#svm_typeC_SVC, ...

Type of the model. Integer value, one of the constants in the SvmType module.

Library function svm_get_svm_type.

Returns:

  • (C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR)

447
448
449
450
451
452
# File 'ext/libsvm/libsvm.c', line 447

static VALUE cModel_svm_type(VALUE obj)
{
  const struct svm_model *model;
  Data_Get_Struct(obj, struct svm_model, model);
  return INT2NUM(svm_get_svm_type(model));
}