numo-libsvm 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ #ifndef NUMO_LIBSVMEXT_H
2
+ #define NUMO_LIBSVMEXT_H 1
3
+
4
+ #include <math.h>
5
+ #include <string.h>
6
+ #include <svm.h>
7
+ #include <ruby.h>
8
+ #include <numo/narray.h>
9
+ #include <numo/template.h>
10
+
11
+ #include "converter.h"
12
+ #include "svm_parameter.h"
13
+ #include "svm_model.h"
14
+ #include "svm_type.h"
15
+ #include "kernel_type.h"
16
+
17
+ #endif /* NUMO_LIBSVMEXT_H */
@@ -0,0 +1,89 @@
1
+
2
+ #include "svm_model.h"
3
+
4
+ struct svm_model* rb_hash_to_svm_model(VALUE model_hash)
5
+ {
6
+ VALUE el;
7
+ struct svm_model* model = ALLOC(struct svm_model);
8
+ el = rb_hash_aref(model_hash, ID2SYM(rb_intern("nr_class")));
9
+ model->nr_class = el != Qnil ? NUM2INT(el) : 0;
10
+ el = rb_hash_aref(model_hash, ID2SYM(rb_intern("l")));
11
+ model->l = el != Qnil ? NUM2INT(el) : 0;
12
+ el = rb_hash_aref(model_hash, ID2SYM(rb_intern("SV")));
13
+ model->SV = nary_to_svm_nodes(el);
14
+ el = rb_hash_aref(model_hash, ID2SYM(rb_intern("sv_coef")));
15
+ model->sv_coef = nary_to_dbl_mat(el);
16
+ el = rb_hash_aref(model_hash, ID2SYM(rb_intern("rho")));
17
+ model->rho = nary_to_dbl_vec(el);
18
+ el = rb_hash_aref(model_hash, ID2SYM(rb_intern("probA")));
19
+ model->probA = nary_to_dbl_vec(el);
20
+ el = rb_hash_aref(model_hash, ID2SYM(rb_intern("probB")));
21
+ model->probB = nary_to_dbl_vec(el);
22
+ el = rb_hash_aref(model_hash, ID2SYM(rb_intern("sv_indices")));
23
+ model->sv_indices = nary_to_int_vec(el);
24
+ el = rb_hash_aref(model_hash, ID2SYM(rb_intern("label")));
25
+ model->label = nary_to_int_vec(el);
26
+ el = rb_hash_aref(model_hash, ID2SYM(rb_intern("nSV")));
27
+ model->nSV = nary_to_int_vec(el);
28
+ el = rb_hash_aref(model_hash, ID2SYM(rb_intern("free_sv")));
29
+ model->free_sv = el != Qnil ? NUM2INT(el) : 0;
30
+ return model;
31
+ }
32
+
33
+ VALUE svm_model_to_rb_hash(struct svm_model* const model)
34
+ {
35
+ int const n_classes = model->nr_class;
36
+ int const n_support_vecs = model->l;
37
+ VALUE support_vecs = model->SV ? svm_nodes_to_nary(model->SV, n_support_vecs) : Qnil;
38
+ VALUE coefficients = model->sv_coef ? dbl_mat_to_nary(model->sv_coef, n_classes - 1, n_support_vecs) : Qnil;
39
+ VALUE intercepts = model->rho ? dbl_vec_to_nary(model->rho, n_classes * (n_classes - 1) / 2) : Qnil;
40
+ VALUE prob_alpha = model->probA ? dbl_vec_to_nary(model->probA, n_classes * (n_classes - 1) / 2) : Qnil;
41
+ VALUE prob_beta = model->probB ? dbl_vec_to_nary(model->probB, n_classes * (n_classes - 1) / 2) : Qnil;
42
+ VALUE sv_indices = model->sv_indices ? int_vec_to_nary(model->sv_indices, n_support_vecs) : Qnil;
43
+ VALUE labels = model->label ? int_vec_to_nary(model->label, n_classes) : Qnil;
44
+ VALUE n_support_vecs_each_class = model->nSV ? int_vec_to_nary(model->nSV, n_classes) : Qnil;
45
+ VALUE model_hash = rb_hash_new();
46
+ rb_hash_aset(model_hash, ID2SYM(rb_intern("nr_class")), INT2NUM(n_classes));
47
+ rb_hash_aset(model_hash, ID2SYM(rb_intern("l")), INT2NUM(n_support_vecs));
48
+ rb_hash_aset(model_hash, ID2SYM(rb_intern("SV")), support_vecs);
49
+ rb_hash_aset(model_hash, ID2SYM(rb_intern("sv_coef")), coefficients);
50
+ rb_hash_aset(model_hash, ID2SYM(rb_intern("rho")), intercepts);
51
+ rb_hash_aset(model_hash, ID2SYM(rb_intern("probA")), prob_alpha);
52
+ rb_hash_aset(model_hash, ID2SYM(rb_intern("probB")), prob_beta);
53
+ rb_hash_aset(model_hash, ID2SYM(rb_intern("sv_indices")), sv_indices);
54
+ rb_hash_aset(model_hash, ID2SYM(rb_intern("label")), labels);
55
+ rb_hash_aset(model_hash, ID2SYM(rb_intern("nSV")), n_support_vecs_each_class);
56
+ rb_hash_aset(model_hash, ID2SYM(rb_intern("free_sv")), INT2NUM(model->free_sv));
57
+ return model_hash;
58
+ }
59
+
60
+ void xfree_svm_model(struct svm_model* model)
61
+ {
62
+ int i;
63
+ if (model) {
64
+ if (model->SV) {
65
+ for (i = 0; i < model->l; xfree(model->SV[i++]));
66
+ xfree(model->SV);
67
+ model->SV = NULL;
68
+ }
69
+ if (model->sv_coef) {
70
+ for (i = 0; i < model->nr_class - 1; xfree(model->sv_coef[i++]));
71
+ xfree(model->sv_coef);
72
+ model->sv_coef = NULL;
73
+ }
74
+ xfree(model->rho);
75
+ model->rho = NULL;
76
+ xfree(model->probA);
77
+ model->probA = NULL;
78
+ xfree(model->probB);
79
+ model->probB = NULL;
80
+ xfree(model->sv_indices);
81
+ model->sv_indices = NULL;
82
+ xfree(model->label);
83
+ model->label = NULL;
84
+ xfree(model->nSV);
85
+ model->nSV = NULL;
86
+ xfree(model);
87
+ model = NULL;
88
+ }
89
+ }
@@ -0,0 +1,15 @@
1
+ #ifndef NUMO_LIBSVM_SVM_MODEL_H
2
+ #define NUMO_LIBSVM_SVM_MODEL_H 1
3
+
4
+ #include <svm.h>
5
+ #include <ruby.h>
6
+ #include <numo/narray.h>
7
+ #include <numo/template.h>
8
+
9
+ #include "converter.h"
10
+
11
+ struct svm_model* rb_hash_to_svm_model(VALUE model_hash);
12
+ VALUE svm_model_to_rb_hash(struct svm_model* const model);
13
+ void xfree_svm_model(struct svm_model* model);
14
+
15
+ #endif /* NUMO_LIBSVM_SVM_MODEL_H */
@@ -0,0 +1,88 @@
1
+
2
+ #include "svm_parameter.h"
3
+
4
+ struct svm_parameter* rb_hash_to_svm_parameter(VALUE param_hash)
5
+ {
6
+ VALUE el;
7
+ struct svm_parameter* param = ALLOC(struct svm_parameter);
8
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("svm_type")));
9
+ param->svm_type = !NIL_P(el) ? NUM2INT(el) : C_SVC;
10
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("kernel_type")));
11
+ param->kernel_type = !NIL_P(el) ? NUM2INT(el) : RBF;
12
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("degree")));
13
+ param->degree = !NIL_P(el) ? NUM2INT(el) : 3;
14
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("gamma")));
15
+ param->gamma = !NIL_P(el) ? NUM2DBL(el) : 1;
16
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("coef0")));
17
+ param->coef0 = !NIL_P(el) ? NUM2DBL(el) : 0;
18
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("cache_size")));
19
+ param->cache_size = !NIL_P(el) ? NUM2DBL(el) : 100;
20
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("eps")));
21
+ param->eps = !NIL_P(el) ? NUM2DBL(el) : 1e-3;
22
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("C")));
23
+ param->C = !NIL_P(el) ? NUM2DBL(el) : 1;
24
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("nr_weight")));
25
+ param->nr_weight = !NIL_P(el) ? NUM2INT(el) : 0;
26
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("nu")));
27
+ param->nu = !NIL_P(el) ? NUM2DBL(el) : 0.5;
28
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("p")));
29
+ param->p = !NIL_P(el) ? NUM2DBL(el) : 0.1;
30
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("shrinking")));
31
+ param->shrinking = RB_TYPE_P(el, T_FALSE) ? 0 : 1;
32
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("probability")));
33
+ param->probability = RB_TYPE_P(el, T_TRUE) ? 1 : 0;
34
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("weight_label")));
35
+ param->weight_label = NULL;
36
+ if (!NIL_P(el)) {
37
+ param->weight_label = ALLOC_N(int, param->nr_weight);
38
+ memcpy(param->weight_label, (int32_t*)na_get_pointer_for_read(el), param->nr_weight);
39
+ }
40
+ el = rb_hash_aref(param_hash, ID2SYM(rb_intern("weight")));
41
+ param->weight = NULL;
42
+ if (!NIL_P(el)) {
43
+ param->weight = ALLOC_N(double, param->nr_weight);
44
+ memcpy(param->weight, (double*)na_get_pointer_for_read(el), param->nr_weight);
45
+ }
46
+ return param;
47
+ }
48
+
49
+ VALUE svm_parameter_to_rb_hash(struct svm_parameter* const param)
50
+ {
51
+ VALUE param_hash = rb_hash_new();
52
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("svm_type")), INT2NUM(param->svm_type));
53
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("kernel_type")), INT2NUM(param->kernel_type));
54
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("degree")), INT2NUM(param->degree));
55
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("gamma")), DBL2NUM(param->gamma));
56
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("coef0")), DBL2NUM(param->coef0));
57
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("cache_size")), DBL2NUM(param->cache_size));
58
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("eps")), DBL2NUM(param->eps));
59
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("C")), DBL2NUM(param->C));
60
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("nr_weight")), INT2NUM(param->nr_weight));
61
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("nu")), DBL2NUM(param->nu));
62
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("p")), DBL2NUM(param->p));
63
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("shrinking")),
64
+ param->shrinking == 1 ? Qtrue : Qfalse);
65
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("probability")),
66
+ param->probability == 1 ? Qtrue : Qfalse);
67
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("weight_label")),
68
+ param->weight_label ? int_vec_to_nary(param->weight_label, param->nr_weight) : Qnil);
69
+ rb_hash_aset(param_hash, ID2SYM(rb_intern("weight")),
70
+ param->weight ? dbl_vec_to_nary(param->weight, param->nr_weight) : Qnil);
71
+ return param_hash;
72
+ }
73
+
74
+ void xfree_svm_parameter(struct svm_parameter* param)
75
+ {
76
+ if (param) {
77
+ if (param->weight_label) {
78
+ xfree(param->weight_label);
79
+ param->weight_label = NULL;
80
+ }
81
+ if (param->weight) {
82
+ xfree(param->weight);
83
+ param->weight = NULL;
84
+ }
85
+ xfree(param);
86
+ param = NULL;
87
+ }
88
+ }
@@ -0,0 +1,15 @@
1
+ #ifndef NUMO_LIBSVM_SVM_PARAMETER_H
2
+ #define NUMO_LIBSVM_SVM_PARAMETER_H 1
3
+
4
+ #include <svm.h>
5
+ #include <ruby.h>
6
+ #include <numo/narray.h>
7
+ #include <numo/template.h>
8
+
9
+ #include "converter.h"
10
+
11
+ struct svm_parameter* rb_hash_to_svm_parameter(VALUE param_hash);
12
+ VALUE svm_parameter_to_rb_hash(struct svm_parameter* const param);
13
+ void xfree_svm_parameter(struct svm_parameter* param);
14
+
15
+ #endif /* NUMO_LIBSVM_SVM_PARAMETER_H */
@@ -0,0 +1,22 @@
1
+ #include "svm_type.h"
2
+
3
+ RUBY_EXTERN VALUE mLibsvm;
4
+
5
+ void rb_init_svm_type_module()
6
+ {
7
+ /**
8
+ * Document-module: Numo::Libsvm::SvmType
9
+ * The module consisting of constants for SVM algorithm type that used for parameter of LIBSVM.
10
+ */
11
+ VALUE mSvmType = rb_define_module_under(mLibsvm, "SvmType");
12
+ /* C-SVM classification */
13
+ rb_define_const(mSvmType, "C_SVC", INT2NUM(C_SVC));
14
+ /* nu-SVM classification */
15
+ rb_define_const(mSvmType, "NU_SVC", INT2NUM(NU_SVC));
16
+ /* one-class-SVM */
17
+ rb_define_const(mSvmType, "ONE_CLASS", INT2NUM(ONE_CLASS));
18
+ /* epsilon-SVM regression */
19
+ rb_define_const(mSvmType, "EPSILON_SVR", INT2NUM(EPSILON_SVR));
20
+ /* nu-SVM regression */
21
+ rb_define_const(mSvmType, "NU_SVR", INT2NUM(NU_SVR));
22
+ }
@@ -0,0 +1,9 @@
1
+ #ifndef NUMO_LIBSVM_SVM_TYPE_H
2
+ #define NUMO_LIBSVM_SVM_TYPE_H 1
3
+
4
+ #include <svm.h>
5
+ #include <ruby.h>
6
+
7
+ void rb_init_svm_type_module();
8
+
9
+ #endif /* NUMO_LIBSVM_SVM_TYPE_H */
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'numo/narray'
4
+ require 'numo/libsvm/version'
5
+ require 'numo/libsvm/libsvmext'
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Numo
4
+ module Libsvm
5
+ # The version of Numo::Libsvm you are using.
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'numo/libsvm/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'numo-libsvm'
9
+ spec.version = Numo::Libsvm::VERSION
10
+ spec.authors = ['yoshoku']
11
+ spec.email = ['yoshoku@outlook.com']
12
+
13
+ spec.summary = <<~MSG
14
+ Numo::Libsvm is a Ruby gem binding to the LIBSVM library.
15
+ Numo::Libsvm makes to use the LIBSVM functions with dataset represented by Numo::NArray.
16
+ MSG
17
+ spec.description = <<~MSG
18
+ Numo::Libsvm is a Ruby gem binding to the LIBSVM library.
19
+ LIBSVM is one of the famous libraries that implemented Support Vector Machines,
20
+ and provides functions for support vector classifier, regression, and distribution estimation.
21
+ Numo::Libsvm makes to use the LIBSVM functions with dataset represented by Numo::NArray.
22
+ MSG
23
+ spec.homepage = 'https://github.com/yoshoku/numo-libsvm'
24
+ spec.license = 'BSD-3-Clause'
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = 'exe'
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ['lib']
34
+ spec.extensions = ['ext/numo/libsvm/extconf.rb']
35
+
36
+ spec.add_runtime_dependency 'numo-narray', '~> 0.9.1'
37
+ spec.add_development_dependency 'bundler', '~> 2.0'
38
+ spec.add_development_dependency 'rake', '~> 10.0'
39
+ spec.add_development_dependency 'rake-compiler', '~> 1.0'
40
+ spec.add_development_dependency 'rspec', '~> 3.0'
41
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: numo-libsvm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yoshoku
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: numo-narray
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake-compiler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: |
84
+ Numo::Libsvm is a Ruby gem binding to the LIBSVM library.
85
+ LIBSVM is one of the famous libraries that implemented Support Vector Machines,
86
+ and provides functions for support vector classifier, regression, and distribution estimation.
87
+ Numo::Libsvm makes to use the LIBSVM functions with dataset represented by Numo::NArray.
88
+ email:
89
+ - yoshoku@outlook.com
90
+ executables: []
91
+ extensions:
92
+ - ext/numo/libsvm/extconf.rb
93
+ extra_rdoc_files: []
94
+ files:
95
+ - ".gitignore"
96
+ - ".rspec"
97
+ - ".travis.yml"
98
+ - CHANGELOG.md
99
+ - CODE_OF_CONDUCT.md
100
+ - Gemfile
101
+ - LICENSE.txt
102
+ - README.md
103
+ - Rakefile
104
+ - ext/numo/libsvm/converter.c
105
+ - ext/numo/libsvm/converter.h
106
+ - ext/numo/libsvm/extconf.rb
107
+ - ext/numo/libsvm/kernel_type.c
108
+ - ext/numo/libsvm/kernel_type.h
109
+ - ext/numo/libsvm/libsvmext.c
110
+ - ext/numo/libsvm/libsvmext.h
111
+ - ext/numo/libsvm/svm_model.c
112
+ - ext/numo/libsvm/svm_model.h
113
+ - ext/numo/libsvm/svm_parameter.c
114
+ - ext/numo/libsvm/svm_parameter.h
115
+ - ext/numo/libsvm/svm_type.c
116
+ - ext/numo/libsvm/svm_type.h
117
+ - lib/numo/libsvm.rb
118
+ - lib/numo/libsvm/version.rb
119
+ - numo-libsvm.gemspec
120
+ homepage: https://github.com/yoshoku/numo-libsvm
121
+ licenses:
122
+ - BSD-3-Clause
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.6.14.4
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Numo::Libsvm is a Ruby gem binding to the LIBSVM library. Numo::Libsvm makes
144
+ to use the LIBSVM functions with dataset represented by Numo::NArray.
145
+ test_files: []