ext/libsvm/libsvm.c in rb-libsvm-1.1.3 vs ext/libsvm/libsvm.c in rb-libsvm-1.1.4
- old
+ new
@@ -25,38 +25,38 @@
n = calloc(1,sizeof(struct svm_node));
if(n == NULL)
return NULL;
return n;
}
-
+
static void node_free(struct svm_node *n) {
free(n);
}
static VALUE node_alloc(VALUE cls) {
struct svm_node *n;
n = node_new();
if(n == NULL)
rb_raise(rb_eNoMemError, "Not enough memory for allocating Node.");
-
+
return Data_Wrap_Struct(cls, 0, node_free, n);
}
rx_def_accessor(cNode,struct svm_node,int,index);
rx_def_accessor(cNode,struct svm_node,double,value);
-
+
/* Libsvm::Problem */
static struct svm_problem *problem_new() {
struct svm_problem *n;
n = calloc(1,sizeof(struct svm_problem));
if(n == NULL)
return NULL;
return n;
}
static void problem_free(struct svm_problem *n) {
- /*
+ /*
Deliberate no-op, because of this note from the README:
`*NOTE* Because svm_model contains pointers to svm_problem, you can
not free the memory used by svm_problem if you are still using the
svm_model produced by svm_train().'
@@ -107,43 +107,43 @@
x = calloc(num,sizeof(struct svm_node *));
if(x == 0) {
rb_raise(rb_eNoMemError, "%s:%i", __FILE__,__LINE__);
}
-
+
for(i = 0; i < num; ++i) {
nodes_ary = rb_ary_entry(examples_ary,i);
*(x+i) = example_to_internal(nodes_ary);
}
return x;
}
-/*
+/*
call-seq:
problem.set_examples(labels, examples_array)
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.
*/
-static VALUE cProblem_examples_set(VALUE obj,VALUE labels_ary,VALUE examples_ary)
+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);
-
+ 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));
}
@@ -163,30 +163,30 @@
prob->l = num;
return INT2FIX(num);
}
-/*
+/*
call-seq:
labels, array_of_arrays = problem.examples
double *y; // class/label of the example
- struct svm_node **x;
+ struct svm_node **x;
*/
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);
+ 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__);
}
@@ -223,22 +223,22 @@
n = calloc(1,sizeof(struct svm_parameter));
if(n == NULL)
return NULL;
return n;
}
-
+
static void parameter_free(struct svm_parameter *n) {
// svm_destroy_param(n);
free(n);
}
static VALUE parameter_alloc(VALUE cls) {
struct svm_parameter *n;
n = parameter_new();
if(n == NULL)
rb_raise(rb_eNoMemError, "Not enough memory for allocating SvmParameter.");
-
+
return Data_Wrap_Struct(cls, 0, parameter_free, n);
}
rx_def_accessor(cSvmParameter,struct svm_parameter,int,svm_type)
rx_def_accessor(cSvmParameter,struct svm_parameter,int,kernel_type);
@@ -257,11 +257,11 @@
input data or with asymmetric misclassification cost.
nr_weight is the number of elements in the array weight_label and
weight. Each weight[i] corresponds to weight_label[i], meaning that
the penalty of class weight_label[i] is scaled by a factor of weight[i].
-
+
If you do not want to change penalty for any of the classes,
just set nr_weight to 0.
*/
static VALUE cSvmParameter_label_weights_set(VALUE obj,VALUE weight_hash) {
@@ -283,25 +283,25 @@
keys = rb_funcall(weight_hash, rb_intern("keys"),0);
for(i = 0; i < param->nr_weight; ++i) {
key = rb_ary_entry(keys,i);
val = rb_hash_aref(weight_hash,key);
-
+
param->weight_label[i] = NUM2INT(key);
param->weight[i] = NUM2DBL(val);
}
return Qnil;
}
static VALUE cSvmParameter_label_weights(VALUE obj) {
- struct svm_parameter *param;
+ struct svm_parameter *param;
int i;
VALUE hash,key,val;
Data_Get_Struct(obj,struct svm_parameter,param);
-
+
hash = rb_hash_new();
for(i = 0; i < param->nr_weight; ++i) {
key = INT2NUM(param->weight_label[i]);
val = rb_float_new(param->weight[i]);
@@ -324,11 +324,11 @@
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);
@@ -383,30 +383,30 @@
static VALUE cModel_save(VALUE obj, VALUE filename)
{
const struct svm_model *model;
const char *path;
- int rc;
+ int rc;
Data_Get_Struct(obj, struct svm_model, model);
path = StringValueCStr(filename);
-
- if(rc = svm_save_model(path, model)) {
+
+ if((rc = svm_save_model(path, model))) {
rb_raise(rb_eStandardError, "Error on saving model, code: %i", rc);
}
-
+
return Qnil;
}
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));
}
-static VALUE cModel_classes(VALUE obj)
+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));
}
@@ -428,10 +428,10 @@
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) {