numo-libsvm 1.1.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f321e889c3d7a6a617f6700885d5c39f00272a4cd2aad82e2efadb6818fe78aa
4
- data.tar.gz: bc8062c42b4f20cc1b6cfe11a2cebff4fc2881be180c92ca287e5f3c85b74a76
3
+ metadata.gz: 1c35f46a2bba049bfb540087ab92b16213aef0289088b0d07c656054bf62be75
4
+ data.tar.gz: c8473b0084bdb2e2a7396d906f5264bf9e0dc315f43aac1c5feae90d52e7a0e4
5
5
  SHA512:
6
- metadata.gz: faa13722b019804975e539f4f10a64bb602a3052529d885e168e343801a61a074af186efa6f0c8e124f03ed8b95cfc7067a7c06eda334d89a9c41f4775d18c2c
7
- data.tar.gz: 28b0830bd53f280c6511013ca838bf29ed510cf8062b7f47425d15aa287418452c14ea7f98567ee459518be9aa946cee40d3401a9011169e3f51d64f64f96ca8
6
+ metadata.gz: e164fae3d226b73f226fda35b5997b92752dc2386d2be8d9a4cfa00e8f0ca42eb7ed96f597f72ffd2f2203aef7ced2a0bc60b0c82fbc39c1089b3510d2275d49
7
+ data.tar.gz: be27aa1d940590808e9e6235e2a5fc00e9e5ec32715effea22218fa92bb85d922610db332c8a9d9dff6aa6ae9233e1d39bb6f7f28e0270a2057c8b6a5aef912e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 2.0.0
2
+ - Redesign native extension codes.
3
+ - Change not ot use git submodule for LIBSVM codes bundle.
4
+ - Introduce conventional commits.
5
+
1
6
  # 1.1.2
2
7
  - Remove dependent gem's type declaration file from installation files.
3
8
 
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
@@ -194,7 +194,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/yoshok
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).
@@ -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
- $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 << 'svm.cpp'
33
- Dir.glob("#{$srcdir}/*/") do |path|
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
+ }