rb-libsvm 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +2 -2
- data/README.md +1 -1
- data/ext/libsvm/libsvm.c +135 -30
- data/lib/libsvm.rb +29 -0
- data/lib/libsvm/model.rb +9 -0
- data/lib/libsvm/node.rb +74 -5
- data/lib/libsvm/problem.rb +20 -0
- data/lib/libsvm/svm_parameter.rb +115 -0
- data/lib/libsvm/version.rb +1 -1
- data/spec/core_ext_spec.rb +15 -0
- data/spec/model_spec.rb +4 -4
- data/spec/node_spec.rb +2 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6628f92d9d2d643e194b46b8e225c5cade354eb
|
4
|
+
data.tar.gz: 58b9eee2167ec2d4eb60a6010c3d05fab7836c94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64ec0c4d492262ce7dbc4b28132c998a886e04b558beec111ee3118a7608732447d54641e5f9a400d7ed311d8cf8babfc07185e482359f998cd0d1174344fc78
|
7
|
+
data.tar.gz: 35956f1af2012ad765a3312b926f92e60af0c469924512db1325d0e52d97c6f9850cd8639fb02723615a6976a54d1c637ef51dd2baa232ee3c7eb6641f0091ce
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -25,7 +25,7 @@ this gem. You should install the original package if you need them.
|
|
25
25
|
It is helpful to consult the [README][] of the LIBSVM package for
|
26
26
|
reference when configuring the training parameters.
|
27
27
|
|
28
|
-
Currently this package includes libsvm version 3.
|
28
|
+
Currently this package includes libsvm version 3.20.
|
29
29
|
|
30
30
|
## Dependencies
|
31
31
|
|
data/ext/libsvm/libsvm.c
CHANGED
@@ -85,7 +85,7 @@ static struct svm_node *example_to_internal(VALUE example_ary)
|
|
85
85
|
if(x == 0) {
|
86
86
|
rb_raise(rb_eNoMemError, "on Libsvm::Node allocation" " %s:%i", __FILE__,__LINE__);
|
87
87
|
}
|
88
|
-
/* loop
|
88
|
+
/* loop its element nodes */
|
89
89
|
for(j = 0; j < example_ary_len; ++j) {
|
90
90
|
node = rb_ary_entry(example_ary,j);
|
91
91
|
Data_Get_Struct(node,struct svm_node,node_struct);
|
@@ -122,12 +122,12 @@ static struct svm_node **examples_ary_to_internal(VALUE examples_ary)
|
|
122
122
|
call-seq:
|
123
123
|
problem.set_examples(labels, examples_array)
|
124
124
|
|
125
|
-
|
126
|
-
struct svm_node **x; // examples
|
125
|
+
Sets the examples and their label for a training set.
|
127
126
|
|
128
|
-
|
129
|
-
|
130
|
-
|
127
|
+
The indices of the arrays are supposed to correspond. If they don't
|
128
|
+
match in length an ArgumentError is raised.
|
129
|
+
|
130
|
+
@return [Integer] number of examples in the traning set
|
131
131
|
*/
|
132
132
|
static VALUE cProblem_examples_set(VALUE obj,VALUE labels_ary,VALUE examples_ary)
|
133
133
|
{
|
@@ -167,10 +167,14 @@ static VALUE cProblem_examples_set(VALUE obj,VALUE labels_ary,VALUE examples_ary
|
|
167
167
|
|
168
168
|
/*
|
169
169
|
call-seq:
|
170
|
-
|
170
|
+
problem.examples
|
171
|
+
|
172
|
+
Returns the array of labels and the array of corresponding examples
|
173
|
+
contained in this training set.
|
174
|
+
|
175
|
+
labels, array_of_examples = problem.examples
|
171
176
|
|
172
|
-
|
173
|
-
struct svm_node **x;
|
177
|
+
@return [[Array<Float>, Array<Array<Node>>]]
|
174
178
|
*/
|
175
179
|
static VALUE cProblem_examples(VALUE problem) {
|
176
180
|
struct svm_problem *prob;
|
@@ -249,21 +253,6 @@ rx_def_accessor(cSvmParameter,struct svm_parameter,double,cache_size);
|
|
249
253
|
rx_def_accessor(cSvmParameter,struct svm_parameter,double,eps);
|
250
254
|
rx_def_accessor_as(cSvmParameter,struct svm_parameter,double,C,c);
|
251
255
|
|
252
|
-
/* Label weight.
|
253
|
-
|
254
|
-
nr_weight, weight_label, and weight are used to change the penalty
|
255
|
-
for some classes (If the weight for a class is not changed, it is
|
256
|
-
set to 1). This is useful for training classifier using unbalanced
|
257
|
-
input data or with asymmetric misclassification cost.
|
258
|
-
|
259
|
-
nr_weight is the number of elements in the array weight_label and
|
260
|
-
weight. Each weight[i] corresponds to weight_label[i], meaning that
|
261
|
-
the penalty of class weight_label[i] is scaled by a factor of weight[i].
|
262
|
-
|
263
|
-
If you do not want to change penalty for any of the classes,
|
264
|
-
just set nr_weight to 0.
|
265
|
-
|
266
|
-
*/
|
267
256
|
static VALUE cSvmParameter_label_weights_set(VALUE obj,VALUE weight_hash) {
|
268
257
|
struct svm_parameter *param;
|
269
258
|
int i;
|
@@ -316,8 +305,20 @@ rx_def_accessor(cSvmParameter,struct svm_parameter,double,p);
|
|
316
305
|
rx_def_accessor(cSvmParameter,struct svm_parameter,int,shrinking);
|
317
306
|
rx_def_accessor(cSvmParameter,struct svm_parameter,int,probability);
|
318
307
|
|
319
|
-
/*
|
320
|
-
|
308
|
+
/* call-seq:
|
309
|
+
* Model.train(problem, parameter)
|
310
|
+
*
|
311
|
+
* Train a model with given training set (problem) and training
|
312
|
+
* parameter object.
|
313
|
+
*
|
314
|
+
* Library function
|
315
|
+
* svm_train[https://github.com/cjlin1/libsvm/blob/master/README#L313]
|
316
|
+
*
|
317
|
+
* @param [Libsvm::Problem] the training set (problem)
|
318
|
+
* @param [Libsvm::SvmParameter] training parameter object
|
319
|
+
*
|
320
|
+
* @return [Libsvm::Model] the trained model
|
321
|
+
*/
|
321
322
|
static VALUE cModel_class_train(VALUE obj,VALUE problem,VALUE parameter) {
|
322
323
|
const struct svm_problem *prob;
|
323
324
|
const struct svm_parameter *param;
|
@@ -336,6 +337,19 @@ static VALUE cModel_class_train(VALUE obj,VALUE problem,VALUE parameter) {
|
|
336
337
|
return Data_Wrap_Struct(cModel, 0, model_free, model);
|
337
338
|
}
|
338
339
|
|
340
|
+
/* call-seq:
|
341
|
+
* model.predict(example)
|
342
|
+
*
|
343
|
+
* Classify an example and return the class (label).
|
344
|
+
*
|
345
|
+
* This is the class (label) value for a classifier model or the
|
346
|
+
* funtion value for a regression model. 1 or -1 for one-class model.
|
347
|
+
*
|
348
|
+
* Library function
|
349
|
+
* svm_predict[https://github.com/cjlin1/libsvm/blob/master/README#L511].
|
350
|
+
*
|
351
|
+
* @return [Float] predicted class label
|
352
|
+
*/
|
339
353
|
static VALUE cModel_predict(VALUE obj,VALUE example) {
|
340
354
|
struct svm_node *x;
|
341
355
|
struct svm_model *model;
|
@@ -350,6 +364,22 @@ static VALUE cModel_predict(VALUE obj,VALUE example) {
|
|
350
364
|
return rb_float_new(class);
|
351
365
|
}
|
352
366
|
|
367
|
+
/* call-seq:
|
368
|
+
* model.predict_probability(example)
|
369
|
+
*
|
370
|
+
* Classify an example and return both the label (or regression
|
371
|
+
* value), as well as the array of probability found for each class.
|
372
|
+
*
|
373
|
+
* The first element of the returned array contains the label. The
|
374
|
+
* second element is another array which contains the probability
|
375
|
+
* value found for each model class.
|
376
|
+
*
|
377
|
+
* Library function
|
378
|
+
* svm_predict_probability[https://github.com/cjlin1/libsvm/blob/master/README#L591].
|
379
|
+
*
|
380
|
+
* @return [Array<Float, Array<Float>>] predicted label and
|
381
|
+
* probability per class
|
382
|
+
*/
|
353
383
|
static VALUE cModel_predict_probability(VALUE obj,VALUE example) {
|
354
384
|
struct svm_node *x;
|
355
385
|
struct svm_model *model;
|
@@ -381,6 +411,15 @@ static VALUE cModel_predict_probability(VALUE obj,VALUE example) {
|
|
381
411
|
return target;
|
382
412
|
}
|
383
413
|
|
414
|
+
/* call-seq:
|
415
|
+
* model.save(filename)
|
416
|
+
*
|
417
|
+
* Saves the model to file `filename`. The format is the LIBSVM
|
418
|
+
* internal model representation.
|
419
|
+
*
|
420
|
+
* Library function
|
421
|
+
* svm_save_model[https://github.com/cjlin1/libsvm/blob/master/README#L621].
|
422
|
+
*/
|
384
423
|
static VALUE cModel_save(VALUE obj, VALUE filename)
|
385
424
|
{
|
386
425
|
const struct svm_model *model;
|
@@ -397,6 +436,14 @@ static VALUE cModel_save(VALUE obj, VALUE filename)
|
|
397
436
|
return Qnil;
|
398
437
|
}
|
399
438
|
|
439
|
+
/* Type of the model. Integer value, one of the constants in the
|
440
|
+
* {Libsvm::SvmType} module.
|
441
|
+
*
|
442
|
+
* Library function
|
443
|
+
* svm_get_svm_type[https://github.com/cjlin1/libsvm/blob/master/README#L533].
|
444
|
+
*
|
445
|
+
* @return [C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR]
|
446
|
+
*/
|
400
447
|
static VALUE cModel_svm_type(VALUE obj)
|
401
448
|
{
|
402
449
|
const struct svm_model *model;
|
@@ -404,20 +451,47 @@ static VALUE cModel_svm_type(VALUE obj)
|
|
404
451
|
return INT2NUM(svm_get_svm_type(model));
|
405
452
|
}
|
406
453
|
|
407
|
-
|
454
|
+
/* Number of classes the model is configured and trained to
|
455
|
+
* predict. For single-class or regression models 2 is returned.
|
456
|
+
*
|
457
|
+
* Library function
|
458
|
+
* svm_get_nr_class[https://github.com/cjlin1/libsvm/blob/master/README#L538].
|
459
|
+
*
|
460
|
+
* @return [Integer] the number of classes
|
461
|
+
*/
|
462
|
+
static VALUE cModel_classes_count(VALUE obj)
|
408
463
|
{
|
409
464
|
const struct svm_model *model;
|
410
465
|
Data_Get_Struct(obj, struct svm_model, model);
|
411
466
|
return INT2NUM(svm_get_nr_class(model));
|
412
467
|
}
|
413
468
|
|
414
|
-
|
469
|
+
/*
|
470
|
+
* Number of the support vectors the model contains.
|
471
|
+
*
|
472
|
+
* This method binds to the function
|
473
|
+
* svm_get_nr_sv[https://github.com/cjlin1/libsvm/blob/master/README#L555].
|
474
|
+
*
|
475
|
+
* @return [Integer] the number of support vectors
|
476
|
+
*/
|
477
|
+
static VALUE cModel_support_vectors_count(VALUE obj)
|
415
478
|
{
|
416
479
|
const struct svm_model *model;
|
417
480
|
Data_Get_Struct(obj, struct svm_model, model);
|
418
481
|
return INT2NUM(svm_get_nr_sv(model));
|
419
482
|
}
|
420
483
|
|
484
|
+
/* call-seq:
|
485
|
+
* Model.load(filename) # => model
|
486
|
+
*
|
487
|
+
* Load a {Libsvm::Model} from a file
|
488
|
+
*
|
489
|
+
* This load a model from file `filename`. Format is the LIBSVM
|
490
|
+
* internal file representation of the model.
|
491
|
+
*
|
492
|
+
* @param filename [String] name of the model file
|
493
|
+
* @return [Libsvm::Model] the model
|
494
|
+
*/
|
421
495
|
static VALUE cModel_class_load(VALUE cls, VALUE filename)
|
422
496
|
{
|
423
497
|
struct svm_model *model;
|
@@ -427,6 +501,17 @@ static VALUE cModel_class_load(VALUE cls, VALUE filename)
|
|
427
501
|
return Data_Wrap_Struct(cModel, 0, model_free, model);
|
428
502
|
}
|
429
503
|
|
504
|
+
/* call-seq:
|
505
|
+
* Model.cross_validation(problem, parameter, num_fold)
|
506
|
+
*
|
507
|
+
* Perform a cross-validation with num_fold split of the training data
|
508
|
+
* contained in problem.
|
509
|
+
*
|
510
|
+
* @param problem [Libsvm::Problem] the training set
|
511
|
+
* @param parameter [Libsvm::SvmParameter] training parameters object
|
512
|
+
* @param num_fold [Integer] the number of splits to devide the traning set into
|
513
|
+
* @return [Array<Integer>] the labels for each instance in training set
|
514
|
+
*/
|
430
515
|
static VALUE cModel_class_cross_validation(VALUE cls, VALUE problem, VALUE parameter, VALUE num_fold)
|
431
516
|
{
|
432
517
|
const struct svm_problem *prob;
|
@@ -499,16 +584,36 @@ void Init_libsvm_ext() {
|
|
499
584
|
rb_define_singleton_method(cModel, "load", cModel_class_load, 1);
|
500
585
|
rb_define_method(cModel, "save", cModel_save, 1);
|
501
586
|
rb_define_method(cModel, "svm_type", cModel_svm_type, 0);
|
502
|
-
rb_define_method(cModel, "
|
503
|
-
rb_define_method(cModel, "
|
587
|
+
rb_define_method(cModel, "classes_count", cModel_classes_count, 0);
|
588
|
+
rb_define_method(cModel, "support_vectors_count", cModel_support_vectors_count, 0);
|
504
589
|
rb_define_method(cModel, "predict", cModel_predict, 1);
|
505
590
|
rb_define_method(cModel, "predict_probability", cModel_predict_probability, 1);
|
506
591
|
|
592
|
+
/**
|
593
|
+
* Module with constants for values that are allowed for
|
594
|
+
* {Libsvm::SvmParameter#kernel_type}. The value controls what kind
|
595
|
+
* of kernel is used when training the model.
|
596
|
+
*/
|
507
597
|
mKernelType = rb_define_module_under(mLibsvm, "KernelType");
|
598
|
+
/**
|
599
|
+
* A linear kernel
|
600
|
+
*/
|
508
601
|
rb_define_const(mKernelType, "LINEAR", INT2NUM(LINEAR));
|
602
|
+
/**
|
603
|
+
* A polynomial kernel
|
604
|
+
*/
|
509
605
|
rb_define_const(mKernelType, "POLY", INT2NUM(POLY));
|
606
|
+
/**
|
607
|
+
* A redial basis function kernel
|
608
|
+
*/
|
510
609
|
rb_define_const(mKernelType, "RBF", INT2NUM(RBF));
|
610
|
+
/**
|
611
|
+
* A sigmoid kernel
|
612
|
+
*/
|
511
613
|
rb_define_const(mKernelType, "SIGMOID", INT2NUM(SIGMOID));
|
614
|
+
/**
|
615
|
+
* A precomputed kernel
|
616
|
+
*/
|
512
617
|
rb_define_const(mKernelType, "PRECOMPUTED", INT2NUM(PRECOMPUTED));
|
513
618
|
|
514
619
|
mSvmType = rb_define_module_under(mLibsvm,"SvmType");
|
data/lib/libsvm.rb
CHANGED
@@ -3,8 +3,36 @@ require 'libsvm/libsvm_ext'
|
|
3
3
|
require 'libsvm/node'
|
4
4
|
|
5
5
|
module Libsvm
|
6
|
+
|
7
|
+
# Module with constants for values that allowed for
|
8
|
+
# {Libsvm::SvmParameter#svm_type}. The value controls which kind of
|
9
|
+
# model is being trained.
|
10
|
+
#
|
11
|
+
# See the relevant section in the LIBSVM
|
12
|
+
# README[https://github.com/cjlin1/libsvm/blob/master/README#389]
|
13
|
+
# for more information.
|
14
|
+
module SvmType; end
|
15
|
+
|
6
16
|
module CoreExtensions
|
7
17
|
module Collection
|
18
|
+
# @!method to_example
|
19
|
+
#
|
20
|
+
# Converts this collection into an array of feature.
|
21
|
+
#
|
22
|
+
# { 1 => 0.4, 3 => 0.5, 5 => 0.9 }.to_example
|
23
|
+
# # => [#<Libsvm::Node: index=1, value=0.4>, #<Libsvm::Node: index=3, value=0.5>, #<Libsvm::Node: index=5, value=0.9>]
|
24
|
+
# { 1 => 0.4, 3 => 0.5, 5 => 0.9 }.to_example
|
25
|
+
# # => [#<Libsvm::Node: index=1, value=0.4>, #<Libsvm::Node: index=3, value=0.5>, #<Libsvm::Node: index=5, value=0.9>]
|
26
|
+
# [0.4, 0.5, 0.9].to_example
|
27
|
+
# # => [#<Libsvm::Node: index=0, value=0.4>, #<Libsvm::Node: index=1, value=0.5>, #<Libsvm::Node: index=2, value=0.9>]
|
28
|
+
# [ [1, 0.4], [3, 0.5], [5, 0.9] ].to_example
|
29
|
+
# # => [#<Libsvm::Node: index=1, value=0.4>, #<Libsvm::Node: index=3, value=0.5>, #<Libsvm::Node: index=5, value=0.9>]
|
30
|
+
#
|
31
|
+
# An array of features is the type used for classification and model learning.
|
32
|
+
#
|
33
|
+
# @note In an upcoming major release this core extension will become no longer included in Hash and Array, but rather be optional.
|
34
|
+
#
|
35
|
+
# @return [Array<Libsvm::Node>]
|
8
36
|
def to_example
|
9
37
|
Node.features(self)
|
10
38
|
end
|
@@ -15,6 +43,7 @@ end
|
|
15
43
|
class Hash
|
16
44
|
include Libsvm::CoreExtensions::Collection
|
17
45
|
end
|
46
|
+
|
18
47
|
class Array
|
19
48
|
include Libsvm::CoreExtensions::Collection
|
20
49
|
end
|
data/lib/libsvm/model.rb
ADDED
data/lib/libsvm/node.rb
CHANGED
@@ -1,15 +1,58 @@
|
|
1
1
|
module Libsvm
|
2
|
+
# Represents a feature
|
3
|
+
#
|
4
|
+
# A feature has an index and a value.
|
5
|
+
#
|
6
|
+
# An array of features (Libsvm::Node instances) represents an
|
7
|
+
# example which can be classified, or in greater number can
|
8
|
+
# constitute the main part of a training set (Libsvm::Prolem).
|
9
|
+
#
|
10
|
+
# This class represents the struct
|
11
|
+
# svm_node[https://github.com/cjlin1/libsvm/blob/master/README#L357].
|
12
|
+
#
|
13
|
+
# @see Libsvm::Problem Libsvm::Problem, the training set class
|
2
14
|
class Node
|
3
15
|
class << self
|
16
|
+
# Create an array of features from collection or variable number
|
17
|
+
# of float values.
|
18
|
+
#
|
19
|
+
# Indices are converted to Integer, values are converted to
|
20
|
+
# Float. When passed a variable number of Float values or an
|
21
|
+
# array of Floast, the index is implied by the arguments
|
22
|
+
# position. Collection of tuples of hash arguments afford sparse
|
23
|
+
# feature arrays. These can represent features which don't have
|
24
|
+
# consecutive indices starting at zero.
|
25
|
+
#
|
26
|
+
# @overload features(0.3, 0.7, 0.8, ...)
|
27
|
+
# @param vararg variable number of value arguments, or
|
28
|
+
#
|
29
|
+
# @overload features([0.3, 0.7, 0.8, ...])
|
30
|
+
# @param array an array of values, or
|
31
|
+
#
|
32
|
+
# @overload features([[0, 0.3], [1, 0.7], [2, 0.8], ...])
|
33
|
+
# @param array an array of [index, value] tuples, or
|
34
|
+
#
|
35
|
+
# @overload features(0 => 0.3, 1 => 0.7, 2 => 0.8, 99 => 0.1)
|
36
|
+
# @param hash a hash from index to value
|
37
|
+
#
|
38
|
+
# @return [Array<Libsvm::Node>] example, i.e. an array of features
|
4
39
|
def features(*vargs)
|
5
40
|
array_of_nodes = []
|
6
41
|
if vargs.size == 1
|
7
|
-
|
8
|
-
|
9
|
-
|
42
|
+
argument = vargs.first
|
43
|
+
if argument.class == Array
|
44
|
+
case argument.first
|
45
|
+
when Array
|
46
|
+
argument.each do |pair|
|
47
|
+
array_of_nodes << Node.new(pair.first.to_i, pair.last.to_f)
|
48
|
+
end
|
49
|
+
else
|
50
|
+
argument.each_with_index do |value, index|
|
51
|
+
array_of_nodes << Node.new(index.to_i, value.to_f)
|
52
|
+
end
|
10
53
|
end
|
11
|
-
elsif
|
12
|
-
|
54
|
+
elsif argument.class == Hash
|
55
|
+
argument.each do |index, value|
|
13
56
|
array_of_nodes << Node.new(index.to_i, value.to_f)
|
14
57
|
end
|
15
58
|
else
|
@@ -23,18 +66,44 @@ module Libsvm
|
|
23
66
|
array_of_nodes
|
24
67
|
end
|
25
68
|
|
69
|
+
# Create a feature node.
|
70
|
+
#
|
71
|
+
# Libsvm::Node[0, 1.1] # => #<Libsvm::Node: index=0, value=1.1>
|
72
|
+
#
|
73
|
+
# @return [Libsvm::Node]
|
26
74
|
def [](index, value)
|
27
75
|
new(index, value)
|
28
76
|
end
|
29
77
|
end
|
30
78
|
|
79
|
+
# @!attribute index
|
80
|
+
# The index identifies the feature for which this node represents a value
|
81
|
+
|
82
|
+
# @!attribute value
|
83
|
+
# The value of this feature in this instance
|
84
|
+
|
85
|
+
# Create a new feature node.
|
86
|
+
#
|
87
|
+
# Libsvm::Node.new(0, 1.1) # => #<Libsvm::Node: index=0, value=1.1>
|
31
88
|
def initialize(index=0, value=0.0)
|
32
89
|
self.index = index
|
33
90
|
self.value = value
|
34
91
|
end
|
35
92
|
|
93
|
+
# Compare features for equality.
|
94
|
+
#
|
95
|
+
# Nodes with equal index and value are equal.
|
96
|
+
#
|
97
|
+
# @return [Boolean]
|
36
98
|
def ==(other)
|
37
99
|
index == other.index && value == other.value
|
38
100
|
end
|
101
|
+
|
102
|
+
def inspect # :nodoc:
|
103
|
+
vars = %w(index value).map { |name|
|
104
|
+
"#{name}=#{send(name)}"
|
105
|
+
}.join(", ")
|
106
|
+
"#<#{self.class}: #{vars}>"
|
107
|
+
end
|
39
108
|
end
|
40
109
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Libsvm
|
2
|
+
# This file only contains documentation comments. See
|
3
|
+
# ext/libsvm/libsvm.c for implementation.
|
4
|
+
|
5
|
+
# Represents a training set (alternatively called problem).
|
6
|
+
#
|
7
|
+
# In contains labels and examples in equal number.
|
8
|
+
#
|
9
|
+
# This class represents the struct
|
10
|
+
# svm_problem[https://github.com/cjlin1/libsvm/blob/master/README#L319].
|
11
|
+
class Problem
|
12
|
+
|
13
|
+
# @!attribute [r] l
|
14
|
+
#
|
15
|
+
# The number of labels and examples in this traning set. (The name
|
16
|
+
# is lower case L.)
|
17
|
+
#
|
18
|
+
# @return [Integer]
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
module Libsvm
|
2
|
+
# This file only contains documentation comments. See
|
3
|
+
# ext/libsvm/libsvm.c for implementation.
|
4
|
+
|
5
|
+
# Represents the learning parameter struct.
|
6
|
+
#
|
7
|
+
# The parameters in this object control how the SVM model is trained
|
8
|
+
# specifically.
|
9
|
+
#
|
10
|
+
# This class represents the struct
|
11
|
+
# svm_parameter[https://github.com/cjlin1/libsvm/blob/master/README#L366].
|
12
|
+
class SvmParameter
|
13
|
+
|
14
|
+
# @!attribute svm_type
|
15
|
+
#
|
16
|
+
# Type of SVM to use. This parameter controls if the model
|
17
|
+
# classifies or performs regression, if a one-class model is
|
18
|
+
# built, or if the NU variant of SVM is used.
|
19
|
+
#
|
20
|
+
# @see Libsvm::SvmType Libsvm::SvmType for value constants
|
21
|
+
# @return [C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR]
|
22
|
+
|
23
|
+
# @!attribute kernel_type
|
24
|
+
#
|
25
|
+
# Type of kernel to use.
|
26
|
+
#
|
27
|
+
# @see Libsvm::KernelType Libsvm::KernelType for kernel_type constants
|
28
|
+
# @return [LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED]
|
29
|
+
|
30
|
+
|
31
|
+
# @!attribute degree
|
32
|
+
#
|
33
|
+
# Degree parameter, relevant for POLY kernel type.
|
34
|
+
#
|
35
|
+
# @see Libsvm::KernelType Libsvm::KernelType for kernel_type constants
|
36
|
+
# @return [Integer]
|
37
|
+
|
38
|
+
# @!attribute gamma
|
39
|
+
#
|
40
|
+
# Gamma parameter, relevant for kernel types RBF, SIGMOID and POLY.
|
41
|
+
#
|
42
|
+
# @see Libsvm::KernelType Libsvm::KernelType for kernel_type constants
|
43
|
+
# @return [Float]
|
44
|
+
|
45
|
+
# @!attribute coef0
|
46
|
+
#
|
47
|
+
# Coeffectient 0, relevant for kernel types SIGMOID and POLY.
|
48
|
+
#
|
49
|
+
# @see Libsvm::KernelType Libsvm::KernelType for kernel_type constants
|
50
|
+
# @return [Float]
|
51
|
+
|
52
|
+
# @!attribute cache_size
|
53
|
+
#
|
54
|
+
# Size of the kernel cache, in megabytes.
|
55
|
+
#
|
56
|
+
# @return [Integer]
|
57
|
+
|
58
|
+
# @!attribute eps
|
59
|
+
#
|
60
|
+
# Stopping criterion parameter.
|
61
|
+
#
|
62
|
+
# @return [Float]
|
63
|
+
|
64
|
+
# @!attribute c
|
65
|
+
#
|
66
|
+
# The parameter C to use for this model.
|
67
|
+
#
|
68
|
+
# @return [Float]
|
69
|
+
|
70
|
+
# @!attribute label_weights
|
71
|
+
#
|
72
|
+
# Hash with indices of labels as keys and weights as values.
|
73
|
+
#
|
74
|
+
# These weights are used to change the penalty for specific labels
|
75
|
+
# (classes). If the weight for a label is not changed, it is set
|
76
|
+
# to 1.0.
|
77
|
+
#
|
78
|
+
# @return [Hash]
|
79
|
+
|
80
|
+
# @!attribute nu
|
81
|
+
#
|
82
|
+
# The nu parameter in nu-SVM, nu-SVR, and one-class-SVM (i.e. when
|
83
|
+
# the Libsvm::SvmParameter#svm_type is given as NU_SVC, NU_SVR or
|
84
|
+
# ONE_CLASS).
|
85
|
+
#
|
86
|
+
# @see Libsvm::SvmType Libsvm::SvmType for svm_type constants
|
87
|
+
# @return [Float]
|
88
|
+
|
89
|
+
# @!attribute p
|
90
|
+
#
|
91
|
+
# The epsilon in epsilon-insensitive loss function of epsilon-SVM
|
92
|
+
# regression (i.e. when the Libmsvm::SvmParameter#svm_type is
|
93
|
+
# given as Libsvm::SvmType::EPSILON_SVR).
|
94
|
+
#
|
95
|
+
# @return [Float]
|
96
|
+
|
97
|
+
# @!attribute shrinking
|
98
|
+
#
|
99
|
+
# Integer interpreted as boolean value.
|
100
|
+
#
|
101
|
+
# Controls if shrinking heuristics is used.
|
102
|
+
#
|
103
|
+
# @return [Integer] 0 meaning false, 1 true
|
104
|
+
|
105
|
+
# @!attribute probability
|
106
|
+
#
|
107
|
+
# Integer interpreted as boolean value.
|
108
|
+
#
|
109
|
+
# Controls if the model can create probability values for
|
110
|
+
# classifications in addition to the classification.
|
111
|
+
#
|
112
|
+
# @return [Integer] 0 meaning false, 1 true
|
113
|
+
|
114
|
+
end
|
115
|
+
end
|
data/lib/libsvm/version.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Create examples" do
|
4
|
+
describe "from a hash" do
|
5
|
+
example = { 11 => 0.11, 21 => 0.21, 101 => 0.99 }.to_example.sort_by(&:index)
|
6
|
+
example.should == Node.features({11 => 0.11, 21 => 0.21, 101 => 0.99 }).sort_by(&:index)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "from an array of tuples" do
|
10
|
+
it "can create example from array of pairs" do
|
11
|
+
example = [ [11, 0.11], [21, 0.21], [101, 0.99] ].to_example
|
12
|
+
example.should == Node.features({11 => 0.11, 21 => 0.21, 101 => 0.99 }).sort_by(&:index)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/model_spec.rb
CHANGED
@@ -98,7 +98,7 @@ describe "A Libsvm model" do
|
|
98
98
|
end
|
99
99
|
|
100
100
|
it "can be asked for its number of classes (aka. labels)" do
|
101
|
-
expect(@model.
|
101
|
+
expect(@model.classes_count).to eq(2)
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
@@ -108,9 +108,9 @@ describe "A Libsvm model" do
|
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
|
-
describe "
|
111
|
+
describe "support_vectors_count" do
|
112
112
|
it "returns count" do
|
113
|
-
expect(@model.
|
113
|
+
expect(@model.support_vectors_count).to eq(3)
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
@@ -124,7 +124,7 @@ describe "A Libsvm model" do
|
|
124
124
|
end
|
125
125
|
|
126
126
|
it "produces probabilities for each class" do
|
127
|
-
expect(probabilities.length).to eq(@model.
|
127
|
+
expect(probabilities.length).to eq(@model.classes_count)
|
128
128
|
end
|
129
129
|
|
130
130
|
it "can predict probability" do
|
data/spec/node_spec.rb
CHANGED
@@ -47,12 +47,14 @@ describe "A Node" do
|
|
47
47
|
ary = Node.features([0.1, 0.2, 0.3, 0.4, 0.5])
|
48
48
|
ary.map {|n| n.class.should == Node}
|
49
49
|
ary.map {|n| n.value }.should == [0.1, 0.2, 0.3, 0.4, 0.5]
|
50
|
+
ary.map {|n| n.index }.should == [0, 1, 2, 3, 4]
|
50
51
|
end
|
51
52
|
|
52
53
|
it "class can create nodes from variable parameters" do
|
53
54
|
ary = Node.features(0.1, 0.2, 0.3, 0.4, 0.5)
|
54
55
|
ary.map {|n| Node.should === n }
|
55
56
|
ary.map {|n| n.value }.should == [0.1, 0.2, 0.3, 0.4, 0.5]
|
57
|
+
ary.map {|n| n.index }.should == [0, 1, 2, 3, 4]
|
56
58
|
end
|
57
59
|
|
58
60
|
it "class can create nodes from hash" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rb-libsvm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- C. Florian Ebeling
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-01-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake-compiler
|
@@ -67,9 +67,13 @@ files:
|
|
67
67
|
- ext/libsvm/svm.cpp
|
68
68
|
- ext/libsvm/svm.h
|
69
69
|
- lib/libsvm.rb
|
70
|
+
- lib/libsvm/model.rb
|
70
71
|
- lib/libsvm/node.rb
|
72
|
+
- lib/libsvm/problem.rb
|
73
|
+
- lib/libsvm/svm_parameter.rb
|
71
74
|
- lib/libsvm/version.rb
|
72
75
|
- rb-libsvm.gemspec
|
76
|
+
- spec/core_ext_spec.rb
|
73
77
|
- spec/model_spec.rb
|
74
78
|
- spec/node_spec.rb
|
75
79
|
- spec/parameter_spec.rb
|
@@ -100,6 +104,7 @@ signing_key:
|
|
100
104
|
specification_version: 4
|
101
105
|
summary: Ruby bindings for LIBSVM
|
102
106
|
test_files:
|
107
|
+
- spec/core_ext_spec.rb
|
103
108
|
- spec/model_spec.rb
|
104
109
|
- spec/node_spec.rb
|
105
110
|
- spec/parameter_spec.rb
|