numo-libsvm 1.1.2 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/LICENSE.txt +1 -1
- data/README.md +1 -5
- data/ext/numo/libsvm/extconf.rb +6 -16
- data/ext/numo/libsvm/libsvmext.cpp +220 -0
- data/ext/numo/libsvm/libsvmext.hpp +721 -0
- data/ext/numo/libsvm/src/COPYRIGHT +31 -0
- data/ext/numo/libsvm/{libsvm → src}/svm.cpp +134 -18
- data/ext/numo/libsvm/{libsvm → src}/svm.h +2 -1
- data/lib/numo/libsvm/version.rb +1 -1
- data/sig/numo/libsvm.rbs +1 -0
- metadata +12 -31
- data/.github/workflows/build.yml +0 -29
- data/.gitignore +0 -20
- data/.gitmodules +0 -3
- data/.rspec +0 -3
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -11
- data/Rakefile +0 -15
- data/Steepfile +0 -20
- data/ext/numo/libsvm/converter.c +0 -204
- data/ext/numo/libsvm/converter.h +0 -20
- data/ext/numo/libsvm/kernel_type.c +0 -22
- data/ext/numo/libsvm/kernel_type.h +0 -9
- data/ext/numo/libsvm/libsvmext.c +0 -578
- data/ext/numo/libsvm/libsvmext.h +0 -18
- data/ext/numo/libsvm/svm_model.c +0 -89
- data/ext/numo/libsvm/svm_model.h +0 -15
- data/ext/numo/libsvm/svm_parameter.c +0 -88
- data/ext/numo/libsvm/svm_parameter.h +0 -15
- data/ext/numo/libsvm/svm_problem.c +0 -90
- data/ext/numo/libsvm/svm_problem.h +0 -12
- data/ext/numo/libsvm/svm_type.c +0 -22
- data/ext/numo/libsvm/svm_type.h +0 -9
- data/numo-libsvm.gemspec +0 -47
@@ -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 */
|
data/ext/numo/libsvm/svm_type.c
DELETED
@@ -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
|
-
}
|
data/ext/numo/libsvm/svm_type.h
DELETED
data/numo-libsvm.gemspec
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
lib = File.expand_path('lib', __dir__)
|
2
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
-
require 'numo/libsvm/version'
|
4
|
-
|
5
|
-
Gem::Specification.new do |spec|
|
6
|
-
spec.name = 'numo-libsvm'
|
7
|
-
spec.version = Numo::Libsvm::VERSION
|
8
|
-
spec.authors = ['yoshoku']
|
9
|
-
spec.email = ['yoshoku@outlook.com']
|
10
|
-
|
11
|
-
spec.summary = <<~MSG
|
12
|
-
Numo::Libsvm is a Ruby gem binding to the LIBSVM library.
|
13
|
-
Numo::Libsvm makes to use the LIBSVM functions with dataset represented by Numo::NArray.
|
14
|
-
MSG
|
15
|
-
spec.description = <<~MSG
|
16
|
-
Numo::Libsvm is a Ruby gem binding to the LIBSVM library.
|
17
|
-
LIBSVM is one of the famous libraries that implemented Support Vector Machines,
|
18
|
-
and provides functions for support vector classifier, regression, and distribution estimation.
|
19
|
-
Numo::Libsvm makes to use the LIBSVM functions with dataset represented by Numo::NArray.
|
20
|
-
MSG
|
21
|
-
spec.homepage = 'https://github.com/yoshoku/numo-libsvm'
|
22
|
-
spec.license = 'BSD-3-Clause'
|
23
|
-
|
24
|
-
# Specify which files should be added to the gem when it is released.
|
25
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
26
|
-
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
27
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|sig-deps)/}) }
|
28
|
-
end
|
29
|
-
|
30
|
-
submodule_path = `git submodule --quiet foreach pwd`.split($INPUT_RECORD_SEPARATOR).first
|
31
|
-
submodule_relative_path = submodule_path.sub("#{File.expand_path(__dir__)}/", '')
|
32
|
-
spec.files << "#{submodule_relative_path}/svm.cpp"
|
33
|
-
spec.files << "#{submodule_relative_path}/svm.h"
|
34
|
-
|
35
|
-
spec.bindir = 'exe'
|
36
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
-
spec.require_paths = ['lib']
|
38
|
-
spec.extensions = ['ext/numo/libsvm/extconf.rb']
|
39
|
-
|
40
|
-
spec.metadata = {
|
41
|
-
'homepage_uri' => 'https://github.com/yoshoku/numo-libsvm',
|
42
|
-
'source_code_uri' => 'https://github.com/yoshoku/numo-libsvm',
|
43
|
-
'documentation_uri' => 'https://yoshoku.github.io/numo-libsvm/doc/'
|
44
|
-
}
|
45
|
-
|
46
|
-
spec.add_runtime_dependency 'numo-narray', '>= 0.9.1'
|
47
|
-
end
|