numo-libsvm 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,88 +0,0 @@
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 * sizeof(int32_t));
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 * sizeof(double));
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
- }
@@ -1,15 +0,0 @@
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 */
@@ -1,90 +0,0 @@
1
- #include "svm_problem.h"
2
-
3
- void xfree_svm_problem(struct svm_problem* problem)
4
- {
5
- int i;
6
- if (problem) {
7
- if (problem->x) {
8
- for (i = 0; i < problem->l; i++) {
9
- if (problem->x[i]) {
10
- xfree(problem->x[i]);
11
- problem->x[i] = NULL;
12
- }
13
- }
14
- xfree(problem->x);
15
- problem->x = NULL;
16
- }
17
- if (problem->y) {
18
- xfree(problem->y);
19
- problem->y = NULL;
20
- }
21
- xfree(problem);
22
- problem = NULL;
23
- }
24
- }
25
-
26
- struct svm_problem* dataset_to_svm_problem(VALUE x_val, VALUE y_val)
27
- {
28
- struct svm_problem* problem;
29
- narray_t* x_nary;
30
- double* x_pt;
31
- double* y_pt;
32
- int i, j, k;
33
- int n_samples;
34
- int n_features;
35
- int n_nonzero_features;
36
- int is_padded;
37
- int last_feature_id;
38
-
39
- GetNArray(x_val, x_nary);
40
- n_samples = (int)NA_SHAPE(x_nary)[0];
41
- n_features = (int)NA_SHAPE(x_nary)[1];
42
- x_pt = (double*)na_get_pointer_for_read(x_val);
43
- y_pt = (double*)na_get_pointer_for_read(y_val);
44
-
45
- problem = ALLOC(struct svm_problem);
46
- problem->l = n_samples;
47
- problem->x = ALLOC_N(struct svm_node*, n_samples);
48
- problem->y = ALLOC_N(double, n_samples);
49
-
50
- is_padded = 0;
51
- for (i = 0; i < n_samples; i++) {
52
- n_nonzero_features = 0;
53
- for (j = 0; j < n_features; j++) {
54
- if (x_pt[i * n_features + j] != 0.0) {
55
- n_nonzero_features += 1;
56
- last_feature_id = j + 1;
57
- }
58
- }
59
- if (is_padded == 0 && last_feature_id == n_features) {
60
- is_padded = 1;
61
- }
62
- if (is_padded == 1) {
63
- problem->x[i] = ALLOC_N(struct svm_node, n_nonzero_features + 1);
64
- } else {
65
- problem->x[i] = ALLOC_N(struct svm_node, n_nonzero_features + 2);
66
- }
67
- for (j = 0, k = 0; j < n_features; j++) {
68
- if (x_pt[i * n_features + j] != 0.0) {
69
- problem->x[i][k].index = j + 1;
70
- problem->x[i][k].value = (double)x_pt[i * n_features + j];
71
- k++;
72
- }
73
- }
74
- if (is_padded == 1) {
75
- problem->x[i][n_nonzero_features].index = -1;
76
- problem->x[i][n_nonzero_features].value = 0.0;
77
- } else {
78
- problem->x[i][n_nonzero_features].index = n_features;
79
- problem->x[i][n_nonzero_features].value = 0.0;
80
- problem->x[i][n_nonzero_features + 1].index = -1;
81
- problem->x[i][n_nonzero_features + 1].value = 0.0;
82
- }
83
- problem->y[i] = y_pt[i];
84
- }
85
-
86
- RB_GC_GUARD(x_val);
87
- RB_GC_GUARD(y_val);
88
-
89
- return problem;
90
- }
@@ -1,12 +0,0 @@
1
- #ifndef NUMO_LIBSVM_SVM_PROBLEM_H
2
- #define NUMO_LIBSVM_SVM_PROBLEM_H 1
3
-
4
- #include <svm.h>
5
- #include <ruby.h>
6
- #include <numo/narray.h>
7
- #include <numo/template.h>
8
-
9
- void xfree_svm_problem(struct svm_problem* problem);
10
- struct svm_problem* dataset_to_svm_problem(VALUE x_val, VALUE y_val);
11
-
12
- #endif /* NUMO_LIBSVM_SVM_PROBLEM_H */
@@ -1,22 +0,0 @@
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
- }
@@ -1,9 +0,0 @@
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 */
data/numo-libsvm.gemspec DELETED
@@ -1,55 +0,0 @@
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
-
32
- gem_dir = File.expand_path(__dir__) + '/'
33
- submodule_path = `git submodule --quiet foreach pwd`.split($OUTPUT_RECORD_SEPARATOR).first
34
- submodule_relative_path = submodule_path.sub gem_dir, ''
35
- spec.files << "#{submodule_relative_path}/svm.cpp"
36
- spec.files << "#{submodule_relative_path}/svm.h"
37
-
38
- spec.bindir = 'exe'
39
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
40
- spec.require_paths = ['lib']
41
- spec.extensions = ['ext/numo/libsvm/extconf.rb']
42
-
43
- spec.metadata = {
44
- 'homepage_uri' => 'https://github.com/yoshoku/numo-libsvm',
45
- 'source_code_uri' => 'https://github.com/yoshoku/numo-libsvm',
46
- 'documentation_uri' => 'https://yoshoku.github.io/numo-libsvm/doc/'
47
- }
48
-
49
- spec.add_runtime_dependency 'numo-narray', '~> 0.9.1'
50
-
51
- spec.add_development_dependency 'bundler', '~> 2.0'
52
- spec.add_development_dependency 'rake', '~> 12.0'
53
- spec.add_development_dependency 'rake-compiler', '~> 1.0'
54
- spec.add_development_dependency 'rspec', '~> 3.0'
55
- end