Class: Libsvm::Model

Inherits:
Object
  • Object
show all
Defined in:
ext/libsvm/libsvm.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cross_validation(problem, parameter, num_fold) ⇒ Object



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

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) ⇒ Object



414
415
416
417
418
419
420
421
# File 'ext/libsvm/libsvm.c', line 414

static VALUE cModel_class_load(VALUE cls, VALUE filename)
{
  struct svm_model *model;
  char *path;
  path = StringValueCStr(filename);
  model = svm_load_model(path);
  return Data_Wrap_Struct(cModel, 0, model_free, model);
}

.train(problem, parameter) ⇒ Object

Libsvm::Model



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

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

#classesObject



407
408
409
410
411
412
# File 'ext/libsvm/libsvm.c', line 407

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

#predict(example) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'ext/libsvm/libsvm.c', line 339

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) ⇒ Object



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

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



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'ext/libsvm/libsvm.c', line 384

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;
}

#svm_typeObject



400
401
402
403
404
405
# File 'ext/libsvm/libsvm.c', line 400

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));
}