numo-liblinear 1.2.2 → 2.0.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 +5 -0
- data/LICENSE.txt +1 -1
- data/README.md +0 -5
- data/ext/numo/liblinear/extconf.rb +8 -14
- data/ext/numo/liblinear/liblinearext.cpp +215 -0
- data/ext/numo/liblinear/liblinearext.hpp +636 -0
- data/ext/numo/liblinear/src/COPYRIGHT +31 -0
- data/ext/numo/liblinear/{liblinear → src}/blas/blas.h +0 -0
- data/ext/numo/liblinear/{liblinear → src}/blas/blasp.h +0 -0
- data/ext/numo/liblinear/{liblinear → src}/blas/daxpy.c +0 -0
- data/ext/numo/liblinear/{liblinear → src}/blas/ddot.c +0 -0
- data/ext/numo/liblinear/{liblinear → src}/blas/dnrm2.c +0 -0
- data/ext/numo/liblinear/{liblinear → src}/blas/dscal.c +0 -0
- data/ext/numo/liblinear/{liblinear → src}/linear.cpp +0 -0
- data/ext/numo/liblinear/{liblinear → src}/linear.h +0 -0
- data/ext/numo/liblinear/{liblinear → src}/newton.cpp +0 -0
- data/ext/numo/liblinear/{liblinear → src}/newton.h +0 -0
- data/lib/numo/liblinear/version.rb +1 -1
- metadata +19 -37
- 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/liblinear/converter.c +0 -133
- data/ext/numo/liblinear/converter.h +0 -18
- data/ext/numo/liblinear/liblinearext.c +0 -576
- data/ext/numo/liblinear/liblinearext.h +0 -17
- data/ext/numo/liblinear/model.c +0 -48
- data/ext/numo/liblinear/model.h +0 -15
- data/ext/numo/liblinear/parameter.c +0 -105
- data/ext/numo/liblinear/parameter.h +0 -15
- data/ext/numo/liblinear/problem.c +0 -92
- data/ext/numo/liblinear/problem.h +0 -12
- data/ext/numo/liblinear/solver_type.c +0 -36
- data/ext/numo/liblinear/solver_type.h +0 -9
- data/numo-liblinear.gemspec +0 -47
@@ -1,105 +0,0 @@
|
|
1
|
-
#include "parameter.h"
|
2
|
-
|
3
|
-
struct parameter* rb_hash_to_parameter(VALUE param_hash)
|
4
|
-
{
|
5
|
-
VALUE el;
|
6
|
-
struct parameter* param = ALLOC(struct parameter);
|
7
|
-
el = rb_hash_aref(param_hash, ID2SYM(rb_intern("solver_type")));
|
8
|
-
param->solver_type = !NIL_P(el) ? NUM2INT(el) : L2R_L2LOSS_SVC_DUAL;
|
9
|
-
el = rb_hash_aref(param_hash, ID2SYM(rb_intern("eps")));
|
10
|
-
if (!NIL_P(el)) {
|
11
|
-
param->eps = NUM2DBL(el);
|
12
|
-
} else {
|
13
|
-
switch(param->solver_type)
|
14
|
-
{
|
15
|
-
case L2R_LR:
|
16
|
-
case L2R_L2LOSS_SVC:
|
17
|
-
param->eps = 0.01;
|
18
|
-
break;
|
19
|
-
case L2R_L2LOSS_SVR:
|
20
|
-
param->eps = 0.0001;
|
21
|
-
break;
|
22
|
-
case L2R_L2LOSS_SVC_DUAL:
|
23
|
-
case L2R_L1LOSS_SVC_DUAL:
|
24
|
-
case MCSVM_CS:
|
25
|
-
case L2R_LR_DUAL:
|
26
|
-
param->eps = 0.1;
|
27
|
-
break;
|
28
|
-
case L1R_L2LOSS_SVC:
|
29
|
-
case L1R_LR:
|
30
|
-
param->eps = 0.01;
|
31
|
-
break;
|
32
|
-
case L2R_L1LOSS_SVR_DUAL:
|
33
|
-
case L2R_L2LOSS_SVR_DUAL:
|
34
|
-
param->eps = 0.1;
|
35
|
-
break;
|
36
|
-
case ONECLASS_SVM:
|
37
|
-
param->eps = 0.01;
|
38
|
-
break;
|
39
|
-
}
|
40
|
-
}
|
41
|
-
el = rb_hash_aref(param_hash, ID2SYM(rb_intern("C")));
|
42
|
-
param->C = !NIL_P(el) ? NUM2DBL(el) : 1;
|
43
|
-
el = rb_hash_aref(param_hash, ID2SYM(rb_intern("nr_weight")));
|
44
|
-
param->nr_weight = !NIL_P(el) ? NUM2INT(el) : 0;
|
45
|
-
el = rb_hash_aref(param_hash, ID2SYM(rb_intern("weight_label")));
|
46
|
-
param->weight_label = NULL;
|
47
|
-
if (!NIL_P(el)) {
|
48
|
-
param->weight_label = ALLOC_N(int, param->nr_weight);
|
49
|
-
memcpy(param->weight_label, (int32_t*)na_get_pointer_for_read(el), param->nr_weight * sizeof(int32_t));
|
50
|
-
}
|
51
|
-
el = rb_hash_aref(param_hash, ID2SYM(rb_intern("weight")));
|
52
|
-
param->weight = NULL;
|
53
|
-
if (!NIL_P(el)) {
|
54
|
-
param->weight = ALLOC_N(double, param->nr_weight);
|
55
|
-
memcpy(param->weight, (double*)na_get_pointer_for_read(el), param->nr_weight * sizeof(double));
|
56
|
-
}
|
57
|
-
el = rb_hash_aref(param_hash, ID2SYM(rb_intern("p")));
|
58
|
-
param->p = !NIL_P(el) ? NUM2DBL(el) : 0.1;
|
59
|
-
el = rb_hash_aref(param_hash, ID2SYM(rb_intern("nu")));
|
60
|
-
param->nu = !NIL_P(el) ? NUM2DBL(el) : 0.5;
|
61
|
-
el = rb_hash_aref(param_hash, ID2SYM(rb_intern("init_sol")));
|
62
|
-
param->init_sol = NULL;
|
63
|
-
if (!NIL_P(el)) {
|
64
|
-
param->init_sol = nary_to_dbl_vec(el);
|
65
|
-
}
|
66
|
-
param->regularize_bias = 1;
|
67
|
-
return param;
|
68
|
-
}
|
69
|
-
|
70
|
-
VALUE parameter_to_rb_hash(struct parameter* const param)
|
71
|
-
{
|
72
|
-
VALUE param_hash = rb_hash_new();
|
73
|
-
rb_hash_aset(param_hash, ID2SYM(rb_intern("solver_type")), INT2NUM(param->solver_type));
|
74
|
-
rb_hash_aset(param_hash, ID2SYM(rb_intern("eps")), DBL2NUM(param->eps));
|
75
|
-
rb_hash_aset(param_hash, ID2SYM(rb_intern("C")), DBL2NUM(param->C));
|
76
|
-
rb_hash_aset(param_hash, ID2SYM(rb_intern("nr_weight")), INT2NUM(param->nr_weight));
|
77
|
-
rb_hash_aset(param_hash, ID2SYM(rb_intern("weight_label")),
|
78
|
-
param->weight_label ? int_vec_to_nary(param->weight_label, param->nr_weight) : Qnil);
|
79
|
-
rb_hash_aset(param_hash, ID2SYM(rb_intern("weight")),
|
80
|
-
param->weight ? dbl_vec_to_nary(param->weight, param->nr_weight) : Qnil);
|
81
|
-
rb_hash_aset(param_hash, ID2SYM(rb_intern("p")), DBL2NUM(param->p));
|
82
|
-
rb_hash_aset(param_hash, ID2SYM(rb_intern("nu")), DBL2NUM(param->nu));
|
83
|
-
rb_hash_aset(param_hash, ID2SYM(rb_intern("init_sol")), Qnil);
|
84
|
-
return param_hash;
|
85
|
-
}
|
86
|
-
|
87
|
-
void xfree_parameter(struct parameter* param)
|
88
|
-
{
|
89
|
-
if (param) {
|
90
|
-
if (param->weight_label) {
|
91
|
-
xfree(param->weight_label);
|
92
|
-
param->weight_label = NULL;
|
93
|
-
}
|
94
|
-
if (param->weight) {
|
95
|
-
xfree(param->weight);
|
96
|
-
param->weight = NULL;
|
97
|
-
}
|
98
|
-
if (param->init_sol) {
|
99
|
-
xfree(param->init_sol);
|
100
|
-
param->init_sol = NULL;
|
101
|
-
}
|
102
|
-
xfree(param);
|
103
|
-
param = NULL;
|
104
|
-
}
|
105
|
-
}
|
@@ -1,15 +0,0 @@
|
|
1
|
-
#ifndef NUMO_LIBLINEAR_PARAMETER_H
|
2
|
-
#define NUMO_LIBLINEAR_PARAMETER_H 1
|
3
|
-
|
4
|
-
#include <linear.h>
|
5
|
-
#include <ruby.h>
|
6
|
-
#include <numo/narray.h>
|
7
|
-
#include <numo/template.h>
|
8
|
-
|
9
|
-
#include "converter.h"
|
10
|
-
|
11
|
-
struct parameter* rb_hash_to_parameter(VALUE parm_hash);
|
12
|
-
VALUE parameter_to_rb_hash(struct parameter* const param);
|
13
|
-
void xfree_parameter(struct parameter* param);
|
14
|
-
|
15
|
-
#endif /* NUMO_LIBLINEAR_PARAMETER_H */
|
@@ -1,92 +0,0 @@
|
|
1
|
-
#include "problem.h"
|
2
|
-
|
3
|
-
void xfree_problem(struct 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 problem* dataset_to_problem(VALUE x_val, VALUE y_val)
|
27
|
-
{
|
28
|
-
struct 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 problem);
|
46
|
-
problem->bias = -1;
|
47
|
-
problem->n = n_features;
|
48
|
-
problem->l = n_samples;
|
49
|
-
problem->x = ALLOC_N(struct feature_node*, n_samples);
|
50
|
-
problem->y = ALLOC_N(double, n_samples);
|
51
|
-
|
52
|
-
is_padded = 0;
|
53
|
-
for (i = 0; i < n_samples; i++) {
|
54
|
-
n_nonzero_features = 0;
|
55
|
-
for (j = 0; j < n_features; j++) {
|
56
|
-
if (x_pt[i * n_features + j] != 0.0) {
|
57
|
-
n_nonzero_features++;
|
58
|
-
last_feature_id = j + 1;
|
59
|
-
}
|
60
|
-
}
|
61
|
-
if (is_padded == 0 && last_feature_id == n_features) {
|
62
|
-
is_padded = 1;
|
63
|
-
}
|
64
|
-
if (is_padded == 1) {
|
65
|
-
problem->x[i] = ALLOC_N(struct feature_node, n_nonzero_features + 1);
|
66
|
-
} else {
|
67
|
-
problem->x[i] = ALLOC_N(struct feature_node, n_nonzero_features + 2);
|
68
|
-
}
|
69
|
-
for (j = 0, k = 0; j < n_features; j++) {
|
70
|
-
if (x_pt[i * n_features + j] != 0.0) {
|
71
|
-
problem->x[i][k].index = j + 1;
|
72
|
-
problem->x[i][k].value = x_pt[i * n_features + j];
|
73
|
-
k++;
|
74
|
-
}
|
75
|
-
}
|
76
|
-
if (is_padded == 1) {
|
77
|
-
problem->x[i][n_nonzero_features].index = -1;
|
78
|
-
problem->x[i][n_nonzero_features].value = 0.0;
|
79
|
-
} else {
|
80
|
-
problem->x[i][n_nonzero_features].index = n_features;
|
81
|
-
problem->x[i][n_nonzero_features].value = 0.0;
|
82
|
-
problem->x[i][n_nonzero_features + 1].index = -1;
|
83
|
-
problem->x[i][n_nonzero_features + 1].value = 0.0;
|
84
|
-
}
|
85
|
-
problem->y[i] = y_pt[i];
|
86
|
-
}
|
87
|
-
|
88
|
-
RB_GC_GUARD(x_val);
|
89
|
-
RB_GC_GUARD(y_val);
|
90
|
-
|
91
|
-
return problem;
|
92
|
-
}
|
@@ -1,12 +0,0 @@
|
|
1
|
-
#ifndef NUMO_LIBLINEAR_PROBLEM_H
|
2
|
-
#define NUMO_LIBLINEAR_PROBLEM_H 1
|
3
|
-
|
4
|
-
#include <linear.h>
|
5
|
-
#include <ruby.h>
|
6
|
-
#include <numo/narray.h>
|
7
|
-
#include <numo/template.h>
|
8
|
-
|
9
|
-
void xfree_problem(struct problem* problem);
|
10
|
-
struct problem* dataset_to_problem(VALUE x_val, VALUE y_val);
|
11
|
-
|
12
|
-
#endif /* NUMO_LIBLINEAR_PROBLEM_H */
|
@@ -1,36 +0,0 @@
|
|
1
|
-
#include "solver_type.h"
|
2
|
-
|
3
|
-
RUBY_EXTERN VALUE mLiblinear;
|
4
|
-
|
5
|
-
void rb_init_solver_type_module()
|
6
|
-
{
|
7
|
-
/**
|
8
|
-
* Document-module: Numo::Liblinear::SolverType
|
9
|
-
* The module consisting of constants for solver type that used for parameter of LIBLINER.
|
10
|
-
*/
|
11
|
-
VALUE mSolverType = rb_define_module_under(mLiblinear, "SolverType");
|
12
|
-
/* L2-regularized logistic regression (primal) */
|
13
|
-
rb_define_const(mSolverType, "L2R_LR", INT2NUM(L2R_LR));
|
14
|
-
/* L2-regularized L2-loss support vector classification (dual) */
|
15
|
-
rb_define_const(mSolverType, "L2R_L2LOSS_SVC_DUAL", INT2NUM(L2R_L2LOSS_SVC_DUAL));
|
16
|
-
/* L2-regularized L2-loss support vector classification (primal) */
|
17
|
-
rb_define_const(mSolverType, "L2R_L2LOSS_SVC", INT2NUM(L2R_L2LOSS_SVC));
|
18
|
-
/* L2-regularized L1-loss support vector classification (dual) */
|
19
|
-
rb_define_const(mSolverType, "L2R_L1LOSS_SVC_DUAL", INT2NUM(L2R_L1LOSS_SVC_DUAL));
|
20
|
-
/* support vector classification by Crammer and Singer */
|
21
|
-
rb_define_const(mSolverType, "MCSVM_CS", INT2NUM(MCSVM_CS));
|
22
|
-
/* L1-regularized L2-loss support vector classification */
|
23
|
-
rb_define_const(mSolverType, "L1R_L2LOSS_SVC", INT2NUM(L1R_L2LOSS_SVC));
|
24
|
-
/* L1-regularized logistic regression */
|
25
|
-
rb_define_const(mSolverType, "L1R_LR", INT2NUM(L1R_LR));
|
26
|
-
/* L2-regularized logistic regression (dual) */
|
27
|
-
rb_define_const(mSolverType, "L2R_LR_DUAL", INT2NUM(L2R_LR_DUAL));
|
28
|
-
/* L2-regularized L2-loss support vector regression (primal) */
|
29
|
-
rb_define_const(mSolverType, "L2R_L2LOSS_SVR", INT2NUM(L2R_L2LOSS_SVR));
|
30
|
-
/* L2-regularized L2-loss support vector regression (dual) */
|
31
|
-
rb_define_const(mSolverType, "L2R_L2LOSS_SVR_DUAL", INT2NUM(L2R_L2LOSS_SVR_DUAL));
|
32
|
-
/* L2-regularized L1-loss support vector regression (dual) */
|
33
|
-
rb_define_const(mSolverType, "L2R_L1LOSS_SVR_DUAL", INT2NUM(L2R_L1LOSS_SVR_DUAL));
|
34
|
-
/* one-class support vector machine (dual) */
|
35
|
-
rb_define_const(mSolverType, "ONECLASS_SVM", INT2NUM(ONECLASS_SVM));
|
36
|
-
}
|
data/numo-liblinear.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/liblinear/version'
|
4
|
-
|
5
|
-
Gem::Specification.new do |spec|
|
6
|
-
spec.name = 'numo-liblinear'
|
7
|
-
spec.version = Numo::Liblinear::VERSION
|
8
|
-
spec.authors = ['yoshoku']
|
9
|
-
spec.email = ['yoshoku@outlook.com']
|
10
|
-
|
11
|
-
spec.summary = <<~MSG
|
12
|
-
Numo::Liblinear is a Ruby gem binding to the LIBLINEAR library.
|
13
|
-
Numo::Liblinear makes to use the LIBLINEAR functions with dataset represented by Numo::NArray.
|
14
|
-
MSG
|
15
|
-
spec.description = <<~MSG
|
16
|
-
Numo::Liblinear is a Ruby gem binding to the LIBLINEAR library.
|
17
|
-
LIBLINEAR is one of the famous libraries for large-scale regularized linear classification and regression.
|
18
|
-
Numo::Liblinear makes to use the LIBLINEAR functions with dataset represented by Numo::NArray.
|
19
|
-
MSG
|
20
|
-
spec.homepage = 'https://github.com/yoshoku/numo-liblinear'
|
21
|
-
spec.license = 'BSD-3-Clause'
|
22
|
-
|
23
|
-
# Specify which files should be added to the gem when it is released.
|
24
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
-
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|sig-deps)/}) }
|
27
|
-
end
|
28
|
-
|
29
|
-
submodule_path = `git submodule --quiet foreach pwd`.split($INPUT_RECORD_SEPARATOR).first
|
30
|
-
submodule_relative_path = submodule_path.sub("#{File.expand_path(__dir__)}/", '')
|
31
|
-
liblinear_files = %w[linear.cpp linear.h newton.cpp newton.h
|
32
|
-
blas/blas.h blas/blasp.h blas/daxpy.c blas/ddot.c blas/dnrm2.c blas/dscal.c]
|
33
|
-
liblinear_files.each { |liblinf| spec.files << "#{submodule_relative_path}/#{liblinf}" }
|
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/liblinear/extconf.rb']
|
39
|
-
|
40
|
-
spec.metadata = {
|
41
|
-
'homepage_uri' => 'https://github.com/yoshoku/numo-liblinear',
|
42
|
-
'source_code_uri' => 'https://github.com/yoshoku/numo-liblinear',
|
43
|
-
'documentation_uri' => 'https://yoshoku.github.io/numo-liblinear/doc/'
|
44
|
-
}
|
45
|
-
|
46
|
-
spec.add_runtime_dependency 'numo-narray', '>= 0.9.1'
|
47
|
-
end
|