numo-liblinear 0.2.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,8 @@ struct model* rb_hash_to_model(VALUE model_hash)
14
14
  model->label = nary_to_int_vec(el);
15
15
  el = rb_hash_aref(model_hash, ID2SYM(rb_intern("bias")));
16
16
  model->bias = NUM2DBL(el);
17
+ el = rb_hash_aref(model_hash, ID2SYM(rb_intern("rho")));
18
+ model->rho = NUM2DBL(el);
17
19
  return model;
18
20
  }
19
21
 
@@ -29,6 +31,7 @@ VALUE model_to_rb_hash(struct model* const model)
29
31
  rb_hash_aset(model_hash, ID2SYM(rb_intern("label")),
30
32
  model->label ? int_vec_to_nary(model->label, model->nr_class) : Qnil);
31
33
  rb_hash_aset(model_hash, ID2SYM(rb_intern("bias")), DBL2NUM(model->bias));
34
+ rb_hash_aset(model_hash, ID2SYM(rb_intern("rho")), DBL2NUM(model->rho));
32
35
  return model_hash;
33
36
  }
34
37
 
@@ -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")));
@@ -53,11 +56,14 @@ struct parameter* rb_hash_to_parameter(VALUE param_hash)
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
  }
@@ -29,9 +29,12 @@ struct problem* dataset_to_problem(VALUE x_val, VALUE y_val)
29
29
  narray_t* x_nary;
30
30
  double* x_pt;
31
31
  double* y_pt;
32
- int i, j;
32
+ int i, j, k;
33
33
  int n_samples;
34
34
  int n_features;
35
+ int n_nonzero_features;
36
+ int is_padded;
37
+ int last_feature_id;
35
38
 
36
39
  GetNArray(x_val, x_nary);
37
40
  n_samples = (int)NA_SHAPE(x_nary)[0];
@@ -46,14 +49,39 @@ struct problem* dataset_to_problem(VALUE x_val, VALUE y_val)
46
49
  problem->x = ALLOC_N(struct feature_node*, n_samples);
47
50
  problem->y = ALLOC_N(double, n_samples);
48
51
 
52
+ is_padded = 0;
49
53
  for (i = 0; i < n_samples; i++) {
50
- problem->x[i] = ALLOC_N(struct feature_node, n_features + 1);
54
+ n_nonzero_features = 0;
51
55
  for (j = 0; j < n_features; j++) {
52
- problem->x[i][j].index = j + 1;
53
- problem->x[i][j].value = x_pt[i * 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;
54
84
  }
55
- problem->x[i][n_features].index = -1;
56
- problem->x[i][n_features].value = 0.0;
57
85
  problem->y[i] = y_pt[i];
58
86
  }
59
87
 
@@ -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.2.0'
6
+ VERSION = '1.1.0'
7
7
  end
8
8
  end
@@ -27,14 +27,28 @@ 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']
33
40
  spec.extensions = ['ext/numo/liblinear/extconf.rb']
34
41
 
42
+ spec.metadata = {
43
+ 'homepage_uri' => 'https://github.com/yoshoku/numo-liblinear',
44
+ 'source_code_uri' => 'https://github.com/yoshoku/numo-liblinear',
45
+ 'documentation_uri' => 'https://yoshoku.github.io/numo-liblinear/doc/'
46
+ }
47
+
35
48
  spec.add_runtime_dependency 'numo-narray', '~> 0.9.1'
49
+
36
50
  spec.add_development_dependency 'bundler', '~> 2.0'
37
- spec.add_development_dependency 'rake', '~> 10.0'
51
+ spec.add_development_dependency 'rake', '~> 12.0'
38
52
  spec.add_development_dependency 'rake-compiler', '~> 1.0'
39
53
  spec.add_development_dependency 'rspec', '~> 3.0'
40
54
  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.2.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshoku
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-15 00:00:00.000000000 Z
11
+ date: 2020-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-narray
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '12.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '12.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake-compiler
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -92,6 +92,7 @@ extensions:
92
92
  extra_rdoc_files: []
93
93
  files:
94
94
  - ".gitignore"
95
+ - ".gitmodules"
95
96
  - ".rspec"
96
97
  - ".travis.yml"
97
98
  - CHANGELOG.md
@@ -103,6 +104,16 @@ files:
103
104
  - ext/numo/liblinear/converter.c
104
105
  - ext/numo/liblinear/converter.h
105
106
  - ext/numo/liblinear/extconf.rb
107
+ - ext/numo/liblinear/liblinear/blas/blas.h
108
+ - ext/numo/liblinear/liblinear/blas/blasp.h
109
+ - ext/numo/liblinear/liblinear/blas/daxpy.c
110
+ - ext/numo/liblinear/liblinear/blas/ddot.c
111
+ - ext/numo/liblinear/liblinear/blas/dnrm2.c
112
+ - ext/numo/liblinear/liblinear/blas/dscal.c
113
+ - ext/numo/liblinear/liblinear/linear.cpp
114
+ - ext/numo/liblinear/liblinear/linear.h
115
+ - ext/numo/liblinear/liblinear/newton.cpp
116
+ - ext/numo/liblinear/liblinear/newton.h
106
117
  - ext/numo/liblinear/liblinearext.c
107
118
  - ext/numo/liblinear/liblinearext.h
108
119
  - ext/numo/liblinear/model.c
@@ -119,8 +130,11 @@ files:
119
130
  homepage: https://github.com/yoshoku/numo-liblinear
120
131
  licenses:
121
132
  - BSD-3-Clause
122
- metadata: {}
123
- post_install_message:
133
+ metadata:
134
+ homepage_uri: https://github.com/yoshoku/numo-liblinear
135
+ source_code_uri: https://github.com/yoshoku/numo-liblinear
136
+ documentation_uri: https://yoshoku.github.io/numo-liblinear/doc/
137
+ post_install_message:
124
138
  rdoc_options: []
125
139
  require_paths:
126
140
  - lib
@@ -135,9 +149,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
149
  - !ruby/object:Gem::Version
136
150
  version: '0'
137
151
  requirements: []
138
- rubyforge_project:
139
- rubygems_version: 2.6.14.4
140
- signing_key:
152
+ rubygems_version: 3.1.2
153
+ signing_key:
141
154
  specification_version: 4
142
155
  summary: Numo::Liblinear is a Ruby gem binding to the LIBLINEAR library. Numo::Liblinear
143
156
  makes to use the LIBLINEAR functions with dataset represented by Numo::NArray.