Class: Libsvm::SvmParameter

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

Instance Method Summary collapse

Instance Method Details

#label_weightsObject



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'ext/rb-libsvm/libsvm.c', line 301

static VALUE cSvmParameter_label_weights(VALUE obj) {
  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]);
    rb_hash_aset(hash,key,val);
  }

  return hash;
}

#label_weights=(weight_hash) ⇒ Object

Label weight.

nr_weight, weight_label, and weight are used to change the penalty
for some classes (If the weight for a class is not changed, it is
set to 1). This is useful for training classifier using unbalanced
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.


271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'ext/rb-libsvm/libsvm.c', line 271

static VALUE cSvmParameter_label_weights_set(VALUE obj,VALUE weight_hash) {
  struct svm_parameter *param;
  int i,len,weight_label;
  double weight;
  VALUE keys,key,val;

  Data_Get_Struct(obj,struct svm_parameter,param);

  if(param->nr_weight > 0) {
    free(param->weight);
    free(param->weight_label);
  }

  param->nr_weight = rx_hash_size(weight_hash);
  param->weight = (double *)calloc(param->nr_weight,sizeof(double));
  param->weight_label = (int *)calloc(param->nr_weight,sizeof(int));

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