rumale-svm 0.6.0 → 0.8.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: c8cb843c4d4245f90f57ee22722e8e899e8e5077e29773629001dccf9e243010
4
- data.tar.gz: b26af5c7c940562086ee2f7859315779db0e2be372c47da4e518b6fa77099d35
3
+ metadata.gz: 5611ab7bee24d673f0803b5679532326389a9a485dd5beafeb9d448ab5538363
4
+ data.tar.gz: 033024146647d6ba5e4acf08eb876dc35e0fe25f141555891dec77e84cd6572b
5
5
  SHA512:
6
- metadata.gz: 0ca52d3c96333ee5ee60ec43b36c9d7a80687d9a3241b29d0872cbc1569eb45e22966f52cbde34de103c43c46d88d64a8685703db58c58c9828db2d4daf8b9c7
7
- data.tar.gz: ff2a02ec2891dcf3626af1033dd3b181b697e8a43155bb6923e3cda31ef2116581b77e1201e934d152d6667a7b51ccf943a498cccf9fb8617a438899a99b5cdf
6
+ metadata.gz: b70772fd9480c0de3f28d477bdf35ccae3260f9a9d1156423cac8654caebbc1891a9182e1ee3a4b3b7add8ce885ac06c2e57e8d3ca46a71e9d7de99d46d1c33d
7
+ data.tar.gz: a38ccc2f952bb1e3fe5c4906e9225bb2d6c5c938b93aa6d08a8b66a2b20f4e94b0d96fe08c86f489050051eb149cdf68b74826b6a5f7439fbab5ee47de2b67e6
data/CHANGELOG.md CHANGED
@@ -1,8 +1,15 @@
1
+ # 0.8.0
2
+ - Refactor to support the new Rumale API.
3
+
4
+ # 0.7.0
5
+ - Support for probabilistic outputs with Rumale::SVM::OneClassSVM.
6
+ - Update numo-libsvm depedency to v2.1 or higher.
7
+
1
8
  # 0.6.0
2
9
  - Update numo-libsvm and numo-liblinear depedency to v2.0 or higher.
3
10
 
4
11
  # 0.5.1
5
- - Refator specs and config files.
12
+ - Refactor specs and config files.
6
13
 
7
14
  # 0.5.0
8
15
  - Add type declaration files.
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # Rumale::SVM
2
2
 
3
3
  [![Build Status](https://github.com/yoshoku/rumale-svm/workflows/build/badge.svg)](https://github.com/yoshoku/rumale-svm/actions?query=workflow%3Abuild)
4
- [![Coverage Status](https://coveralls.io/repos/github/yoshoku/rumale-svm/badge.svg?branch=main)](https://coveralls.io/github/yoshoku/rumale-svm?branch=main)
5
4
  [![Gem Version](https://badge.fury.io/rb/rumale-svm.svg)](https://badge.fury.io/rb/rumale-svm)
6
5
  [![BSD 3-Clause License](https://img.shields.io/badge/License-BSD%203--Clause-orange.svg)](https://github.com/yoshoku/rumale-svm/blob/main/LICENSE.txt)
7
6
  [![Documentation](https://img.shields.io/badge/api-reference-blue.svg)](https://yoshoku.github.io/rumale-svm/doc/)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'numo/liblinear'
4
- require 'rumale/base/base_estimator'
4
+ require 'rumale/base/estimator'
5
5
  require 'rumale/validation'
6
6
 
7
7
  module Rumale
@@ -12,10 +12,7 @@ module Rumale
12
12
  # estimator = Rumale::SVM::LinearOneClassSVM.new(nu: 0.05, random_seed: 1)
13
13
  # estimator.fit(training_samples, traininig_labels)
14
14
  # results = estimator.predict(testing_samples)
15
- class LinearOneClassSVM
16
- include Base::BaseEstimator
17
- include Validation
18
-
15
+ class LinearOneClassSVM < Rumale::Base::Estimator
19
16
  # Return the weight vector for LinearOneClassSVM.
20
17
  # @return [Numo::DFloat] (shape: [n_features])
21
18
  attr_reader :weight_vec
@@ -32,9 +29,7 @@ module Rumale
32
29
  # @param verbose [Boolean] The flag indicating whether to output learning process message
33
30
  # @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
34
31
  def initialize(nu: 0.05, reg_param: 1.0, tol: 1e-3, verbose: false, random_seed: nil)
35
- check_params_numeric(nu: nu, reg_param: reg_param, tol: tol)
36
- check_params_boolean(verbose: verbose)
37
- check_params_numeric_or_nil(random_seed: random_seed)
32
+ super()
38
33
  @params = {}
39
34
  @params[:nu] = nu.to_f
40
35
  @params[:reg_param] = reg_param.to_f
@@ -50,7 +45,7 @@ module Rumale
50
45
  #
51
46
  # @return [LinearOneClassSVM] The learned estimator itself.
52
47
  def fit(x, _y = nil)
53
- x = check_convert_sample_array(x)
48
+ x = Rumale::Validation.check_convert_sample_array(x)
54
49
  dummy = Numo::DFloat.ones(x.shape[0])
55
50
  @model = Numo::Liblinear.train(x, dummy, liblinear_params)
56
51
  @weight_vec = @model[:w].dup
@@ -63,8 +58,8 @@ module Rumale
63
58
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to compute the scores.
64
59
  # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
65
60
  def decision_function(x)
66
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
67
- x = check_convert_sample_array(x)
61
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
62
+ x = Rumale::Validation.check_convert_sample_array(x)
68
63
  Numo::Liblinear.decision_function(x, liblinear_params, @model)
69
64
  end
70
65
 
@@ -73,8 +68,8 @@ module Rumale
73
68
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
74
69
  # @return [Numo::Int32] (shape: [n_samples]) Predicted label per sample.
75
70
  def predict(x)
76
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
77
- x = check_convert_sample_array(x)
71
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
72
+ x = Rumale::Validation.check_convert_sample_array(x)
78
73
  Numo::Int32.cast(Numo::Liblinear.predict(x, liblinear_params, @model))
79
74
  end
80
75
 
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'numo/liblinear'
4
- require 'rumale/base/base_estimator'
4
+ require 'rumale/base/estimator'
5
5
  require 'rumale/base/classifier'
6
6
  require 'rumale/probabilistic_output'
7
+ require 'rumale/validation'
7
8
 
8
9
  module Rumale
9
10
  module SVM
@@ -13,9 +14,8 @@ module Rumale
13
14
  # estimator = Rumale::SVM::LinearSVC.new(penalty: 'l2', loss: 'squared_hinge', reg_param: 1.0, random_seed: 1)
14
15
  # estimator.fit(training_samples, traininig_labels)
15
16
  # results = estimator.predict(testing_samples)
16
- class LinearSVC
17
- include Base::BaseEstimator
18
- include Base::Classifier
17
+ class LinearSVC < Rumale::Base::Estimator
18
+ include Rumale::Base::Classifier
19
19
 
20
20
  # Return the weight vector for LinearSVC.
21
21
  # @return [Numo::DFloat] (shape: [n_classes, n_features])
@@ -43,10 +43,7 @@ module Rumale
43
43
  # @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
44
44
  def initialize(penalty: 'l2', loss: 'squared_hinge', dual: true, reg_param: 1.0,
45
45
  fit_bias: true, bias_scale: 1.0, probability: false, tol: 1e-3, verbose: false, random_seed: nil)
46
- check_params_string(penalty: penalty, loss: loss)
47
- check_params_numeric(reg_param: reg_param, bias_scale: bias_scale, tol: tol)
48
- check_params_boolean(dual: dual, fit_bias: fit_bias, probability: probability, verbose: verbose)
49
- check_params_numeric_or_nil(random_seed: random_seed)
46
+ super()
50
47
  @params = {}
51
48
  @params[:penalty] = penalty == 'l1' ? 'l1' : 'l2'
52
49
  @params[:loss] = loss == 'hinge' ? 'hinge' : 'squared_hinge'
@@ -66,9 +63,9 @@ module Rumale
66
63
  # @param y [Numo::Int32] (shape: [n_samples]) The labels to be used for fitting the model.
67
64
  # @return [LinearSVC] The learned classifier itself.
68
65
  def fit(x, y)
69
- x = check_convert_sample_array(x)
70
- y = check_convert_label_array(y)
71
- check_sample_label_size(x, y)
66
+ x = Rumale::Validation.check_convert_sample_array(x)
67
+ y = Rumale::Validation.check_convert_label_array(y)
68
+ Rumale::Validation.check_sample_size(x, y)
72
69
  xx = fit_bias? ? expand_feature(x) : x
73
70
  @model = Numo::Liblinear.train(xx, y, liblinear_params)
74
71
  @weight_vec, @bias_term = weight_and_bias(@model[:w])
@@ -81,8 +78,8 @@ module Rumale
81
78
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to compute the scores.
82
79
  # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
83
80
  def decision_function(x)
84
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
85
- x = check_convert_sample_array(x)
81
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
82
+ x = Rumale::Validation.check_convert_sample_array(x)
86
83
  xx = fit_bias? ? expand_feature(x) : x
87
84
  Numo::Liblinear.decision_function(xx, liblinear_params, @model)
88
85
  end
@@ -92,8 +89,8 @@ module Rumale
92
89
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
93
90
  # @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.
94
91
  def predict(x)
95
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
96
- x = check_convert_sample_array(x)
92
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
93
+ x = Rumale::Validation.check_convert_sample_array(x)
97
94
  xx = fit_bias? ? expand_feature(x) : x
98
95
  Numo::Int32.cast(Numo::Liblinear.predict(xx, liblinear_params, @model))
99
96
  end
@@ -104,8 +101,8 @@ module Rumale
104
101
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the probailities.
105
102
  # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.
106
103
  def predict_proba(x)
107
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
108
- x = check_convert_sample_array(x)
104
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
105
+ x = Rumale::Validation.check_convert_sample_array(x)
109
106
  if binary_class?
110
107
  probs = Numo::DFloat.zeros(x.shape[0], 2)
111
108
  probs[true, 0] = 1.0 / (Numo::NMath.exp(@prob_param[0] * decision_function(x) + @prob_param[1]) + 1.0)
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'numo/libsvm'
4
- require 'rumale/base/base_estimator'
4
+ require 'rumale/base/estimator'
5
5
  require 'rumale/base/regressor'
6
+ require 'rumale/validation'
6
7
 
7
8
  module Rumale
8
9
  module SVM
@@ -12,9 +13,8 @@ module Rumale
12
13
  # estimator = Rumale::SVM::LinearSVR.new(reg_param: 1.0, random_seed: 1)
13
14
  # estimator.fit(training_samples, traininig_target_values)
14
15
  # results = estimator.predict(testing_samples)
15
- class LinearSVR
16
- include Base::BaseEstimator
17
- include Base::Regressor
16
+ class LinearSVR < Rumale::Base::Estimator
17
+ include Rumale::Base::Regressor
18
18
 
19
19
  # Return the weight vector for LinearSVR.
20
20
  # @return [Numo::DFloat] (shape: [n_classes, n_features])
@@ -40,10 +40,7 @@ module Rumale
40
40
  # @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
41
41
  def initialize(loss: 'squared_epsilon_insensitive', dual: true, reg_param: 1.0, epsilon: 0.1,
42
42
  fit_bias: true, bias_scale: 1.0, tol: 1e-3, verbose: false, random_seed: nil)
43
- check_params_string(loss: loss)
44
- check_params_numeric(reg_param: reg_param, epsilon: epsilon, bias_scale: bias_scale, tol: tol)
45
- check_params_boolean(dual: dual, fit_bias: fit_bias, verbose: verbose)
46
- check_params_numeric_or_nil(random_seed: random_seed)
43
+ super()
47
44
  @params = {}
48
45
  @params[:loss] = loss == 'epsilon_insensitive' ? 'epsilon_insensitive' : 'squared_epsilon_insensitive'
49
46
  @params[:dual] = dual
@@ -62,9 +59,9 @@ module Rumale
62
59
  # @param y [Numo::DFloat] (shape: [n_samples]) The target values to be used for fitting the model.
63
60
  # @return [LinearSVR] The learned regressor itself.
64
61
  def fit(x, y)
65
- x = check_convert_sample_array(x)
66
- y = check_convert_tvalue_array(y)
67
- check_sample_tvalue_size(x, y)
62
+ x = Rumale::Validation.check_convert_sample_array(x)
63
+ y = Rumale::Validation.check_convert_target_value_array(y)
64
+ Rumale::Validation.check_sample_size(x, y)
68
65
  xx = fit_bias? ? expand_feature(x) : x
69
66
  @model = Numo::Liblinear.train(xx, y, liblinear_params)
70
67
  @weight_vec, @bias_term = weight_and_bias(@model[:w])
@@ -76,8 +73,8 @@ module Rumale
76
73
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
77
74
  # @return [Numo::DFloat] (shape: [n_samples]) Predicted value per sample.
78
75
  def predict(x)
79
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
80
- x = check_convert_sample_array(x)
76
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
77
+ x = Rumale::Validation.check_convert_sample_array(x)
81
78
  xx = fit_bias? ? expand_feature(x) : x
82
79
  Numo::Liblinear.predict(xx, liblinear_params, @model)
83
80
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'numo/liblinear'
4
- require 'rumale/base/base_estimator'
4
+ require 'rumale/base/estimator'
5
5
  require 'rumale/base/classifier'
6
6
 
7
7
  module Rumale
@@ -12,9 +12,8 @@ module Rumale
12
12
  # estimator = Rumale::SVM::LogisticRegression.new(penalty: 'l2', dual: false, reg_param: 1.0, random_seed: 1)
13
13
  # estimator.fit(training_samples, traininig_labels)
14
14
  # results = estimator.predict(testing_samples)
15
- class LogisticRegression
16
- include Base::BaseEstimator
17
- include Base::Classifier
15
+ class LogisticRegression < Rumale::Base::Estimator
16
+ include Rumale::Base::Classifier
18
17
 
19
18
  # Return the weight vector for LogisticRegression.
20
19
  # @return [Numo::DFloat] (shape: [n_classes, n_features])
@@ -39,10 +38,7 @@ module Rumale
39
38
  # @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
40
39
  def initialize(penalty: 'l2', dual: true, reg_param: 1.0,
41
40
  fit_bias: true, bias_scale: 1.0, tol: 1e-3, verbose: false, random_seed: nil)
42
- check_params_string(penalty: penalty)
43
- check_params_numeric(reg_param: reg_param, bias_scale: bias_scale, tol: tol)
44
- check_params_boolean(dual: dual, fit_bias: fit_bias, verbose: verbose)
45
- check_params_numeric_or_nil(random_seed: random_seed)
41
+ super()
46
42
  @params = {}
47
43
  @params[:penalty] = penalty == 'l1' ? 'l1' : 'l2'
48
44
  @params[:dual] = dual
@@ -60,9 +56,9 @@ module Rumale
60
56
  # @param y [Numo::Int32] (shape: [n_samples]) The labels to be used for fitting the model.
61
57
  # @return [LogisticRegression] The learned classifier itself.
62
58
  def fit(x, y)
63
- x = check_convert_sample_array(x)
64
- y = check_convert_label_array(y)
65
- check_sample_label_size(x, y)
59
+ x = Rumale::Validation.check_convert_sample_array(x)
60
+ y = Rumale::Validation.check_convert_label_array(y)
61
+ Rumale::Validation.check_sample_size(x, y)
66
62
  xx = fit_bias? ? expand_feature(x) : x
67
63
  @model = Numo::Liblinear.train(xx, y, liblinear_params)
68
64
  @weight_vec, @bias_term = weight_and_bias(@model[:w])
@@ -74,8 +70,8 @@ module Rumale
74
70
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to compute the scores.
75
71
  # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
76
72
  def decision_function(x)
77
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
78
- x = check_convert_sample_array(x)
73
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
74
+ x = Rumale::Validation.check_convert_sample_array(x)
79
75
  xx = fit_bias? ? expand_feature(x) : x
80
76
  Numo::Liblinear.decision_function(xx, liblinear_params, @model)
81
77
  end
@@ -85,8 +81,8 @@ module Rumale
85
81
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
86
82
  # @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.
87
83
  def predict(x)
88
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
89
- x = check_convert_sample_array(x)
84
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
85
+ x = Rumale::Validation.check_convert_sample_array(x)
90
86
  xx = fit_bias? ? expand_feature(x) : x
91
87
  Numo::Int32.cast(Numo::Liblinear.predict(xx, liblinear_params, @model))
92
88
  end
@@ -97,8 +93,8 @@ module Rumale
97
93
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the probailities.
98
94
  # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.
99
95
  def predict_proba(x)
100
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
101
- x = check_convert_sample_array(x)
96
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
97
+ x = Rumale::Validation.check_convert_sample_array(x)
102
98
  xx = fit_bias? ? expand_feature(x) : x
103
99
  Numo::Liblinear.predict_proba(xx, liblinear_params, @model)
104
100
  end
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'numo/libsvm'
4
- require 'rumale/base/base_estimator'
4
+ require 'rumale/base/estimator'
5
5
  require 'rumale/base/classifier'
6
+ require 'rumale/validation'
6
7
 
7
8
  module Rumale
8
9
  module SVM
@@ -12,9 +13,8 @@ module Rumale
12
13
  # estimator = Rumale::SVM::NuSVC.new(nu: 0.5, kernel: 'rbf', gamma: 10.0, random_seed: 1)
13
14
  # estimator.fit(training_samples, traininig_labels)
14
15
  # results = estimator.predict(testing_samples)
15
- class NuSVC
16
- include Base::BaseEstimator
17
- include Base::Classifier
16
+ class NuSVC < Rumale::Base::Estimator
17
+ include Rumale::Base::Classifier
18
18
 
19
19
  # Create a new classifier with Kernel Nu-Support Vector Classifier.
20
20
  #
@@ -31,10 +31,7 @@ module Rumale
31
31
  # @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
32
32
  def initialize(nu: 0.5, kernel: 'rbf', degree: 3, gamma: 1.0, coef0: 0.0,
33
33
  shrinking: true, probability: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil)
34
- check_params_numeric(nu: nu, degree: degree, gamma: gamma, coef0: coef0, cache_size: cache_size, tol: tol)
35
- check_params_string(kernel: kernel)
36
- check_params_boolean(shrinking: shrinking, probability: probability, verbose: verbose)
37
- check_params_numeric_or_nil(random_seed: random_seed)
34
+ super()
38
35
  @params = {}
39
36
  @params[:nu] = nu.to_f
40
37
  @params[:kernel] = kernel
@@ -56,9 +53,9 @@ module Rumale
56
53
  # @param y [Numo::Int32] (shape: [n_samples]) The labels to be used for fitting the model.
57
54
  # @return [NuSVC] The learned classifier itself.
58
55
  def fit(x, y)
59
- x = check_convert_sample_array(x)
60
- y = check_convert_label_array(y)
61
- check_sample_label_size(x, y)
56
+ x = Rumale::Validation.check_convert_sample_array(x)
57
+ y = Rumale::Validation.check_convert_label_array(y)
58
+ Rumale::Validation.check_sample_size(x, y)
62
59
  xx = precomputed_kernel? ? add_index_col(x) : x
63
60
  @model = Numo::Libsvm.train(xx, y, libsvm_params)
64
61
  self
@@ -70,8 +67,8 @@ module Rumale
70
67
  # If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
71
68
  # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
72
69
  def decision_function(x)
73
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
74
- x = check_convert_sample_array(x)
70
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
71
+ x = Rumale::Validation.check_convert_sample_array(x)
75
72
  xx = precomputed_kernel? ? add_index_col(x) : x
76
73
  Numo::Libsvm.decision_function(xx, libsvm_params, @model)
77
74
  end
@@ -82,8 +79,8 @@ module Rumale
82
79
  # If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
83
80
  # @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.
84
81
  def predict(x)
85
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
86
- x = check_convert_sample_array(x)
82
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
83
+ x = Rumale::Validation.check_convert_sample_array(x)
87
84
  xx = precomputed_kernel? ? add_index_col(x) : x
88
85
  Numo::Int32.cast(Numo::Libsvm.predict(xx, libsvm_params, @model))
89
86
  end
@@ -95,8 +92,8 @@ module Rumale
95
92
  # If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
96
93
  # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.
97
94
  def predict_proba(x)
98
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
99
- x = check_convert_sample_array(x)
95
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
96
+ x = Rumale::Validation.check_convert_sample_array(x)
100
97
  xx = precomputed_kernel? ? add_index_col(x) : x
101
98
  Numo::Libsvm.predict_proba(xx, libsvm_params, @model)
102
99
  end
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'numo/libsvm'
4
- require 'rumale/base/base_estimator'
4
+ require 'rumale/base/estimator'
5
5
  require 'rumale/base/regressor'
6
+ require 'rumale/validation'
6
7
 
7
8
  module Rumale
8
9
  module SVM
@@ -12,9 +13,8 @@ module Rumale
12
13
  # estimator = Rumale::SVM::NuSVR.new(nu: 0.5, kernel: 'rbf', gamma: 10.0, random_seed: 1)
13
14
  # estimator.fit(training_samples, traininig_target_values)
14
15
  # results = estimator.predict(testing_samples)
15
- class NuSVR
16
- include Base::BaseEstimator
17
- include Base::Regressor
16
+ class NuSVR < Rumale::Base::Estimator
17
+ include Rumale::Base::Regressor
18
18
 
19
19
  # Create a new regressor with Kernel Nu-Support Vector Regressor.
20
20
  #
@@ -30,10 +30,7 @@ module Rumale
30
30
  # @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
31
31
  def initialize(nu: 0.5, kernel: 'rbf', degree: 3, gamma: 1.0, coef0: 0.0,
32
32
  shrinking: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil)
33
- check_params_numeric(nu: nu, degree: degree, gamma: gamma, coef0: coef0, cache_size: cache_size, tol: tol)
34
- check_params_string(kernel: kernel)
35
- check_params_boolean(shrinking: shrinking, verbose: verbose)
36
- check_params_numeric_or_nil(random_seed: random_seed)
33
+ super()
37
34
  @params = {}
38
35
  @params[:nu] = nu.to_f
39
36
  @params[:kernel] = kernel
@@ -54,9 +51,9 @@ module Rumale
54
51
  # @param y [Numo::DFloat] (shape: [n_samples]) The target values to be used for fitting the model.
55
52
  # @return [NuSVR] The learned regressor itself.
56
53
  def fit(x, y)
57
- x = check_convert_sample_array(x)
58
- y = check_convert_tvalue_array(y)
59
- check_sample_tvalue_size(x, y)
54
+ x = Rumale::Validation.check_convert_sample_array(x)
55
+ y = Rumale::Validation.check_convert_target_value_array(y)
56
+ Rumale::Validation.check_sample_size(x, y)
60
57
  xx = precomputed_kernel? ? add_index_col(x) : x
61
58
  @model = Numo::Libsvm.train(xx, y, libsvm_params)
62
59
  self
@@ -68,8 +65,8 @@ module Rumale
68
65
  # If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
69
66
  # @return [Numo::DFloat] (shape: [n_samples]) Predicted value per sample.
70
67
  def predict(x)
71
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
72
- x = check_convert_sample_array(x)
68
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
69
+ x = Rumale::Validation.check_convert_sample_array(x)
73
70
  xx = precomputed_kernel? ? add_index_col(x) : x
74
71
  Numo::Libsvm.predict(xx, libsvm_params, @model)
75
72
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'numo/libsvm'
4
- require 'rumale/base/base_estimator'
4
+ require 'rumale/base/estimator'
5
5
  require 'rumale/validation'
6
6
 
7
7
  module Rumale
@@ -12,10 +12,7 @@ module Rumale
12
12
  # estimator = Rumale::SVM::OneClassSVM.new(nu: 0.5, kernel: 'rbf', gamma: 10.0, random_seed: 1)
13
13
  # estimator.fit(training_samples, traininig_labels)
14
14
  # results = estimator.predict(testing_samples)
15
- class OneClassSVM
16
- include Base::BaseEstimator
17
- include Validation
18
-
15
+ class OneClassSVM < Rumale::Base::Estimator
19
16
  # Create a new estimator with One-class Support Vector Machine.
20
17
  #
21
18
  # @param nu [Float] The regularization parameter. The interval of nu is (0, 1].
@@ -24,16 +21,14 @@ module Rumale
24
21
  # @param gamma [Float] The gamma parameter in rbf/poly/sigmoid kernel function.
25
22
  # @param coef0 [Float] The coefficient in poly/sigmoid kernel function.
26
23
  # @param shrinking [Boolean] The flag indicating whether to use the shrinking heuristics.
24
+ # @param probability [Boolean] The flag indicating whether to train the parameter for probability estimation.
27
25
  # @param cache_size [Float] The cache memory size in MB.
28
26
  # @param tol [Float] The tolerance of termination criterion.
29
27
  # @param verbose [Boolean] The flag indicating whether to output learning process message
30
28
  # @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
31
29
  def initialize(nu: 1.0, kernel: 'rbf', degree: 3, gamma: 1.0, coef0: 0.0,
32
- shrinking: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil)
33
- check_params_numeric(nu: nu, degree: degree, gamma: gamma, coef0: coef0, cache_size: cache_size, tol: tol)
34
- check_params_string(kernel: kernel)
35
- check_params_boolean(shrinking: shrinking, verbose: verbose)
36
- check_params_numeric_or_nil(random_seed: random_seed)
30
+ shrinking: true, probability: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil)
31
+ super()
37
32
  @params = {}
38
33
  @params[:nu] = nu.to_f
39
34
  @params[:kernel] = kernel
@@ -41,6 +36,7 @@ module Rumale
41
36
  @params[:gamma] = gamma.to_f
42
37
  @params[:coef0] = coef0.to_f
43
38
  @params[:shrinking] = shrinking
39
+ @params[:probability] = probability
44
40
  @params[:cache_size] = cache_size.to_f
45
41
  @params[:tol] = tol.to_f
46
42
  @params[:verbose] = verbose
@@ -54,7 +50,7 @@ module Rumale
54
50
  # If the kernel is 'precomputed', x must be a square distance matrix (shape: [n_samples, n_samples]).
55
51
  # @return [OneClassSVM] The learned estimator itself.
56
52
  def fit(x, _y = nil)
57
- x = check_convert_sample_array(x)
53
+ x = Rumale::Validation.check_convert_sample_array(x)
58
54
  dummy = Numo::DFloat.ones(x.shape[0])
59
55
  @model = Numo::Libsvm.train(x, dummy, libsvm_params)
60
56
  self
@@ -66,8 +62,8 @@ module Rumale
66
62
  # If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
67
63
  # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
68
64
  def decision_function(x)
69
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
70
- x = check_convert_sample_array(x)
65
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
66
+ x = Rumale::Validation.check_convert_sample_array(x)
71
67
  Numo::Libsvm.decision_function(x, libsvm_params, @model)
72
68
  end
73
69
 
@@ -77,11 +73,24 @@ module Rumale
77
73
  # If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
78
74
  # @return [Numo::Int32] (shape: [n_samples]) Predicted label per sample.
79
75
  def predict(x)
80
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
81
- x = check_convert_sample_array(x)
76
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
77
+ x = Rumale::Validation.check_convert_sample_array(x)
82
78
  Numo::Int32.cast(Numo::Libsvm.predict(x, libsvm_params, @model))
83
79
  end
84
80
 
81
+ # Predict class probability for samples.
82
+ # This method works correctly only if the probability parameter is true.
83
+ #
84
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the probailities.
85
+ # If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
86
+ # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.
87
+ def predict_proba(x)
88
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
89
+ raise "#{self.class.name}##{__method__} expects to be called after training the probablity parameters." unless trained_probs?
90
+ x = Rumale::Validation.check_convert_sample_array(x)
91
+ Numo::Libsvm.predict_proba(x, libsvm_params, @model)
92
+ end
93
+
85
94
  # Dump marshal data.
86
95
  # @return [Hash] The marshal data about SVC.
87
96
  def marshal_dump
@@ -150,6 +159,10 @@ module Rumale
150
159
  def trained?
151
160
  !@model.nil?
152
161
  end
162
+
163
+ def trained_probs?
164
+ @model[:prob_density_marks].is_a?(Numo::NArray)
165
+ end
153
166
  end
154
167
  end
155
168
  end
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'numo/libsvm'
4
- require 'rumale/base/base_estimator'
4
+ require 'rumale/base/estimator'
5
5
  require 'rumale/base/classifier'
6
+ require 'rumale/validation'
6
7
 
7
8
  module Rumale
8
9
  module SVM
@@ -12,9 +13,8 @@ module Rumale
12
13
  # estimator = Rumale::SVM::SVC.new(reg_param: 1.0, kernel: 'rbf', gamma: 10.0, random_seed: 1)
13
14
  # estimator.fit(training_samples, traininig_labels)
14
15
  # results = estimator.predict(testing_samples)
15
- class SVC
16
- include Base::BaseEstimator
17
- include Base::Classifier
16
+ class SVC < Rumale::Base::Estimator
17
+ include Rumale::Base::Classifier
18
18
 
19
19
  # Create a new classifier with Kernel C-Support Vector Classifier.
20
20
  #
@@ -31,10 +31,7 @@ module Rumale
31
31
  # @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
32
32
  def initialize(reg_param: 1.0, kernel: 'rbf', degree: 3, gamma: 1.0, coef0: 0.0,
33
33
  shrinking: true, probability: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil)
34
- check_params_numeric(reg_param: reg_param, degree: degree, gamma: gamma, coef0: coef0, cache_size: cache_size, tol: tol)
35
- check_params_string(kernel: kernel)
36
- check_params_boolean(shrinking: shrinking, probability: probability, verbose: verbose)
37
- check_params_numeric_or_nil(random_seed: random_seed)
34
+ super()
38
35
  @params = {}
39
36
  @params[:reg_param] = reg_param.to_f
40
37
  @params[:kernel] = kernel
@@ -56,9 +53,9 @@ module Rumale
56
53
  # @param y [Numo::Int32] (shape: [n_samples]) The labels to be used for fitting the model.
57
54
  # @return [SVC] The learned classifier itself.
58
55
  def fit(x, y)
59
- x = check_convert_sample_array(x)
60
- y = check_convert_label_array(y)
61
- check_sample_label_size(x, y)
56
+ x = Rumale::Validation.check_convert_sample_array(x)
57
+ y = Rumale::Validation.check_convert_label_array(y)
58
+ Rumale::Validation.check_sample_size(x, y)
62
59
  xx = precomputed_kernel? ? add_index_col(x) : x
63
60
  @model = Numo::Libsvm.train(xx, y, libsvm_params)
64
61
  self
@@ -70,8 +67,8 @@ module Rumale
70
67
  # If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
71
68
  # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
72
69
  def decision_function(x)
73
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
74
- x = check_convert_sample_array(x)
70
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
71
+ x = Rumale::Validation.check_convert_sample_array(x)
75
72
  xx = precomputed_kernel? ? add_index_col(x) : x
76
73
  Numo::Libsvm.decision_function(xx, libsvm_params, @model)
77
74
  end
@@ -82,8 +79,8 @@ module Rumale
82
79
  # If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
83
80
  # @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.
84
81
  def predict(x)
85
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
86
- x = check_convert_sample_array(x)
82
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
83
+ x = Rumale::Validation.check_convert_sample_array(x)
87
84
  xx = precomputed_kernel? ? add_index_col(x) : x
88
85
  Numo::Int32.cast(Numo::Libsvm.predict(xx, libsvm_params, @model))
89
86
  end
@@ -95,8 +92,8 @@ module Rumale
95
92
  # If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
96
93
  # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.
97
94
  def predict_proba(x)
98
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
99
- x = check_convert_sample_array(x)
95
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
96
+ x = Rumale::Validation.check_convert_sample_array(x)
100
97
  xx = precomputed_kernel? ? add_index_col(x) : x
101
98
  Numo::Libsvm.predict_proba(xx, libsvm_params, @model)
102
99
  end
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'numo/libsvm'
4
- require 'rumale/base/base_estimator'
4
+ require 'rumale/base/estimator'
5
5
  require 'rumale/base/regressor'
6
+ require 'rumale/validation'
6
7
 
7
8
  module Rumale
8
9
  module SVM
@@ -12,9 +13,8 @@ module Rumale
12
13
  # estimator = Rumale::SVM::SVR.new(reg_param: 1.0, kernel: 'rbf', gamma: 10.0, random_seed: 1)
13
14
  # estimator.fit(training_samples, traininig_target_values)
14
15
  # results = estimator.predict(testing_samples)
15
- class SVR
16
- include Base::BaseEstimator
17
- include Base::Regressor
16
+ class SVR < Rumale::Base::Estimator
17
+ include Rumale::Base::Regressor
18
18
 
19
19
  # Create a new regressor with Kernel Epsilon-Support Vector Regressor.
20
20
  #
@@ -31,11 +31,7 @@ module Rumale
31
31
  # @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
32
32
  def initialize(reg_param: 1.0, epsilon: 0.1, kernel: 'rbf', degree: 3, gamma: 1.0, coef0: 0.0,
33
33
  shrinking: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil)
34
- check_params_numeric(reg_param: reg_param, degree: degree, epsilon: epsilon, gamma: gamma, coef0: coef0,
35
- cache_size: cache_size, tol: tol)
36
- check_params_string(kernel: kernel)
37
- check_params_boolean(shrinking: shrinking, verbose: verbose)
38
- check_params_numeric_or_nil(random_seed: random_seed)
34
+ super()
39
35
  @params = {}
40
36
  @params[:reg_param] = reg_param.to_f
41
37
  @params[:epsilon] = epsilon.to_f
@@ -57,9 +53,9 @@ module Rumale
57
53
  # @param y [Numo::DFloat] (shape: [n_samples]) The target values to be used for fitting the model.
58
54
  # @return [SVR] The learned regressor itself.
59
55
  def fit(x, y)
60
- x = check_convert_sample_array(x)
61
- y = check_convert_tvalue_array(y)
62
- check_sample_tvalue_size(x, y)
56
+ x = Rumale::Validation.check_convert_sample_array(x)
57
+ y = Rumale::Validation.check_convert_target_value_array(y)
58
+ Rumale::Validation.check_sample_size(x, y)
63
59
  xx = precomputed_kernel? ? add_index_col(x) : x
64
60
  @model = Numo::Libsvm.train(xx, y, libsvm_params)
65
61
  self
@@ -71,8 +67,8 @@ module Rumale
71
67
  # If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
72
68
  # @return [Numo::DFloat] (shape: [n_samples]) Predicted value per sample.
73
69
  def predict(x)
74
- raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
75
- x = check_convert_sample_array(x)
70
+ raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained?
71
+ x = Rumale::Validation.check_convert_sample_array(x)
76
72
  xx = precomputed_kernel? ? add_index_col(x) : x
77
73
  Numo::Libsvm.predict(xx, libsvm_params, @model)
78
74
  end
@@ -5,6 +5,6 @@ module Rumale
5
5
  # This module consists of Rumale interfaces for suppor vector machine algorithms with LIBSVM and LIBLINEAR.
6
6
  module SVM
7
7
  # The version of Rumale::SVM you are using.
8
- VERSION = '0.6.0'
8
+ VERSION = '0.8.0'
9
9
  end
10
10
  end
@@ -10,6 +10,7 @@ module Rumale
10
10
  def fit: (Numo::DFloat x, ?untyped? _y) -> OneClassSVM
11
11
  def decision_function: (Numo::DFloat x) -> Numo::DFloat
12
12
  def predict: (Numo::DFloat x) -> Numo::Int32
13
+ def predict_proba: (Numo::DFloat x) -> Numo::DFloat
13
14
  def marshal_dump: () -> { params: Hash[Symbol, untyped], model: untyped }
14
15
  def marshal_load: (Hash[Symbol, untyped] obj) -> void
15
16
  def support: () -> Numo::Int32
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rumale-svm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshoku
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-15 00:00:00.000000000 Z
11
+ date: 2023-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-liblinear
@@ -30,22 +30,19 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.0'
33
+ version: '2.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.0'
40
+ version: '2.1'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rumale
42
+ name: rumale-core
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '0.14'
48
- - - "<"
49
46
  - !ruby/object:Gem::Version
50
47
  version: '0.24'
51
48
  type: :runtime
@@ -53,9 +50,6 @@ dependencies:
53
50
  version_requirements: !ruby/object:Gem::Requirement
54
51
  requirements:
55
52
  - - "~>"
56
- - !ruby/object:Gem::Version
57
- version: '0.14'
58
- - - "<"
59
53
  - !ruby/object:Gem::Version
60
54
  version: '0.24'
61
55
  description: 'Rumale::SVM provides support vector machine algorithms of LIBSVM and
@@ -116,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
110
  - !ruby/object:Gem::Version
117
111
  version: '0'
118
112
  requirements: []
119
- rubygems_version: 3.2.33
113
+ rubygems_version: 3.3.26
120
114
  signing_key:
121
115
  specification_version: 4
122
116
  summary: Rumale::SVM provides support vector machine algorithms of LIBSVM and LIBLINEAR