numo-liblinear 0.5.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,36 +5,39 @@ struct parameter* rb_hash_to_parameter(VALUE param_hash)
5
5
  VALUE el;
6
6
  struct parameter* param = ALLOC(struct parameter);
7
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;
8
+ param->solver_type = !NIL_P(el) ? NUM2INT(el) : L2R_L2LOSS_SVC_DUAL;
9
9
  el = rb_hash_aref(param_hash, ID2SYM(rb_intern("eps")));
10
- if (!NIL_P(el)) {
10
+ if (!NIL_P(el)) {
11
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;
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;
36
39
  }
37
- }
40
+ }
38
41
  el = rb_hash_aref(param_hash, ID2SYM(rb_intern("C")));
39
42
  param->C = !NIL_P(el) ? NUM2DBL(el) : 1;
40
43
  el = rb_hash_aref(param_hash, ID2SYM(rb_intern("nr_weight")));
@@ -43,21 +46,24 @@ struct parameter* rb_hash_to_parameter(VALUE param_hash)
43
46
  param->weight_label = NULL;
44
47
  if (!NIL_P(el)) {
45
48
  param->weight_label = ALLOC_N(int, param->nr_weight);
46
- memcpy(param->weight_label, (int32_t*)na_get_pointer_for_read(el), param->nr_weight);
49
+ memcpy(param->weight_label, (int32_t*)na_get_pointer_for_read(el), param->nr_weight * sizeof(int32_t));
47
50
  }
48
51
  el = rb_hash_aref(param_hash, ID2SYM(rb_intern("weight")));
49
52
  param->weight = NULL;
50
53
  if (!NIL_P(el)) {
51
54
  param->weight = ALLOC_N(double, param->nr_weight);
52
- memcpy(param->weight, (double*)na_get_pointer_for_read(el), param->nr_weight);
55
+ memcpy(param->weight, (double*)na_get_pointer_for_read(el), param->nr_weight * sizeof(double));
53
56
  }
54
57
  el = rb_hash_aref(param_hash, ID2SYM(rb_intern("p")));
55
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;
56
61
  el = rb_hash_aref(param_hash, ID2SYM(rb_intern("init_sol")));
57
62
  param->init_sol = NULL;
58
63
  if (!NIL_P(el)) {
59
64
  param->init_sol = nary_to_dbl_vec(el);
60
65
  }
66
+ param->regularize_bias = 1;
61
67
  return param;
62
68
  }
63
69
 
@@ -73,6 +79,7 @@ VALUE parameter_to_rb_hash(struct parameter* const param)
73
79
  rb_hash_aset(param_hash, ID2SYM(rb_intern("weight")),
74
80
  param->weight ? dbl_vec_to_nary(param->weight, param->nr_weight) : Qnil);
75
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));
76
83
  rb_hash_aset(param_hash, ID2SYM(rb_intern("init_sol")), Qnil);
77
84
  return param_hash;
78
85
  }
@@ -85,5 +85,8 @@ struct problem* dataset_to_problem(VALUE x_val, VALUE y_val)
85
85
  problem->y[i] = y_pt[i];
86
86
  }
87
87
 
88
+ RB_GC_GUARD(x_val);
89
+ RB_GC_GUARD(y_val);
90
+
88
91
  return problem;
89
92
  }
@@ -17,18 +17,20 @@ void rb_init_solver_type_module()
17
17
  rb_define_const(mSolverType, "L2R_L2LOSS_SVC", INT2NUM(L2R_L2LOSS_SVC));
18
18
  /* L2-regularized L1-loss support vector classification (dual) */
19
19
  rb_define_const(mSolverType, "L2R_L1LOSS_SVC_DUAL", INT2NUM(L2R_L1LOSS_SVC_DUAL));
20
- /* support vector classification by Crammer and Singer */
20
+ /* support vector classification by Crammer and Singer */
21
21
  rb_define_const(mSolverType, "MCSVM_CS", INT2NUM(MCSVM_CS));
22
- /* L1-regularized L2-loss support vector classification */
22
+ /* L1-regularized L2-loss support vector classification */
23
23
  rb_define_const(mSolverType, "L1R_L2LOSS_SVC", INT2NUM(L1R_L2LOSS_SVC));
24
- /* L1-regularized logistic regression */
24
+ /* L1-regularized logistic regression */
25
25
  rb_define_const(mSolverType, "L1R_LR", INT2NUM(L1R_LR));
26
26
  /* L2-regularized logistic regression (dual) */
27
27
  rb_define_const(mSolverType, "L2R_LR_DUAL", INT2NUM(L2R_LR_DUAL));
28
- /* L2-regularized L2-loss support vector regression (primal) */
28
+ /* L2-regularized L2-loss support vector regression (primal) */
29
29
  rb_define_const(mSolverType, "L2R_L2LOSS_SVR", INT2NUM(L2R_L2LOSS_SVR));
30
- /* L2-regularized L2-loss support vector regression (dual) */
30
+ /* L2-regularized L2-loss support vector regression (dual) */
31
31
  rb_define_const(mSolverType, "L2R_L2LOSS_SVR_DUAL", INT2NUM(L2R_L2LOSS_SVR_DUAL));
32
- /* L2-regularized L1-loss support vector regression (dual) */
32
+ /* L2-regularized L1-loss support vector regression (dual) */
33
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));
34
36
  }
@@ -3,6 +3,6 @@
3
3
  module Numo
4
4
  module Liblinear
5
5
  # The version of Numo::Liblienar you are using.
6
- VERSION = '0.5.0'
6
+ VERSION = '1.2.0'
7
7
  end
8
8
  end
@@ -27,6 +27,13 @@ Gem::Specification.new do |spec|
27
27
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
28
28
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
29
  end
30
+
31
+ gem_dir = File.expand_path(__dir__) + '/'
32
+ submodule_path = `git submodule --quiet foreach pwd`.split($OUTPUT_RECORD_SEPARATOR).first
33
+ submodule_relative_path = submodule_path.sub gem_dir, ''
34
+ liblinear_files = %w[linear.cpp linear.h newton.cpp newton.h blas/blas.h blas/blasp.h blas/daxpy.c blas/ddot.c blas/dnrm2.c blas/dscal.c]
35
+ liblinear_files.each { |liblinf| spec.files << "#{submodule_relative_path}/#{liblinf}" }
36
+
30
37
  spec.bindir = 'exe'
31
38
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
39
  spec.require_paths = ['lib']
@@ -39,8 +46,4 @@ Gem::Specification.new do |spec|
39
46
  }
40
47
 
41
48
  spec.add_runtime_dependency 'numo-narray', '~> 0.9.1'
42
- spec.add_development_dependency 'bundler', '~> 2.0'
43
- spec.add_development_dependency 'rake', '~> 10.0'
44
- spec.add_development_dependency 'rake-compiler', '~> 1.0'
45
- spec.add_development_dependency 'rspec', '~> 3.0'
46
49
  end
@@ -0,0 +1,52 @@
1
+ module Numo
2
+ module Liblinear
3
+ module SolverType
4
+ L2R_LR: Integer
5
+ L2R_L2LOSS_SVC_DUAL: Integer
6
+ L2R_L2LOSS_SVC: Integer
7
+ L2R_L1LOSS_SVC_DUAL: Integer
8
+ MCSVM_CS: Integer
9
+ L1R_L2LOSS_SVC: Integer
10
+ L1R_LR: Integer
11
+ L2R_LR_DUAL: Integer
12
+ L2R_L2LOSS_SVR: Integer
13
+ L2R_L2LOSS_SVR_DUAL: Integer
14
+ L2R_L1LOSS_SVR_DUAL: Integer
15
+ ONECLASS_SVM: Integer
16
+ end
17
+
18
+ LIBLINEAR_VERSION: Integer
19
+ VERSION: String
20
+
21
+ type model = {
22
+ nr_class: Integer,
23
+ nr_feature: Integer,
24
+ w: Numo::DFloat,
25
+ label: Numo::Int32,
26
+ bias: Float,
27
+ rho: Float
28
+ }
29
+
30
+ type param = {
31
+ solver_type: Integer?,
32
+ eps: Float?,
33
+ C: Float?,
34
+ nr_weight: Integer?,
35
+ weight_label: Numo::Int32?,
36
+ weight: Numo::DFloat?,
37
+ p: Float?,
38
+ nu: Float?,
39
+ verbose: bool?,
40
+ random_seed: Integer?
41
+ }
42
+
43
+ def self?.cv: (Numo::DFloat x, Numo::DFloat y, param, Integer n_folds) -> Numo::DFloat
44
+ def self?.train: (Numo::DFloat x, Numo::DFloat y, param) -> model
45
+ def self?.predict: (Numo::DFloat x, param, model) -> Numo::DFloat
46
+ def self?.predict_proba: (Numo::DFloat x, param, model) -> Numo::DFloat
47
+ def self?.decision_function: (Numo::DFloat x, param, model) -> Numo::DFloat
48
+ def self?.save_model: (String filename, param, model) -> bool
49
+ def self?.load_model: (String filename) -> [param, model]
50
+ end
51
+ end
52
+
data/sig/patch.rbs ADDED
@@ -0,0 +1,8 @@
1
+ module Numo
2
+ class NArray
3
+ end
4
+ class DFloat < NArray
5
+ end
6
+ class Int32 < NArray
7
+ end
8
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numo-liblinear
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshoku
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-23 00:00:00.000000000 Z
11
+ date: 2021-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-narray
@@ -24,62 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
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
27
  description: |
84
28
  Numo::Liblinear is a Ruby gem binding to the LIBLINEAR library.
85
29
  LIBLINEAR is one of the famous libraries for large-scale regularized linear classification and regression.
@@ -91,18 +35,30 @@ extensions:
91
35
  - ext/numo/liblinear/extconf.rb
92
36
  extra_rdoc_files: []
93
37
  files:
38
+ - ".github/workflows/build.yml"
94
39
  - ".gitignore"
40
+ - ".gitmodules"
95
41
  - ".rspec"
96
- - ".travis.yml"
97
42
  - CHANGELOG.md
98
43
  - CODE_OF_CONDUCT.md
99
44
  - Gemfile
100
45
  - LICENSE.txt
101
46
  - README.md
102
47
  - Rakefile
48
+ - Steepfile
103
49
  - ext/numo/liblinear/converter.c
104
50
  - ext/numo/liblinear/converter.h
105
51
  - ext/numo/liblinear/extconf.rb
52
+ - ext/numo/liblinear/liblinear/blas/blas.h
53
+ - ext/numo/liblinear/liblinear/blas/blasp.h
54
+ - ext/numo/liblinear/liblinear/blas/daxpy.c
55
+ - ext/numo/liblinear/liblinear/blas/ddot.c
56
+ - ext/numo/liblinear/liblinear/blas/dnrm2.c
57
+ - ext/numo/liblinear/liblinear/blas/dscal.c
58
+ - ext/numo/liblinear/liblinear/linear.cpp
59
+ - ext/numo/liblinear/liblinear/linear.h
60
+ - ext/numo/liblinear/liblinear/newton.cpp
61
+ - ext/numo/liblinear/liblinear/newton.h
106
62
  - ext/numo/liblinear/liblinearext.c
107
63
  - ext/numo/liblinear/liblinearext.h
108
64
  - ext/numo/liblinear/model.c
@@ -116,6 +72,8 @@ files:
116
72
  - lib/numo/liblinear.rb
117
73
  - lib/numo/liblinear/version.rb
118
74
  - numo-liblinear.gemspec
75
+ - sig/numo/liblinear.rbs
76
+ - sig/patch.rbs
119
77
  homepage: https://github.com/yoshoku/numo-liblinear
120
78
  licenses:
121
79
  - BSD-3-Clause
@@ -138,8 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
96
  - !ruby/object:Gem::Version
139
97
  version: '0'
140
98
  requirements: []
141
- rubyforge_project:
142
- rubygems_version: 2.6.14.4
99
+ rubygems_version: 3.1.6
143
100
  signing_key:
144
101
  specification_version: 4
145
102
  summary: Numo::Liblinear is a Ruby gem binding to the LIBLINEAR library. Numo::Liblinear
data/.travis.yml DELETED
@@ -1,14 +0,0 @@
1
- ---
2
- sudo: true
3
- os: linux
4
- dist: bionic
5
- language: ruby
6
- cache: bundler
7
- rvm:
8
- - '2.4'
9
- - '2.5'
10
- - '2.6'
11
-
12
- before_install:
13
- - sudo apt-get install -y liblinear-dev
14
- - gem install bundler -v 2.0.2