Class: Libsvm::Problem
- Inherits:
-
Object
- Object
- Libsvm::Problem
- Defined in:
- ext/rb-libsvm/libsvm.c
Instance Method Summary collapse
-
#examples ⇒ Object
labels, array_of_arrays = problem.examples.
-
#set_examples(labels, examples_array) ⇒ Object
double *y; // class (aka. label) of the example struct svm_node **x; // examples.
Instance Method Details
#examples ⇒ Object
labels, array_of_arrays = problem.examples
double *y; // class/label of the example struct svm_node **x;
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/rb-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,n;
Data_Get_Struct(problem, struct svm_problem, prob);
labels_ary = rb_ary_new2(prob->l);
examples_ary = rb_ary_new2(prob->l);
features = (struct svm_node *)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 = (struct svm_node *)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) ⇒ Object
double *y; // class (aka. label) of the example
struct svm_node **x; // examples
This method sets the contents of an SVM Problem, which consists of lables (or classifications) and examples (or feature vectors). If those 2 don’t match in length and ArgumentError is raised.
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 167 168 169 170 |
# File 'ext/rb-libsvm/libsvm.c', line 133
static VALUE cProblem_examples_set(VALUE obj,VALUE labels_ary,VALUE examples_ary)
{
struct svm_problem *prob;
struct svm_node *node_struct;
/* VALUE num;*/
int i, nodes_ary_len;
VALUE label, node, nodes_ary;
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 = (double *)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);
}
|