numo-libsvm 0.2.0 → 0.3.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
  SHA1:
3
- metadata.gz: 92066a1f30fa986bae110cffad133765bc1a2356
4
- data.tar.gz: db27417bc129d089716a97df86b88c3dbd935ac1
3
+ metadata.gz: c8cf97ac572136b9ee1d0919dc308eded11bc3ce
4
+ data.tar.gz: 6df5e37fa325cf802643626c95f88498b75f50a8
5
5
  SHA512:
6
- metadata.gz: 77064dc4a7128b69ca9130165378dd53a71d98ce8fc1cdccc1462a913d1a5f79ebb1394750cdb67122bf3e37ddfd986ade99c39cde299aef492da11b1d58f548
7
- data.tar.gz: b37b562c48d434c21b94fff3d1718703011d69be188ed078936936d84fca3d0164cefafad1c880f25191eb72fccd2207824f3e434552b88dc51f736bea8090bb
6
+ metadata.gz: ac703a48952bfcf3909c672f6901c8873e4c5e1f2c998da28d3912bc62d254f4cb2e689417c8472fb9e10a9f80c6a9f4cfda4b64850de1cbb4ca1d34527277da
7
+ data.tar.gz: 826f1d341a53c733dfc0917615829b66673d18e43cd4efce062634057b84201347a1398da2dbb6b80b8baaebfbea784b8c10d57f2b0e7da73e9ac6e9389d2917
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
+ # 0.3.0
2
+ - Add random_seed parameter for specifying seed to give to srand function.
3
+ - Several documentation improvements.
4
+
1
5
  # 0.2.0
2
- - Add valation of method parameters.
6
+ - Add validation of method parameters.
3
7
  - Several documentation improvements.
4
8
 
5
9
  # 0.1.0
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Build Status](https://travis-ci.org/yoshoku/numo-libsvm.svg?branch=master)](https://travis-ci.org/yoshoku/numo-libsvm)
4
4
  [![Gem Version](https://badge.fury.io/rb/numo-libsvm.svg)](https://badge.fury.io/rb/numo-libsvm)
5
5
  [![BSD 3-Clause License](https://img.shields.io/badge/License-BSD%203--Clause-orange.svg)](https://github.com/yoshoku/numo-libsvm/blob/master/LICENSE.txt)
6
- [![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg)](https://www.rubydoc.info/gems/numo-libsvm/0.2.0)
6
+ [![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg)](https://www.rubydoc.info/gems/numo-libsvm/0.3.0)
7
7
 
8
8
  Numo::Libsvm is a Ruby gem binding to the [LIBSVM](https://github.com/cjlin1/libsvm) library.
9
9
  LIBSVM is one of the famous libraries that implemented Support Vector Machines,
@@ -172,7 +172,6 @@ The svm_parameter is detailed in [LIBSVM README](https://github.com/cjlin1/libsv
172
172
  param = {
173
173
  svm_type: # [Integer] Type of SVM
174
174
  Numo::Libsvm::SvmType::C_SVC,
175
- # for kernel function
176
175
  kernel_type: # [Integer] Type of kernel function
177
176
  Numo::Libsvm::KernelType::RBF,
178
177
  degree: 3, # [Integer] Degree in polynomial kernel function
@@ -190,7 +189,8 @@ param = {
190
189
  nu: 0.5, # [Float] Parameter nu of nu-SVC, one-class SVM, and nu-SVR
191
190
  p: 0.1, # [Float] Parameter epsilon in loss function of epsilon-SVR
192
191
  shrinking: true, # [Boolean] Whether to use the shrinking heuristics
193
- probability: false # [Boolean] Whether to train a SVC or SVR model for probability estimates
192
+ probability: false, # [Boolean] Whether to train a SVC or SVR model for probability estimates
193
+ random_seed: 1 # [Integer/Nil] Random seed
194
194
  }
195
195
  ```
196
196
 
@@ -16,6 +16,30 @@ void print_null(const char *s) {}
16
16
  * @param y [Numo::DFloat] (shape: [n_samples]) The labels or target values for samples.
17
17
  * @param param [Hash] The parameters of an SVM model.
18
18
  *
19
+ * @example
20
+ * require 'numo/libsvm'
21
+ *
22
+ * # Prepare XOR data.
23
+ * x = Numo::DFloat[[-0.8, -0.7], [0.9, 0.8], [-0.7, 0.9], [0.8, -0.9]]
24
+ * y = Numo::Int32[-1, -1, 1, 1]
25
+ *
26
+ * # Train C-Support Vector Classifier with RBF kernel.
27
+ * param = {
28
+ * svm_type: Numo::Libsvm::SvmType::C_SVC,
29
+ * kernel_type: Numo::Libsvm::KernelType::RBF,
30
+ * gamma: 2.0,
31
+ * C: 1,
32
+ * random_seed: 1
33
+ * }
34
+ * model = Numo::Libsvm.train(x, y, param)
35
+ *
36
+ * # Predict labels of test data.
37
+ * x_test = Numo::DFloat[[-0.4, -0.5], [0.5, -0.4]]
38
+ * result = Numo::Libsvm.predict(x_test, param, model)
39
+ * p result
40
+ * # Numo::DFloat#shape=[2]
41
+ * # [-1, 1]
42
+ *
19
43
  * @raise [ArgumentError] If the sample array is not 2-dimensional, the label array is not 1-dimensional,
20
44
  * the sample array and label array do not have the same number of samples, or
21
45
  * the hyperparameter has an invalid value, this error is raised.
@@ -30,6 +54,7 @@ VALUE train(VALUE self, VALUE x_val, VALUE y_val, VALUE param_hash)
30
54
  narray_t* x_nary;
31
55
  narray_t* y_nary;
32
56
  char* err_msg;
57
+ VALUE random_seed;
33
58
  VALUE model_hash;
34
59
 
35
60
  if (CLASS_OF(x_val) != numo_cDFloat) {
@@ -60,6 +85,11 @@ VALUE train(VALUE self, VALUE x_val, VALUE y_val, VALUE param_hash)
60
85
  return Qnil;
61
86
  }
62
87
 
88
+ random_seed = rb_hash_aref(param_hash, ID2SYM(rb_intern("random_seed")));
89
+ if (!NIL_P(random_seed)) {
90
+ srand(NUM2UINT(random_seed));
91
+ }
92
+
63
93
  param = rb_hash_to_svm_parameter(param_hash);
64
94
  problem = dataset_to_svm_problem(x_val, y_val);
65
95
 
@@ -107,6 +137,7 @@ VALUE cross_validation(VALUE self, VALUE x_val, VALUE y_val, VALUE param_hash, V
107
137
  narray_t* x_nary;
108
138
  narray_t* y_nary;
109
139
  char* err_msg;
140
+ VALUE random_seed;
110
141
  struct svm_problem* problem;
111
142
  struct svm_parameter* param;
112
143
 
@@ -138,6 +169,11 @@ VALUE cross_validation(VALUE self, VALUE x_val, VALUE y_val, VALUE param_hash, V
138
169
  return Qnil;
139
170
  }
140
171
 
172
+ random_seed = rb_hash_aref(param_hash, ID2SYM(rb_intern("random_seed")));
173
+ if (!NIL_P(random_seed)) {
174
+ srand(NUM2UINT(random_seed));
175
+ }
176
+
141
177
  param = rb_hash_to_svm_parameter(param_hash);
142
178
  problem = dataset_to_svm_problem(x_val, y_val);
143
179
 
@@ -3,6 +3,6 @@
3
3
  module Numo
4
4
  module Libsvm
5
5
  # The version of Numo::Libsvm you are using.
6
- VERSION = '0.2.0'
6
+ VERSION = '0.3.0'
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numo-libsvm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshoku
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-15 00:00:00.000000000 Z
11
+ date: 2019-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-narray