numo-libsvm 1.1.2 → 2.1.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 +13 -0
- data/LICENSE.txt +1 -1
- data/README.md +1 -5
- data/ext/numo/libsvm/extconf.rb +6 -16
- data/ext/numo/libsvm/libsvmext.cpp +220 -0
- data/ext/numo/libsvm/libsvmext.hpp +721 -0
- data/ext/numo/libsvm/src/COPYRIGHT +31 -0
- data/ext/numo/libsvm/{libsvm → src}/svm.cpp +134 -18
- data/ext/numo/libsvm/{libsvm → src}/svm.h +2 -1
- data/lib/numo/libsvm/version.rb +1 -1
- data/sig/numo/libsvm.rbs +1 -0
- metadata +12 -31
- 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/libsvm/converter.c +0 -204
- data/ext/numo/libsvm/converter.h +0 -20
- data/ext/numo/libsvm/kernel_type.c +0 -22
- data/ext/numo/libsvm/kernel_type.h +0 -9
- data/ext/numo/libsvm/libsvmext.c +0 -578
- data/ext/numo/libsvm/libsvmext.h +0 -18
- data/ext/numo/libsvm/svm_model.c +0 -89
- data/ext/numo/libsvm/svm_model.h +0 -15
- data/ext/numo/libsvm/svm_parameter.c +0 -88
- data/ext/numo/libsvm/svm_parameter.h +0 -15
- data/ext/numo/libsvm/svm_problem.c +0 -90
- data/ext/numo/libsvm/svm_problem.h +0 -12
- data/ext/numo/libsvm/svm_type.c +0 -22
- data/ext/numo/libsvm/svm_type.h +0 -9
- data/numo-libsvm.gemspec +0 -47
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37613a10119b2974024687c3f781d271ebaa30b15ccff6100df17c32642dcd2d
|
4
|
+
data.tar.gz: 210e072576d4fa859364fdb522e6f02c9be12458e5ed0647fcd8bebedfdb3e84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 142e15fe744460f679f7bd7978f1654e2e71a40a49ce3a414c3c0e68b507d6d9c286eec9edd37a10f5b9fc60a940079c42e59d1283ba607cd7748b80d16fa12f
|
7
|
+
data.tar.gz: 67909b309d9a186e77dc716cc2e8b1299d0be3baf10be7be87fc89f2bc67a47b087063827be2f8ba750378d354f745c58dec0feb072f8eea73c883c3c034a669
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
# 2.1.0
|
2
|
+
- Update bundled LIBSVM to 3.30.
|
3
|
+
- Support for probilistic outputs of one-class SVM.
|
4
|
+
|
5
|
+
# 2.0.1
|
6
|
+
- Chnage to use memcpy to copy vector.
|
7
|
+
- Refactor codes and configs with RuboCop.
|
8
|
+
|
9
|
+
# 2.0.0
|
10
|
+
- Redesign native extension codes.
|
11
|
+
- Change not ot use git submodule for LIBSVM codes bundle.
|
12
|
+
- Introduce conventional commits.
|
13
|
+
|
1
14
|
# 1.1.2
|
2
15
|
- Remove dependent gem's type declaration file from installation files.
|
3
16
|
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -189,12 +189,8 @@ param = {
|
|
189
189
|
|
190
190
|
## Contributing
|
191
191
|
|
192
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/numo-libsvm. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](
|
192
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/numo-libsvm. 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.
|
193
193
|
|
194
194
|
## License
|
195
195
|
|
196
196
|
The gem is available as open source under the terms of the [BSD-3-Clause License](https://opensource.org/licenses/BSD-3-Clause).
|
197
|
-
|
198
|
-
## Code of Conduct
|
199
|
-
|
200
|
-
Everyone interacting in the Numo::Libsvm project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/yoshoku/numo-libsvm/blob/main/CODE_OF_CONDUCT.md).
|
data/ext/numo/libsvm/extconf.rb
CHANGED
@@ -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,20 +17,13 @@ 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
|
-
|
23
|
+
abort 'libstdc++ is not found.' unless have_library('stdc++')
|
30
24
|
|
31
|
-
$srcs = Dir.glob("#{$srcdir}
|
32
|
-
$
|
33
|
-
|
34
|
-
dir = File.basename(path)
|
35
|
-
$INCFLAGS << " -I$(srcdir)/#{dir}"
|
36
|
-
$VPATH << "$(srcdir)/#{dir}"
|
37
|
-
end
|
25
|
+
$srcs = Dir.glob("#{$srcdir}/**/*.cpp").map { |path| File.basename(path) }
|
26
|
+
$INCFLAGS << " -I$(srcdir)/src"
|
27
|
+
$VPATH << "$(srcdir)/src"
|
38
28
|
|
39
29
|
create_makefile('numo/libsvm/libsvmext')
|
@@ -0,0 +1,220 @@
|
|
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 "libsvmext.hpp"
|
32
|
+
|
33
|
+
extern "C" void Init_libsvmext(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::Libsvm
|
43
|
+
* Numo::Libsvm is a binding library for LIBSVM that handles dataset with Numo::NArray.
|
44
|
+
*/
|
45
|
+
VALUE mLibsvm = rb_define_module_under(mNumo, "Libsvm");
|
46
|
+
|
47
|
+
/* The version of LIBSVM used in backgroud library. */
|
48
|
+
rb_define_const(mLibsvm, "LIBSVM_VERSION", INT2NUM(LIBSVM_VERSION));
|
49
|
+
|
50
|
+
/**
|
51
|
+
* Document-module: Numo::Libsvm::SvmType
|
52
|
+
* The module consisting of constants for SVM algorithm type that used for parameter of LIBSVM.
|
53
|
+
*/
|
54
|
+
VALUE mSvmType = rb_define_module_under(mLibsvm, "SvmType");
|
55
|
+
/* C-SVM classification */
|
56
|
+
rb_define_const(mSvmType, "C_SVC", INT2NUM(C_SVC));
|
57
|
+
/* nu-SVM classification */
|
58
|
+
rb_define_const(mSvmType, "NU_SVC", INT2NUM(NU_SVC));
|
59
|
+
/* one-class-SVM */
|
60
|
+
rb_define_const(mSvmType, "ONE_CLASS", INT2NUM(ONE_CLASS));
|
61
|
+
/* epsilon-SVM regression */
|
62
|
+
rb_define_const(mSvmType, "EPSILON_SVR", INT2NUM(EPSILON_SVR));
|
63
|
+
/* nu-SVM regression */
|
64
|
+
rb_define_const(mSvmType, "NU_SVR", INT2NUM(NU_SVR));
|
65
|
+
|
66
|
+
/**
|
67
|
+
* Document-module: Numo::Libsvm::KernelType
|
68
|
+
* The module consisting of constants for kernel type that used for parameter of LIBSVM.
|
69
|
+
*/
|
70
|
+
VALUE mKernelType = rb_define_module_under(mLibsvm, "KernelType");
|
71
|
+
/* Linear kernel; u' * v */
|
72
|
+
rb_define_const(mKernelType, "LINEAR", INT2NUM(LINEAR));
|
73
|
+
/* Polynomial kernel; (gamma * u' * v + coef0)^degree */
|
74
|
+
rb_define_const(mKernelType, "POLY", INT2NUM(POLY));
|
75
|
+
/* RBF kernel; exp(-gamma * ||u - v||^2) */
|
76
|
+
rb_define_const(mKernelType, "RBF", INT2NUM(RBF));
|
77
|
+
/* Sigmoid kernel; tanh(gamma * u' * v + coef0) */
|
78
|
+
rb_define_const(mKernelType, "SIGMOID", INT2NUM(SIGMOID));
|
79
|
+
/* Precomputed kernel */
|
80
|
+
rb_define_const(mKernelType, "PRECOMPUTED", INT2NUM(PRECOMPUTED));
|
81
|
+
|
82
|
+
/**
|
83
|
+
* Train the SVM model according to the given training data.
|
84
|
+
*
|
85
|
+
* @overload train(x, y, param) -> Hash
|
86
|
+
* @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to be used for training the model.
|
87
|
+
* @param y [Numo::DFloat] (shape: [n_samples]) The labels or target values for samples.
|
88
|
+
* @param param [Hash] The parameters of an SVM model.
|
89
|
+
*
|
90
|
+
* @example
|
91
|
+
* require 'numo/libsvm'
|
92
|
+
*
|
93
|
+
* # Prepare XOR data.
|
94
|
+
* x = Numo::DFloat[[-0.8, -0.7], [0.9, 0.8], [-0.7, 0.9], [0.8, -0.9]]
|
95
|
+
* y = Numo::Int32[-1, -1, 1, 1]
|
96
|
+
*
|
97
|
+
* # Train C-Support Vector Classifier with RBF kernel.
|
98
|
+
* param = {
|
99
|
+
* svm_type: Numo::Libsvm::SvmType::C_SVC,
|
100
|
+
* kernel_type: Numo::Libsvm::KernelType::RBF,
|
101
|
+
* gamma: 2.0,
|
102
|
+
* C: 1,
|
103
|
+
* random_seed: 1
|
104
|
+
* }
|
105
|
+
* model = Numo::Libsvm.train(x, y, param)
|
106
|
+
*
|
107
|
+
* # Predict labels of test data.
|
108
|
+
* x_test = Numo::DFloat[[-0.4, -0.5], [0.5, -0.4]]
|
109
|
+
* result = Numo::Libsvm.predict(x_test, param, model)
|
110
|
+
* p result
|
111
|
+
* # Numo::DFloat#shape=[2]
|
112
|
+
* # [-1, 1]
|
113
|
+
*
|
114
|
+
* @raise [ArgumentError] If the sample array is not 2-dimensional, the label array is not 1-dimensional,
|
115
|
+
* the sample array and label array do not have the same number of samples, or
|
116
|
+
* the hyperparameter has an invalid value, this error is raised.
|
117
|
+
* @return [Hash] The model obtained from the training procedure.
|
118
|
+
*/
|
119
|
+
rb_define_module_function(mLibsvm, "train", RUBY_METHOD_FUNC(numo_libsvm_train), 3);
|
120
|
+
/**
|
121
|
+
* Perform cross validation under given parameters. The given samples are separated to n_fols folds.
|
122
|
+
* The predicted labels or values in the validation process are returned.
|
123
|
+
*
|
124
|
+
* @overload cv(x, y, param, n_folds) -> Numo::DFloat
|
125
|
+
* @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to be used for training the model.
|
126
|
+
* @param y [Numo::DFloat] (shape: [n_samples]) The labels or target values for samples.
|
127
|
+
* @param param [Hash] The parameters of an SVM model.
|
128
|
+
* @param n_folds [Integer] The number of folds.
|
129
|
+
*
|
130
|
+
* @example
|
131
|
+
* require 'numo/libsvm'
|
132
|
+
*
|
133
|
+
* # x: samples
|
134
|
+
* # y: labels
|
135
|
+
*
|
136
|
+
* # Define parameters of C-SVC with RBF Kernel.
|
137
|
+
* param = {
|
138
|
+
* svm_type: Numo::Libsvm::SvmType::C_SVC,
|
139
|
+
* kernel_type: Numo::Libsvm::KernelType::RBF,
|
140
|
+
* gamma: 1.0,
|
141
|
+
* C: 1,
|
142
|
+
* random_seed: 1,
|
143
|
+
* verbose: true
|
144
|
+
* }
|
145
|
+
*
|
146
|
+
* # Perform 5-cross validation.
|
147
|
+
* n_folds = 5
|
148
|
+
* res = Numo::Libsvm.cv(x, y, param, n_folds)
|
149
|
+
*
|
150
|
+
* # Print mean accuracy.
|
151
|
+
* mean_accuracy = y.eq(res).count.fdiv(y.size)
|
152
|
+
* puts "Accuracy: %.1f %%" % (100 * mean_accuracy)
|
153
|
+
*
|
154
|
+
* @raise [ArgumentError] If the sample array is not 2-dimensional, the label array is not 1-dimensional,
|
155
|
+
* the sample array and label array do not have the same number of samples, or
|
156
|
+
* the hyperparameter has an invalid value, this error is raised.
|
157
|
+
* @return [Numo::DFloat] (shape: [n_samples]) The predicted class label or value of each sample.
|
158
|
+
*/
|
159
|
+
rb_define_module_function(mLibsvm, "cv", RUBY_METHOD_FUNC(numo_libsvm_cross_validation), 4);
|
160
|
+
/**
|
161
|
+
* Predict class labels or values for given samples.
|
162
|
+
*
|
163
|
+
* @overload predict(x, param, model) -> Numo::DFloat
|
164
|
+
* @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to calculate the scores.
|
165
|
+
* @param param [Hash] The parameters of the trained SVM model.
|
166
|
+
* @param model [Hash] The model obtained from the training procedure.
|
167
|
+
*
|
168
|
+
* @raise [ArgumentError] If the sample array is not 2-dimensional, this error is raised.
|
169
|
+
* @return [Numo::DFloat] (shape: [n_samples]) The predicted class label or value of each sample.
|
170
|
+
*/
|
171
|
+
rb_define_module_function(mLibsvm, "predict", RUBY_METHOD_FUNC(numo_libsvm_predict), 3);
|
172
|
+
/**
|
173
|
+
* Calculate decision values for given samples.
|
174
|
+
*
|
175
|
+
* @overload decision_function(x, param, model) -> Numo::DFloat
|
176
|
+
* @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to calculate the scores.
|
177
|
+
* @param param [Hash] The parameters of the trained SVM model.
|
178
|
+
* @param model [Hash] The model obtained from the training procedure.
|
179
|
+
*
|
180
|
+
* @raise [ArgumentError] If the sample array is not 2-dimensional, this error is raised.
|
181
|
+
* @return [Numo::DFloat] (shape: [n_samples, n_classes * (n_classes - 1) / 2]) The decision value of each sample.
|
182
|
+
*/
|
183
|
+
rb_define_module_function(mLibsvm, "decision_function", RUBY_METHOD_FUNC(numo_libsvm_decision_function), 3);
|
184
|
+
/**
|
185
|
+
* Predict class probability for given samples. The model must have probability information calcualted in training procedure.
|
186
|
+
* The parameter ':probability' set to 1 in training procedure.
|
187
|
+
*
|
188
|
+
* @overload predict_proba(x, param, model) -> Numo::DFloat
|
189
|
+
* @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the class probabilities.
|
190
|
+
* @param param [Hash] The parameters of the trained SVM model.
|
191
|
+
* @param model [Hash] The model obtained from the training procedure.
|
192
|
+
*
|
193
|
+
* @raise [ArgumentError] If the sample array is not 2-dimensional, this error is raised.
|
194
|
+
* @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probablity of each class per sample.
|
195
|
+
*/
|
196
|
+
rb_define_module_function(mLibsvm, "predict_proba", RUBY_METHOD_FUNC(numo_libsvm_predict_proba), 3);
|
197
|
+
/**
|
198
|
+
* Load the SVM parameters and model from a text file with LIBSVM format.
|
199
|
+
*
|
200
|
+
* @overload load_svm_model(filename) -> Array
|
201
|
+
* @param filename [String] The path to a file to load.
|
202
|
+
*
|
203
|
+
* @raise [IOError] This error raises when failed to load the model file.
|
204
|
+
* @return [Array] Array contains the SVM parameters and model.
|
205
|
+
*/
|
206
|
+
rb_define_module_function(mLibsvm, "load_svm_model", RUBY_METHOD_FUNC(numo_libsvm_load_model), 1);
|
207
|
+
/**
|
208
|
+
* Save the SVM parameters and model as a text file with LIBSVM format. The saved file can be used with the libsvm tools.
|
209
|
+
* Note that the svm_save_model saves only the parameters necessary for estimation with the trained model.
|
210
|
+
*
|
211
|
+
* @overload save_svm_model(filename, param, model) -> Boolean
|
212
|
+
* @param filename [String] The path to a file to save.
|
213
|
+
* @param param [Hash] The parameters of the trained SVM model.
|
214
|
+
* @param model [Hash] The model obtained from the training procedure.
|
215
|
+
*
|
216
|
+
* @raise [IOError] This error raises when failed to save the model file.
|
217
|
+
* @return [Boolean] true on success, or false if an error occurs.
|
218
|
+
*/
|
219
|
+
rb_define_module_function(mLibsvm, "save_svm_model", RUBY_METHOD_FUNC(numo_libsvm_save_model), 3);
|
220
|
+
}
|