rumale-svm 0.7.0 → 0.8.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 +4 -1
- data/README.md +0 -1
- data/lib/rumale/svm/linear_one_class_svm.rb +8 -13
- data/lib/rumale/svm/linear_svc.rb +14 -17
- data/lib/rumale/svm/linear_svr.rb +10 -13
- data/lib/rumale/svm/logistic_regression.rb +13 -17
- data/lib/rumale/svm/nu_svc.rb +14 -17
- data/lib/rumale/svm/nu_svr.rb +10 -13
- data/lib/rumale/svm/one_class_svm.rb +11 -17
- data/lib/rumale/svm/svc.rb +14 -17
- data/lib/rumale/svm/svr.rb +10 -14
- data/lib/rumale/svm/version.rb +1 -1
- metadata +4 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5611ab7bee24d673f0803b5679532326389a9a485dd5beafeb9d448ab5538363
|
4
|
+
data.tar.gz: 033024146647d6ba5e4acf08eb876dc35e0fe25f141555891dec77e84cd6572b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b70772fd9480c0de3f28d477bdf35ccae3260f9a9d1156423cac8654caebbc1891a9182e1ee3a4b3b7add8ce885ac06c2e57e8d3ca46a71e9d7de99d46d1c33d
|
7
|
+
data.tar.gz: a38ccc2f952bb1e3fe5c4906e9225bb2d6c5c938b93aa6d08a8b66a2b20f4e94b0d96fe08c86f489050051eb149cdf68b74826b6a5f7439fbab5ee47de2b67e6
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# 0.8.0
|
2
|
+
- Refactor to support the new Rumale API.
|
3
|
+
|
1
4
|
# 0.7.0
|
2
5
|
- Support for probabilistic outputs with Rumale::SVM::OneClassSVM.
|
3
6
|
- Update numo-libsvm depedency to v2.1 or higher.
|
@@ -6,7 +9,7 @@
|
|
6
9
|
- Update numo-libsvm and numo-liblinear depedency to v2.0 or higher.
|
7
10
|
|
8
11
|
# 0.5.1
|
9
|
-
-
|
12
|
+
- Refactor specs and config files.
|
10
13
|
|
11
14
|
# 0.5.0
|
12
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/
|
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
|
-
|
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}
|
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}
|
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/
|
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::
|
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
|
-
|
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
|
-
|
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}
|
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}
|
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}
|
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/
|
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::
|
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
|
-
|
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 =
|
67
|
-
|
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}
|
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/
|
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::
|
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
|
-
|
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
|
-
|
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}
|
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}
|
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}
|
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
|
data/lib/rumale/svm/nu_svc.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'numo/libsvm'
|
4
|
-
require 'rumale/base/
|
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::
|
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
|
-
|
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
|
-
|
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}
|
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}
|
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}
|
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
|
data/lib/rumale/svm/nu_svr.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'numo/libsvm'
|
4
|
-
require 'rumale/base/
|
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::
|
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
|
-
|
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 =
|
59
|
-
|
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}
|
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/
|
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].
|
@@ -31,10 +28,7 @@ module Rumale
|
|
31
28
|
# @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
|
32
29
|
def initialize(nu: 1.0, kernel: 'rbf', degree: 3, gamma: 1.0, coef0: 0.0,
|
33
30
|
shrinking: true, probability: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil)
|
34
|
-
|
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)
|
31
|
+
super()
|
38
32
|
@params = {}
|
39
33
|
@params[:nu] = nu.to_f
|
40
34
|
@params[:kernel] = kernel
|
@@ -56,7 +50,7 @@ module Rumale
|
|
56
50
|
# If the kernel is 'precomputed', x must be a square distance matrix (shape: [n_samples, n_samples]).
|
57
51
|
# @return [OneClassSVM] The learned estimator itself.
|
58
52
|
def fit(x, _y = nil)
|
59
|
-
x = check_convert_sample_array(x)
|
53
|
+
x = Rumale::Validation.check_convert_sample_array(x)
|
60
54
|
dummy = Numo::DFloat.ones(x.shape[0])
|
61
55
|
@model = Numo::Libsvm.train(x, dummy, libsvm_params)
|
62
56
|
self
|
@@ -68,8 +62,8 @@ module Rumale
|
|
68
62
|
# If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
|
69
63
|
# @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
|
70
64
|
def decision_function(x)
|
71
|
-
raise "#{self.class.name}
|
72
|
-
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)
|
73
67
|
Numo::Libsvm.decision_function(x, libsvm_params, @model)
|
74
68
|
end
|
75
69
|
|
@@ -79,8 +73,8 @@ module Rumale
|
|
79
73
|
# If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
|
80
74
|
# @return [Numo::Int32] (shape: [n_samples]) Predicted label per sample.
|
81
75
|
def predict(x)
|
82
|
-
raise "#{self.class.name}
|
83
|
-
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)
|
84
78
|
Numo::Int32.cast(Numo::Libsvm.predict(x, libsvm_params, @model))
|
85
79
|
end
|
86
80
|
|
@@ -91,9 +85,9 @@ module Rumale
|
|
91
85
|
# If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
|
92
86
|
# @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.
|
93
87
|
def predict_proba(x)
|
94
|
-
raise "#{self.class.name}
|
95
|
-
raise "#{self.class.name}
|
96
|
-
x = check_convert_sample_array(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)
|
97
91
|
Numo::Libsvm.predict_proba(x, libsvm_params, @model)
|
98
92
|
end
|
99
93
|
|
data/lib/rumale/svm/svc.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'numo/libsvm'
|
4
|
-
require 'rumale/base/
|
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::
|
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
|
-
|
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
|
-
|
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}
|
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}
|
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}
|
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
|
data/lib/rumale/svm/svr.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'numo/libsvm'
|
4
|
-
require 'rumale/base/
|
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::
|
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
|
-
|
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 =
|
62
|
-
|
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}
|
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
|
data/lib/rumale/svm/version.rb
CHANGED
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.
|
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:
|
11
|
+
date: 2023-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: numo-liblinear
|
@@ -39,13 +39,10 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
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.3.
|
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
|