Class: Libsvm::Problem

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

Overview

Represents a training set (alternatively called ‘problem’).

In contains labels and examples in equal number.

This class represents the struct svm_problem.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lInteger (readonly)

The number of labels and examples in this traning set. (The name is lowercase-L.)

Returns:

  • (Integer)


# File 'lib/libsvm/problem.rb', line 13

Instance Method Details

#examples[Array<Float>, Array<Array<Node>>]

Returns the array of labels and the array of corresponding examples contained in this training set.

labels, array_of_examples = problem.examples

Returns:



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

static VALUE cProblem_examples(VALUE problem) {
  struct svm_problem *prob;
  struct svm_node *node, *node_copy;
  double label;
  struct svm_node *features;
  VALUE labels_ary, examples_ary, example_ary, v_node, result;
  int i;

  Data_Get_Struct(problem, struct svm_problem, prob);

  labels_ary = rb_ary_new2(prob->l);
  examples_ary = rb_ary_new2(prob->l);

  features = calloc(prob->l, sizeof(struct svm_node));
  if(features == 0) {
    rb_raise(rb_eNoMemError, "on allocating Libsvm::Node" " %s:%i", __FILE__,__LINE__);
  }

  for(i = 0; i < prob->l; ++i) {
    label = *(prob->y+i);
    rb_ary_push(labels_ary,rb_float_new(label));

    node = *(prob->x+i); /* example start pointer */
    example_ary = rb_ary_new();
    while(node->index != -1) {
      node_copy = malloc(sizeof(struct svm_node));
      if(node_copy == 0) {
	rb_raise(rb_eNoMemError, "on allocating Libsvm::Node" " %s:%i", __FILE__,__LINE__);
      }
      memcpy(node_copy,node,sizeof(struct svm_node));
      v_node = Data_Wrap_Struct(cNode,0,node_free,node_copy);
      rb_ary_push(example_ary,v_node);
      ++node;
    }
    rb_ary_push(examples_ary,example_ary);
  }

  result = rb_ary_new2(2);
  rb_ary_push(result,labels_ary);
  rb_ary_push(result,examples_ary);

  return result;
}

#set_examples(labels, examples_array) ⇒ Integer

Sets the examples and their label for a training set.

The indices of the arrays are supposed to correspond. If they don’t match in length an ArgumentError is raised.

Returns:

  • (Integer)

    number of examples in the traning set



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'ext/libsvm/libsvm.c', line 132

static VALUE cProblem_examples_set(VALUE obj,VALUE labels_ary,VALUE examples_ary)
{
  struct svm_problem *prob;
  int i;

  int num = rx_ary_size(labels_ary);

  if(num != rx_ary_size(examples_ary)) {
    rb_raise(rb_eArgError, "Number of labels (%i) does not match number of features (%i).", num, rx_ary_size(examples_ary));
  }

  Data_Get_Struct(obj, struct svm_problem, prob);

  if(prob->l > 0) {
    free(prob->y);
    for(i = 0; i < num; ++i) {
      free(*(prob->x+i));
    }
    free(prob->x);
  }

  prob->y = calloc(num,sizeof(double));
  if(prob->y == 0) {
    rb_raise(rb_eNoMemError, "%s:%i", __FILE__,__LINE__);
  }

  for(i = 0; i < num; ++i) {
    *(prob->y+i) =  NUM2DBL(rb_ary_entry(labels_ary,i));
  }

  prob->x = examples_ary_to_internal(examples_ary);
  prob->l = num;

  return INT2FIX(num);
}