Method: Libsvm::Model#predict_probability

Defined in:
ext/libsvm/libsvm.c

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