rumale 0.22.0 → 0.22.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.coveralls.yml +1 -0
  3. data/.github/workflows/build.yml +6 -3
  4. data/.github/workflows/coverage.yml +28 -0
  5. data/.gitignore +1 -0
  6. data/.rubocop.yml +1 -0
  7. data/CHANGELOG.md +35 -0
  8. data/Gemfile +6 -4
  9. data/LICENSE.txt +1 -1
  10. data/README.md +56 -19
  11. data/ext/rumale/tree.c +24 -12
  12. data/lib/rumale.rb +8 -0
  13. data/lib/rumale/base/base_estimator.rb +5 -3
  14. data/lib/rumale/dataset.rb +7 -3
  15. data/lib/rumale/decomposition/pca.rb +1 -1
  16. data/lib/rumale/ensemble/stacking_classifier.rb +215 -0
  17. data/lib/rumale/ensemble/stacking_regressor.rb +163 -0
  18. data/lib/rumale/ensemble/voting_classifier.rb +126 -0
  19. data/lib/rumale/ensemble/voting_regressor.rb +82 -0
  20. data/lib/rumale/feature_extraction/feature_hasher.rb +1 -1
  21. data/lib/rumale/feature_extraction/hash_vectorizer.rb +1 -1
  22. data/lib/rumale/kernel_approximation/nystroem.rb +29 -9
  23. data/lib/rumale/kernel_machine/kernel_ridge_classifier.rb +92 -0
  24. data/lib/rumale/kernel_machine/kernel_svc.rb +4 -3
  25. data/lib/rumale/linear_model/elastic_net.rb +1 -1
  26. data/lib/rumale/linear_model/lasso.rb +1 -1
  27. data/lib/rumale/linear_model/linear_regression.rb +63 -34
  28. data/lib/rumale/linear_model/logistic_regression.rb +1 -1
  29. data/lib/rumale/linear_model/nnls.rb +137 -0
  30. data/lib/rumale/linear_model/ridge.rb +70 -33
  31. data/lib/rumale/linear_model/svc.rb +4 -3
  32. data/lib/rumale/linear_model/svr.rb +4 -3
  33. data/lib/rumale/metric_learning/mlkr.rb +161 -0
  34. data/lib/rumale/metric_learning/neighbourhood_component_analysis.rb +7 -4
  35. data/lib/rumale/pairwise_metric.rb +1 -1
  36. data/lib/rumale/preprocessing/kernel_calculator.rb +92 -0
  37. data/lib/rumale/validation.rb +13 -1
  38. data/lib/rumale/version.rb +1 -1
  39. data/rumale.gemspec +1 -1
  40. metadata +14 -4
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'rumale/base/base_estimator'
4
4
  require 'rumale/base/transformer'
5
+ require 'rumale/utils'
6
+ require 'rumale/pairwise_metric'
5
7
  require 'lbfgsb'
6
8
 
7
9
  module Rumale
@@ -146,10 +148,11 @@ module Rumale
146
148
  mask_mat = y.expand_dims(1).eq(y)
147
149
  masked_prob_mat = prob_mat * mask_mat
148
150
  loss = n_samples - masked_prob_mat.sum
149
- weighted_prob_mat = masked_prob_mat - prob_mat * masked_prob_mat.sum(1).expand_dims(1)
150
- weighted_prob_mat += weighted_prob_mat.transpose
151
- weighted_prob_mat[weighted_prob_mat.diag_indices] = -weighted_prob_mat.sum(0)
152
- gradient = -2 * z.transpose.dot(weighted_prob_mat).dot(x)
151
+ sum_probs = masked_prob_mat.sum(1)
152
+ weight_mat = (sum_probs.expand_dims(1) * prob_mat - masked_prob_mat)
153
+ weight_mat += weight_mat.transpose
154
+ weight_mat = weight_mat.sum(0).diag - weight_mat
155
+ gradient = -2 * z.transpose.dot(weight_mat).dot(x)
153
156
  [loss, gradient.flatten.dup]
154
157
  end
155
158
 
@@ -123,7 +123,7 @@ module Rumale
123
123
  # @param gamma [Float] The parameter of polynomial kernel, if nil it is 1 / n_features.
124
124
  # @param coef [Integer] The parameter of polynomial kernel.
125
125
  # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)
126
- def polynomial_kernel(x, y = nil, degree = 3, gamma = nil, coef = 1)
126
+ def polynomial_kernel(x, y = nil, degree = 3, gamma = nil, coef = 1) # rubocop:disable Metrics/ParameterLists
127
127
  y = x if y.nil?
128
128
  gamma ||= 1.0 / x.shape[1]
129
129
  x = Rumale::Validation.check_convert_sample_array(x)
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rumale/base/base_estimator'
4
+ require 'rumale/base/transformer'
5
+ require 'rumale/pairwise_metric'
6
+
7
+ module Rumale
8
+ module Preprocessing
9
+ # KernelCalculator is a class that calculates the kernel matrix with training data.
10
+ #
11
+ # @example
12
+ # transformer = Rumale::Preprocessing::KernelCalculator.new(kernel: 'rbf', gamma: 0.5)
13
+ # regressor = Rumale::KernelMachine::KernelRidge.new
14
+ # pipeline = Rumale::Pipeline::Pipeline.new(
15
+ # steps: { trs: transfomer, est: regressor }
16
+ # )
17
+ # pipeline.fit(x_train, y_train)
18
+ # results = pipeline.predict(x_test)
19
+ class KernelCalculator
20
+ include Base::BaseEstimator
21
+ include Base::Transformer
22
+
23
+ # Returns the training data for calculating kernel matrix.
24
+ # @return [Numo::DFloat] (shape: n_components, n_features)
25
+ attr_reader :components
26
+
27
+ # Create a new transformer that transforms feature vectors into a kernel matrix.
28
+ #
29
+ # @param kernel [String] The type of kernel function ('rbf', 'linear', 'poly', and 'sigmoid').
30
+ # @param gamma [Float] The gamma parameter in rbf/poly/sigmoid kernel function.
31
+ # @param degree [Integer] The degree parameter in polynomial kernel function.
32
+ # @param coef [Float] The coefficient in poly/sigmoid kernel function.
33
+ def initialize(kernel: 'rbf', gamma: 1, degree: 3, coef: 1)
34
+ check_params_string(kernel: kernel)
35
+ check_params_numeric(gamma: gamma, coef: coef, degree: degree)
36
+ @params = {}
37
+ @params[:kernel] = kernel
38
+ @params[:gamma] = gamma
39
+ @params[:degree] = degree
40
+ @params[:coef] = coef
41
+ @components = nil
42
+ end
43
+
44
+ # Fit the model with given training data.
45
+ #
46
+ # @overload fit(x) -> KernelCalculator
47
+ # @param x [Numo::NArray] (shape: [n_samples, n_features]) The training data to be used for calculating kernel matrix.
48
+ # @return [KernelCalculator] The learned transformer itself.
49
+ def fit(x, _y = nil)
50
+ x = check_convert_sample_array(x)
51
+ @components = x.dup
52
+ self
53
+ end
54
+
55
+ # Fit the model with training data, and then transform them with the learned model.
56
+ #
57
+ # @overload fit_transform(x) -> Numo::DFloat
58
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for calculating kernel matrix.
59
+ # @return [Numo::DFloat] (shape: [n_samples, n_samples]) The calculated kernel matrix.
60
+ def fit_transform(x, y = nil)
61
+ x = check_convert_sample_array(x)
62
+ fit(x, y).transform(x)
63
+ end
64
+
65
+ # Transform the given data with the learned model.
66
+ #
67
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The data to be used for calculating kernel matrix with the training data.
68
+ # @return [Numo::DFloat] (shape: [n_samples, n_components]) The calculated kernel matrix.
69
+ def transform(x)
70
+ x = check_convert_sample_array(x)
71
+ kernel_mat(x, @components)
72
+ end
73
+
74
+ private
75
+
76
+ def kernel_mat(x, y)
77
+ case @params[:kernel]
78
+ when 'rbf'
79
+ Rumale::PairwiseMetric.rbf_kernel(x, y, @params[:gamma])
80
+ when 'poly'
81
+ Rumale::PairwiseMetric.polynomial_kernel(x, y, @params[:degree], @params[:gamma], @params[:coef])
82
+ when 'sigmoid'
83
+ Rumale::PairwiseMetric.sigmoid_kernel(x, y, @params[:gamma], @params[:coef])
84
+ when 'linear'
85
+ Rumale::PairwiseMetric.linear_kernel(x, y)
86
+ else
87
+ raise ArgumentError, "Expect kernel parameter to be given 'rbf', 'linear', 'poly', or 'sigmoid'."
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -27,6 +27,7 @@ module Rumale
27
27
  y
28
28
  end
29
29
 
30
+ # @deprecated Use check_convert_sample_array instead of this method.
30
31
  # @!visibility private
31
32
  def check_sample_array(x)
32
33
  raise TypeError, 'Expect class of sample matrix to be Numo::DFloat' unless x.is_a?(Numo::DFloat)
@@ -35,6 +36,7 @@ module Rumale
35
36
  nil
36
37
  end
37
38
 
39
+ # @deprecated Use check_convert_label_array instead of this method.
38
40
  # @!visibility private
39
41
  def check_label_array(y)
40
42
  raise TypeError, 'Expect class of label vector to be Numo::Int32' unless y.is_a?(Numo::Int32)
@@ -43,6 +45,7 @@ module Rumale
43
45
  nil
44
46
  end
45
47
 
48
+ # @deprecated Use check_convert_tvalue_array instead of this method.
46
49
  # @!visibility private
47
50
  def check_tvalue_array(y)
48
51
  raise TypeError, 'Expect class of target value vector to be Numo::DFloat' unless y.is_a?(Numo::DFloat)
@@ -64,52 +67,61 @@ module Rumale
64
67
  nil
65
68
  end
66
69
 
70
+ # TODO: Better to replace with RBS in the future.
67
71
  # @!visibility private
68
72
  def check_params_type(type, params = {})
69
73
  params.each { |k, v| raise TypeError, "Expect class of #{k} to be #{type}" unless v.is_a?(type) }
70
74
  nil
71
75
  end
72
76
 
77
+ # TODO: Better to replace with RBS in the future.
73
78
  # @!visibility private
74
79
  def check_params_type_or_nil(type, params = {})
75
80
  params.each { |k, v| raise TypeError, "Expect class of #{k} to be #{type} or nil" unless v.is_a?(type) || v.is_a?(NilClass) }
76
81
  nil
77
82
  end
78
83
 
84
+ # TODO: Better to replace with RBS in the future.
79
85
  # @!visibility private
80
86
  def check_params_numeric(params = {})
81
87
  check_params_type(Numeric, params)
82
88
  end
83
89
 
90
+ # TODO: Better to replace with RBS in the future.
84
91
  # @!visibility private
85
92
  def check_params_numeric_or_nil(params = {})
86
93
  check_params_type_or_nil(Numeric, params)
87
94
  end
88
95
 
96
+ # @deprecated Use check_params_numeric instead of this method.
89
97
  # @!visibility private
90
98
  def check_params_float(params = {})
91
99
  check_params_type(Float, params)
92
100
  end
93
101
 
102
+ # @deprecated Use check_params_numeric instead of this method.
94
103
  # @!visibility private
95
104
  def check_params_integer(params = {})
96
105
  check_params_type(Integer, params)
97
106
  end
98
107
 
108
+ # TODO: Better to replace with RBS in the future.
99
109
  # @!visibility private
100
110
  def check_params_string(params = {})
101
111
  check_params_type(String, params)
102
112
  end
103
113
 
114
+ # TODO: Better to replace with RBS in the future.
104
115
  # @!visibility private
105
116
  def check_params_boolean(params = {})
106
117
  params.each { |k, v| raise TypeError, "Expect class of #{k} to be Boolean" unless v.is_a?(FalseClass) || v.is_a?(TrueClass) }
107
118
  nil
108
119
  end
109
120
 
121
+ # TODO: Better to replace with RBS in the future.
110
122
  # @!visibility private
111
123
  def check_params_positive(params = {})
112
- params.reject { |_, v| v.nil? }.each { |k, v| raise ArgumentError, "Expect #{k} to be positive value" if v.negative? }
124
+ params.compact.each { |k, v| raise ArgumentError, "Expect #{k} to be positive value" if v.negative? }
113
125
  nil
114
126
  end
115
127
  end
@@ -3,5 +3,5 @@
3
3
  # Rumale is a machine learning library in Ruby.
4
4
  module Rumale
5
5
  # The version of Rumale you are using.
6
- VERSION = '0.22.0'
6
+ VERSION = '0.22.5'
7
7
  end
data/rumale.gemspec CHANGED
@@ -38,7 +38,7 @@ Gem::Specification.new do |spec|
38
38
 
39
39
  spec.metadata = {
40
40
  'homepage_uri' => 'https://github.com/yoshoku/rumale',
41
- 'changelog_uri' => 'https://github.com/yoshoku/rumale/blob/master/CHANGELOG.md',
41
+ 'changelog_uri' => 'https://github.com/yoshoku/rumale/blob/main/CHANGELOG.md',
42
42
  'source_code_uri' => 'https://github.com/yoshoku/rumale',
43
43
  'documentation_uri' => 'https://yoshoku.github.io/rumale/doc/',
44
44
  'bug_tracker_uri' => 'https://github.com/yoshoku/rumale/issues'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rumale
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.0
4
+ version: 0.22.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshoku
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-22 00:00:00.000000000 Z
11
+ date: 2021-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-narray
@@ -57,7 +57,9 @@ extensions:
57
57
  - ext/rumale/extconf.rb
58
58
  extra_rdoc_files: []
59
59
  files:
60
+ - ".coveralls.yml"
60
61
  - ".github/workflows/build.yml"
62
+ - ".github/workflows/coverage.yml"
61
63
  - ".gitignore"
62
64
  - ".rspec"
63
65
  - ".rubocop.yml"
@@ -104,6 +106,10 @@ files:
104
106
  - lib/rumale/ensemble/gradient_boosting_regressor.rb
105
107
  - lib/rumale/ensemble/random_forest_classifier.rb
106
108
  - lib/rumale/ensemble/random_forest_regressor.rb
109
+ - lib/rumale/ensemble/stacking_classifier.rb
110
+ - lib/rumale/ensemble/stacking_regressor.rb
111
+ - lib/rumale/ensemble/voting_classifier.rb
112
+ - lib/rumale/ensemble/voting_regressor.rb
107
113
  - lib/rumale/evaluation_measure/accuracy.rb
108
114
  - lib/rumale/evaluation_measure/adjusted_rand_score.rb
109
115
  - lib/rumale/evaluation_measure/calinski_harabasz_score.rb
@@ -133,18 +139,21 @@ files:
133
139
  - lib/rumale/kernel_machine/kernel_fda.rb
134
140
  - lib/rumale/kernel_machine/kernel_pca.rb
135
141
  - lib/rumale/kernel_machine/kernel_ridge.rb
142
+ - lib/rumale/kernel_machine/kernel_ridge_classifier.rb
136
143
  - lib/rumale/kernel_machine/kernel_svc.rb
137
144
  - lib/rumale/linear_model/base_sgd.rb
138
145
  - lib/rumale/linear_model/elastic_net.rb
139
146
  - lib/rumale/linear_model/lasso.rb
140
147
  - lib/rumale/linear_model/linear_regression.rb
141
148
  - lib/rumale/linear_model/logistic_regression.rb
149
+ - lib/rumale/linear_model/nnls.rb
142
150
  - lib/rumale/linear_model/ridge.rb
143
151
  - lib/rumale/linear_model/svc.rb
144
152
  - lib/rumale/linear_model/svr.rb
145
153
  - lib/rumale/manifold/mds.rb
146
154
  - lib/rumale/manifold/tsne.rb
147
155
  - lib/rumale/metric_learning/fisher_discriminant_analysis.rb
156
+ - lib/rumale/metric_learning/mlkr.rb
148
157
  - lib/rumale/metric_learning/neighbourhood_component_analysis.rb
149
158
  - lib/rumale/model_selection/cross_validation.rb
150
159
  - lib/rumale/model_selection/function.rb
@@ -175,6 +184,7 @@ files:
175
184
  - lib/rumale/pipeline/pipeline.rb
176
185
  - lib/rumale/preprocessing/bin_discretizer.rb
177
186
  - lib/rumale/preprocessing/binarizer.rb
187
+ - lib/rumale/preprocessing/kernel_calculator.rb
178
188
  - lib/rumale/preprocessing/l1_normalizer.rb
179
189
  - lib/rumale/preprocessing/l2_normalizer.rb
180
190
  - lib/rumale/preprocessing/label_binarizer.rb
@@ -204,7 +214,7 @@ licenses:
204
214
  - BSD-2-Clause
205
215
  metadata:
206
216
  homepage_uri: https://github.com/yoshoku/rumale
207
- changelog_uri: https://github.com/yoshoku/rumale/blob/master/CHANGELOG.md
217
+ changelog_uri: https://github.com/yoshoku/rumale/blob/main/CHANGELOG.md
208
218
  source_code_uri: https://github.com/yoshoku/rumale
209
219
  documentation_uri: https://yoshoku.github.io/rumale/doc/
210
220
  bug_tracker_uri: https://github.com/yoshoku/rumale/issues
@@ -223,7 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
223
233
  - !ruby/object:Gem::Version
224
234
  version: '0'
225
235
  requirements: []
226
- rubygems_version: 3.1.4
236
+ rubygems_version: 3.2.7
227
237
  signing_key:
228
238
  specification_version: 4
229
239
  summary: Rumale is a machine learning library in Ruby. Rumale provides machine learning