Class: Libsvm::Model

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cross_validation(problem, parameter, num_fold) ⇒ Object



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'ext/rb-libsvm/libsvm.c', line 395

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 = (double *)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



386
387
388
389
390
391
392
393
# File 'ext/rb-libsvm/libsvm.c', line 386

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



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'ext/rb-libsvm/libsvm.c', line 326

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



379
380
381
382
383
384
# File 'ext/rb-libsvm/libsvm.c', line 379

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



344
345
346
347
348
349
350
351
352
353
354
# File 'ext/rb-libsvm/libsvm.c', line 344

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);
  
  return rb_float_new(class);
}

#save(filename) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'ext/rb-libsvm/libsvm.c', line 356

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



372
373
374
375
376
377
# File 'ext/rb-libsvm/libsvm.c', line 372

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