numo-liblinear 1.2.2 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +4 -5
  5. data/ext/numo/liblinear/extconf.rb +8 -14
  6. data/ext/numo/liblinear/liblinearext.cpp +215 -0
  7. data/ext/numo/liblinear/liblinearext.hpp +636 -0
  8. data/ext/numo/liblinear/src/COPYRIGHT +31 -0
  9. data/ext/numo/liblinear/{liblinear → src}/blas/blas.h +0 -0
  10. data/ext/numo/liblinear/{liblinear → src}/blas/blasp.h +0 -0
  11. data/ext/numo/liblinear/{liblinear → src}/blas/daxpy.c +0 -0
  12. data/ext/numo/liblinear/{liblinear → src}/blas/ddot.c +0 -0
  13. data/ext/numo/liblinear/{liblinear → src}/blas/dnrm2.c +0 -0
  14. data/ext/numo/liblinear/{liblinear → src}/blas/dscal.c +0 -0
  15. data/ext/numo/liblinear/{liblinear → src}/linear.cpp +152 -95
  16. data/ext/numo/liblinear/{liblinear → src}/linear.h +5 -5
  17. data/ext/numo/liblinear/{liblinear → src}/newton.cpp +20 -14
  18. data/ext/numo/liblinear/{liblinear → src}/newton.h +0 -0
  19. data/lib/numo/liblinear/version.rb +1 -1
  20. metadata +20 -37
  21. data/.github/workflows/build.yml +0 -29
  22. data/.gitignore +0 -20
  23. data/.gitmodules +0 -3
  24. data/.rspec +0 -3
  25. data/CODE_OF_CONDUCT.md +0 -74
  26. data/Gemfile +0 -11
  27. data/Rakefile +0 -15
  28. data/Steepfile +0 -20
  29. data/ext/numo/liblinear/converter.c +0 -133
  30. data/ext/numo/liblinear/converter.h +0 -18
  31. data/ext/numo/liblinear/liblinearext.c +0 -576
  32. data/ext/numo/liblinear/liblinearext.h +0 -17
  33. data/ext/numo/liblinear/model.c +0 -48
  34. data/ext/numo/liblinear/model.h +0 -15
  35. data/ext/numo/liblinear/parameter.c +0 -105
  36. data/ext/numo/liblinear/parameter.h +0 -15
  37. data/ext/numo/liblinear/problem.c +0 -92
  38. data/ext/numo/liblinear/problem.h +0 -12
  39. data/ext/numo/liblinear/solver_type.c +0 -36
  40. data/ext/numo/liblinear/solver_type.h +0 -9
  41. data/numo-liblinear.gemspec +0 -47
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e01c76e5ada65d064a521c29a11e1ad18644ca60a5f79119b552ad6732fc6ab2
4
- data.tar.gz: d73b0a4e48ca51053ba162783c6dc128ef20b49ef3ecb7f06654ed9eba4c8a55
3
+ metadata.gz: da7218c002f8aaf80578116ddb102cd866b3ec50c585412f8e7fd0d320332ba0
4
+ data.tar.gz: 7d763d787e4458977722eeb531355a37d181934b881f2b23ae9e0adbf72bc9ac
5
5
  SHA512:
6
- metadata.gz: 27188ec56a5956df8acebf89384d901aab38b24f6f98cf5b877b4f70be8172b107e05021f9d637761cdf23e3578daf1c1a50f009129e2f556b6d963cd30ebe28
7
- data.tar.gz: 3bf342d11db29b36a05cd0f345ae98376547a25e3920d7937f1974ed625fa8c4dfbe23be8e7910207090084f2c23cd2b65cf5f999e162d6f1ae35c5d18667df7
6
+ metadata.gz: e10b86d373e13b813c9685f5bfd37efeb984a513345de7d38a52f38d44b78db6673b93b3ce56637ac1c25e62922af43232e5aed796fb3eff9fd4c9f7658b1638
7
+ data.tar.gz: 4b693278b26de842bbe7e5968157a3bc84c4f230aeda64f997b7d98f3fda1dca6053044e9d9c41822a35de89b526d0490eafa9bdbb6b9adb3a9f7384ea96f06e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ # 2.1.1
2
+ - Refactor codes and configs with RuboCop.
3
+
4
+ # 2.1.0
5
+ - Update bundled LIBLINEAR to 2.44
6
+
7
+ # 2.0.0
8
+ - Redesign native extension codes.
9
+ - Change not ot use git submodule for LIBLINEAR codes bundle.
10
+ - Introduce conventional commits.
11
+
1
12
  # 1.2.2
2
13
  - Remove dependent gem's type declaration file from installation files.
3
14
 
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2019-2021 Atsushi Tatsuma
1
+ Copyright (c) 2019-2022 Atsushi Tatsuma
2
2
  All rights reserved.
3
3
 
4
4
  Redistribution and use in source and binary forms, with or without
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Build Status](https://github.com/yoshoku/numo-liblinear/workflows/build/badge.svg)](https://github.com/yoshoku/numo-liblinear/actions?query=workflow%3Abuild)
4
4
  [![Gem Version](https://badge.fury.io/rb/numo-liblinear.svg)](https://badge.fury.io/rb/numo-liblinear)
5
5
  [![BSD 3-Clause License](https://img.shields.io/badge/License-BSD%203--Clause-orange.svg)](https://github.com/yoshoku/numo-liblinear/blob/main/LICENSE.txt)
6
- [![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg)](https://yoshoku.github.io/numo-liblinear/doc/)
6
+ [![Documentation](https://img.shields.io/badge/docs-rdoc.info-blue.svg)](https://yoshoku.github.io/numo-liblinear/doc/)
7
7
 
8
8
  Numo::Liblinear is a Ruby gem binding to the [LIBLINEAR](https://www.csie.ntu.edu.tw/~cjlin/liblinear/) library.
9
9
  LIBLINEAR is one of the famous libraries for large-scale regularized linear classification and regression.
@@ -171,9 +171,8 @@ param = {
171
171
  ## Contributing
172
172
 
173
173
  Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/numo-liblinear.
174
- This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
174
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](https://contributor-covenant.org) code of conduct.
175
175
 
176
- ## Code of Conduct
176
+ ## License
177
177
 
178
- Everyone interacting in the Numo::Liblinear project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow
179
- the [code of conduct](https://github.com/yoshoku/numo-liblinear/blob/main/CODE_OF_CONDUCT.md).
178
+ The gem is available as open source under the terms of the [BSD-3-Clause License](https://opensource.org/licenses/BSD-3-Clause).
@@ -8,10 +8,7 @@ $LOAD_PATH.each do |lp|
8
8
  end
9
9
  end
10
10
 
11
- unless have_header('numo/narray.h')
12
- puts 'numo/narray.h not found.'
13
- exit(1)
14
- end
11
+ abort 'numo/narray.h not found.' unless have_header('numo/narray.h')
15
12
 
16
13
  if RUBY_PLATFORM =~ /mswin|cygwin|mingw/
17
14
  $LOAD_PATH.each do |lp|
@@ -20,19 +17,16 @@ if RUBY_PLATFORM =~ /mswin|cygwin|mingw/
20
17
  break
21
18
  end
22
19
  end
23
- unless have_library('narray', 'nary_new')
24
- puts 'libnarray.a not found.'
25
- exit(1)
26
- end
20
+ abort 'libnarray.a not found.' unless have_library('narray', 'nary_new')
27
21
  end
28
22
 
29
- $LDFLAGS << ' -lstdc++ '
23
+ abort 'libstdc++ is not found.' unless have_library('stdc++')
30
24
 
31
- $srcs = Dir.glob("#{$srcdir}/*.c").map { |path| File.basename(path) }
32
- $srcs.concat(%w[linear.cpp newton.cpp daxpy.c ddot.c dnrm2.c dscal.c])
25
+ $srcs = Dir.glob("#{$srcdir}/**/*.cpp").map { |path| File.basename(path) }
26
+ $srcs.concat(%w[daxpy.c ddot.c dnrm2.c dscal.c])
33
27
 
34
- $INCFLAGS << " -I$(srcdir)/liblinear"
35
- $VPATH << "$(srcdir)/liblinear"
36
- $VPATH << "$(srcdir)/liblinear/blas"
28
+ $INCFLAGS << " -I$(srcdir)/src"
29
+ $VPATH << "$(srcdir)/src"
30
+ $VPATH << "$(srcdir)/src/blas"
37
31
 
38
32
  create_makefile('numo/liblinear/liblinearext')
@@ -0,0 +1,215 @@
1
+ /**
2
+ * Copyright (c) 2019-2022 Atsushi Tatsuma
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * * Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ *
11
+ * * Redistributions in binary form must reproduce the above copyright notice,
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ *
15
+ * * Neither the name of the copyright holder nor the names of its
16
+ * contributors may be used to endorse or promote products derived from
17
+ * this software without specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
+
31
+ #include "liblinearext.hpp"
32
+
33
+ extern "C" void Init_liblinearext(void) {
34
+ rb_require("numo/narray");
35
+
36
+ /**
37
+ * Document-module: Numo
38
+ * Numo is the top level namespace of NUmerical MOdules for Ruby.
39
+ */
40
+
41
+ /**
42
+ * Document-module: Numo::Liblinear
43
+ * Numo::Liblinear is a binding library for LIBLINEAR that handles dataset with Numo::NArray.
44
+ */
45
+ VALUE mLiblinear = rb_define_module_under(mNumo, "Liblinear");
46
+
47
+ /* The version of LIBLINEAR used in backgroud library. */
48
+ rb_define_const(mLiblinear, "LIBLINEAR_VERSION", INT2NUM(LIBLINEAR_VERSION));
49
+
50
+ /**
51
+ * Document-module: Numo::Liblinear::SolverType
52
+ * The module consisting of constants for solver type that used for parameter of LIBLINER.
53
+ */
54
+ VALUE mSolverType = rb_define_module_under(mLiblinear, "SolverType");
55
+ /* L2-regularized logistic regression (primal) */
56
+ rb_define_const(mSolverType, "L2R_LR", INT2NUM(L2R_LR));
57
+ /* L2-regularized L2-loss support vector classification (dual) */
58
+ rb_define_const(mSolverType, "L2R_L2LOSS_SVC_DUAL", INT2NUM(L2R_L2LOSS_SVC_DUAL));
59
+ /* L2-regularized L2-loss support vector classification (primal) */
60
+ rb_define_const(mSolverType, "L2R_L2LOSS_SVC", INT2NUM(L2R_L2LOSS_SVC));
61
+ /* L2-regularized L1-loss support vector classification (dual) */
62
+ rb_define_const(mSolverType, "L2R_L1LOSS_SVC_DUAL", INT2NUM(L2R_L1LOSS_SVC_DUAL));
63
+ /* support vector classification by Crammer and Singer */
64
+ rb_define_const(mSolverType, "MCSVM_CS", INT2NUM(MCSVM_CS));
65
+ /* L1-regularized L2-loss support vector classification */
66
+ rb_define_const(mSolverType, "L1R_L2LOSS_SVC", INT2NUM(L1R_L2LOSS_SVC));
67
+ /* L1-regularized logistic regression */
68
+ rb_define_const(mSolverType, "L1R_LR", INT2NUM(L1R_LR));
69
+ /* L2-regularized logistic regression (dual) */
70
+ rb_define_const(mSolverType, "L2R_LR_DUAL", INT2NUM(L2R_LR_DUAL));
71
+ /* L2-regularized L2-loss support vector regression (primal) */
72
+ rb_define_const(mSolverType, "L2R_L2LOSS_SVR", INT2NUM(L2R_L2LOSS_SVR));
73
+ /* L2-regularized L2-loss support vector regression (dual) */
74
+ rb_define_const(mSolverType, "L2R_L2LOSS_SVR_DUAL", INT2NUM(L2R_L2LOSS_SVR_DUAL));
75
+ /* L2-regularized L1-loss support vector regression (dual) */
76
+ rb_define_const(mSolverType, "L2R_L1LOSS_SVR_DUAL", INT2NUM(L2R_L1LOSS_SVR_DUAL));
77
+ /* one-class support vector machine (dual) */
78
+ rb_define_const(mSolverType, "ONECLASS_SVM", INT2NUM(ONECLASS_SVM));
79
+
80
+ /**
81
+ * Train the model according to the given training data.
82
+ *
83
+ * @overload train(x, y, param) -> Hash
84
+ * @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to be used for training the model.
85
+ * @param y [Numo::DFloat] (shape: [n_samples]) The labels or target values for samples.
86
+ * @param param [Hash] The parameters of a model.
87
+ *
88
+ * @example
89
+ * require 'numo/liblinear'
90
+ *
91
+ * # Prepare training dataset.
92
+ * x = Numo::DFloat[[-0.8, 1.0], [-0.5, 0.8], [0.9, -0.8], [0.8, -0.7]]
93
+ * y = Numo::Int32[-1, -1, 1, 1]
94
+ *
95
+ * # Train L2-regularized L2-loss support vector classifier.
96
+ * param = {
97
+ * solver_type: Numo::Liblinear::SolverType::L2R_L2LOSS_SVC_DUAL,
98
+ * C: 0.1,
99
+ * random_seed: 1
100
+ * }
101
+ * model = Numo::Liblinear.train(x, y, param)
102
+ *
103
+ * # Predict labels of test data.
104
+ * x_test = Numo::DFloat[[-0.7, 0.9], [0.5, -0.4]]
105
+ * result = Numo::Liblinear.predict(x_test, param, model)
106
+ * p result
107
+ * # Numo::DFloat#shape=[2]
108
+ * # [-1, 1]
109
+ *
110
+ * @raise [ArgumentError] If the sample array is not 2-dimensional, the label array is not 1-dimensional,
111
+ * the sample array and label array do not have the same number of samples, or
112
+ * the hyperparameter has an invalid value, this error is raised.
113
+ * @return [Hash] The model obtained from the training procedure.
114
+ */
115
+ rb_define_module_function(mLiblinear, "train", RUBY_METHOD_FUNC(numo_liblinear_train), 3);
116
+ /**
117
+ * Perform cross validation under given parameters. The given samples are separated to n_fols folds.
118
+ * The predicted labels or values in the validation process are returned.
119
+ *
120
+ * @overload cv(x, y, param, n_folds) -> Numo::DFloat
121
+ * @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to be used for training the model.
122
+ * @param y [Numo::DFloat] (shape: [n_samples]) The labels or target values for samples.
123
+ * @param param [Hash] The parameters of a model.
124
+ * @param n_folds [Integer] The number of folds.
125
+ *
126
+ * @example
127
+ * require 'numo/liblinear'
128
+ *
129
+ * # x: samples
130
+ * # y: labels
131
+ *
132
+ * # Define parameters of L2-regularized L2-loss support vector classification.
133
+ * param = {
134
+ * solver_type: Numo::Liblinear::SolverType::L2R_L2LOSS_SVC_DUAL,
135
+ * C: 1,
136
+ * random_seed: 1,
137
+ * verbose: true
138
+ * }
139
+ *
140
+ * # Perform 5-cross validation.
141
+ * n_folds = 5
142
+ * res = Numo::Liblinear::cv(x, y, param, n_folds)
143
+ *
144
+ * # Print mean accuracy.
145
+ * mean_accuracy = y.eq(res).count.fdiv(y.size)
146
+ * puts "Accuracy: %.1f %%" % (100 * mean_accuracy)
147
+ *
148
+ * @raise [ArgumentError] If the sample array is not 2-dimensional, the label array is not 1-dimensional,
149
+ * the sample array and label array do not have the same number of samples, or
150
+ * the hyperparameter has an invalid value, this error is raised.
151
+ * @return [Numo::DFloat] (shape: [n_samples]) The predicted class label or value of each sample.
152
+ */
153
+ rb_define_module_function(mLiblinear, "cv", RUBY_METHOD_FUNC(numo_liblinear_cross_validation), 4);
154
+ /**
155
+ * Predict class labels or values for given samples.
156
+ *
157
+ * @overload predict(x, param, model) -> Numo::DFloat
158
+ * @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to calculate the scores.
159
+ * @param param [Hash] The parameters of the trained model.
160
+ * @param model [Hash] The model obtained from the training procedure.
161
+ *
162
+ * @raise [ArgumentError] If the sample array is not 2-dimensional, this error is raised.
163
+ * @return [Numo::DFloat] (shape: [n_samples]) The predicted class label or value of each sample.
164
+ */
165
+ rb_define_module_function(mLiblinear, "predict", RUBY_METHOD_FUNC(numo_liblinear_predict), 3);
166
+ /**
167
+ * Calculate decision values for given samples.
168
+ *
169
+ * @overload decision_function(x, param, model) -> Numo::DFloat
170
+ * @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to calculate the scores.
171
+ * @param param [Hash] The parameters of the trained model.
172
+ * @param model [Hash] The model obtained from the training procedure.
173
+ *
174
+ * @raise [ArgumentError] If the sample array is not 2-dimensional, this error is raised.
175
+ * @return [Numo::DFloat] (shape: [n_samples, n_classes]) The decision value of each sample.
176
+ */
177
+ rb_define_module_function(mLiblinear, "decision_function", RUBY_METHOD_FUNC(numo_liblinear_decision_function), 3);
178
+ /**
179
+ * Predict class probability for given samples.
180
+ * The model must have probability information calcualted in training procedure.
181
+ * The method supports only the logistic regression.
182
+ *
183
+ * @overload predict_proba(x, param, model) -> Numo::DFloat
184
+ * @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the class probabilities.
185
+ * @param param [Hash] The parameters of the trained Logistic Regression model.
186
+ * @param model [Hash] The model obtained from the training procedure.
187
+ *
188
+ * @raise [ArgumentError] If the sample array is not 2-dimensional, this error is raised.
189
+ * @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probablity of each class per sample.
190
+ */
191
+ rb_define_module_function(mLiblinear, "predict_proba", RUBY_METHOD_FUNC(numo_liblinear_predict_proba), 3);
192
+ /**
193
+ * Load the parameters and model from a text file with LIBLINEAR format.
194
+ *
195
+ * @overload load_model(filename) -> Array
196
+ * @param filename [String] The path to a file to load.
197
+ *
198
+ * @raise [IOError] This error raises when failed to load the model file.
199
+ * @return [Array] Array contains the parameters and model.
200
+ */
201
+ rb_define_module_function(mLiblinear, "load_model", RUBY_METHOD_FUNC(numo_liblinear_load_model), 1);
202
+ /**
203
+ * Save the parameters and model as a text file with LIBLINEAR format. The saved file can be used with the liblinear tools.
204
+ * Note that the save_model saves only the parameters necessary for estimation with the trained model.
205
+ *
206
+ * @overload save_model(filename, param, model) -> Boolean
207
+ * @param filename [String] The path to a file to save.
208
+ * @param param [Hash] The parameters of the trained model.
209
+ * @param model [Hash] The model obtained from the training procedure.
210
+ *
211
+ * @raise [IOError] This error raises when failed to save the model file.
212
+ * @return [Boolean] true on success, or false if an error occurs.
213
+ */
214
+ rb_define_module_function(mLiblinear, "save_model", RUBY_METHOD_FUNC(numo_liblinear_save_model), 3);
215
+ }