numo-liblinear 1.2.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +2 -3
  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 +19 -38
  21. data/.github/workflows/build.yml +0 -28
  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 -49
  42. data/sig/patch.rbs +0 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fbe98bbdc79c6e0f8cb5392d0b990b7d0fc18033f2ef3f6ffe5634550b7a8065
4
- data.tar.gz: 2a1adfe9e9b653885edc7256b517d8ed9ca8ba359cb5dda492592fa3169a664f
3
+ metadata.gz: 8ea54dc3ead49a3edb3d55b2bf46c673fca3a11b8b29001658fc453d8bfd8831
4
+ data.tar.gz: 3c4751dce386d1127ab4c1570807671a7b8ab73f632899e63655096cead86ffe
5
5
  SHA512:
6
- metadata.gz: 8950474d1509b6bdf8398b0fc8961b81298110e7bcf609d60cd36ad7b1b1c5e287c349a07d3ae75799e2c5ca76c8b638a864914eaf32dfdb879c6bfe6015533b
7
- data.tar.gz: 3984b878d18885c3cafe2f0afccef0974e0c9e8722da91f475ba41edfc20b54616b7947350f9a218c40dfed4d5749e5ec52f30ee4600101827ee8df3701d9d23
6
+ metadata.gz: 1f016fa4a2a372fb7948eea6418973ecf9228f3e8a8b58b23b7da9360f0dc3f0facd85a45d029b259bfd5d52ffb4a9b42565ebd1d163c9ac881f37c978a3f22b
7
+ data.tar.gz: 7119f78866b27df545177c6b6d24b2df261caade7a8437721df5b99c616af16ef2a039919ffc9ed35b5ee98760cc77520e9d708edeb9a851d822232b68c43806
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ # 2.1.0
2
+ - Update bundled LIBLINEAR to 2.44
3
+
4
+ # 2.0.0
5
+ - Redesign native extension codes.
6
+ - Change not ot use git submodule for LIBLINEAR codes bundle.
7
+ - Introduce conventional commits.
8
+
9
+ # 1.2.2
10
+ - Remove dependent gem's type declaration file from installation files.
11
+
1
12
  # 1.2.1
2
13
  - Fix version specifier of runtime dependencies.
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
@@ -173,7 +173,6 @@ param = {
173
173
  Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/numo-liblinear.
174
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.
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
+ }