rumale 0.23.3 → 0.24.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +5 -1
  3. data/README.md +3 -288
  4. data/lib/rumale/version.rb +1 -1
  5. data/lib/rumale.rb +20 -131
  6. metadata +252 -150
  7. data/CHANGELOG.md +0 -643
  8. data/CODE_OF_CONDUCT.md +0 -74
  9. data/ext/rumale/extconf.rb +0 -37
  10. data/ext/rumale/rumaleext.c +0 -545
  11. data/ext/rumale/rumaleext.h +0 -12
  12. data/lib/rumale/base/base_estimator.rb +0 -49
  13. data/lib/rumale/base/classifier.rb +0 -36
  14. data/lib/rumale/base/cluster_analyzer.rb +0 -31
  15. data/lib/rumale/base/evaluator.rb +0 -17
  16. data/lib/rumale/base/regressor.rb +0 -36
  17. data/lib/rumale/base/splitter.rb +0 -21
  18. data/lib/rumale/base/transformer.rb +0 -22
  19. data/lib/rumale/clustering/dbscan.rb +0 -123
  20. data/lib/rumale/clustering/gaussian_mixture.rb +0 -218
  21. data/lib/rumale/clustering/hdbscan.rb +0 -291
  22. data/lib/rumale/clustering/k_means.rb +0 -122
  23. data/lib/rumale/clustering/k_medoids.rb +0 -141
  24. data/lib/rumale/clustering/mini_batch_k_means.rb +0 -139
  25. data/lib/rumale/clustering/power_iteration.rb +0 -127
  26. data/lib/rumale/clustering/single_linkage.rb +0 -203
  27. data/lib/rumale/clustering/snn.rb +0 -76
  28. data/lib/rumale/clustering/spectral_clustering.rb +0 -115
  29. data/lib/rumale/dataset.rb +0 -246
  30. data/lib/rumale/decomposition/factor_analysis.rb +0 -150
  31. data/lib/rumale/decomposition/fast_ica.rb +0 -188
  32. data/lib/rumale/decomposition/nmf.rb +0 -124
  33. data/lib/rumale/decomposition/pca.rb +0 -159
  34. data/lib/rumale/ensemble/ada_boost_classifier.rb +0 -179
  35. data/lib/rumale/ensemble/ada_boost_regressor.rb +0 -160
  36. data/lib/rumale/ensemble/extra_trees_classifier.rb +0 -139
  37. data/lib/rumale/ensemble/extra_trees_regressor.rb +0 -125
  38. data/lib/rumale/ensemble/gradient_boosting_classifier.rb +0 -306
  39. data/lib/rumale/ensemble/gradient_boosting_regressor.rb +0 -237
  40. data/lib/rumale/ensemble/random_forest_classifier.rb +0 -189
  41. data/lib/rumale/ensemble/random_forest_regressor.rb +0 -153
  42. data/lib/rumale/ensemble/stacking_classifier.rb +0 -215
  43. data/lib/rumale/ensemble/stacking_regressor.rb +0 -163
  44. data/lib/rumale/ensemble/voting_classifier.rb +0 -126
  45. data/lib/rumale/ensemble/voting_regressor.rb +0 -82
  46. data/lib/rumale/evaluation_measure/accuracy.rb +0 -29
  47. data/lib/rumale/evaluation_measure/adjusted_rand_score.rb +0 -74
  48. data/lib/rumale/evaluation_measure/calinski_harabasz_score.rb +0 -56
  49. data/lib/rumale/evaluation_measure/davies_bouldin_score.rb +0 -53
  50. data/lib/rumale/evaluation_measure/explained_variance_score.rb +0 -39
  51. data/lib/rumale/evaluation_measure/f_score.rb +0 -50
  52. data/lib/rumale/evaluation_measure/function.rb +0 -147
  53. data/lib/rumale/evaluation_measure/log_loss.rb +0 -45
  54. data/lib/rumale/evaluation_measure/mean_absolute_error.rb +0 -29
  55. data/lib/rumale/evaluation_measure/mean_squared_error.rb +0 -29
  56. data/lib/rumale/evaluation_measure/mean_squared_log_error.rb +0 -29
  57. data/lib/rumale/evaluation_measure/median_absolute_error.rb +0 -30
  58. data/lib/rumale/evaluation_measure/mutual_information.rb +0 -49
  59. data/lib/rumale/evaluation_measure/normalized_mutual_information.rb +0 -53
  60. data/lib/rumale/evaluation_measure/precision.rb +0 -50
  61. data/lib/rumale/evaluation_measure/precision_recall.rb +0 -96
  62. data/lib/rumale/evaluation_measure/purity.rb +0 -40
  63. data/lib/rumale/evaluation_measure/r2_score.rb +0 -43
  64. data/lib/rumale/evaluation_measure/recall.rb +0 -50
  65. data/lib/rumale/evaluation_measure/roc_auc.rb +0 -130
  66. data/lib/rumale/evaluation_measure/silhouette_score.rb +0 -82
  67. data/lib/rumale/feature_extraction/feature_hasher.rb +0 -110
  68. data/lib/rumale/feature_extraction/hash_vectorizer.rb +0 -155
  69. data/lib/rumale/feature_extraction/tfidf_transformer.rb +0 -113
  70. data/lib/rumale/kernel_approximation/nystroem.rb +0 -126
  71. data/lib/rumale/kernel_approximation/rbf.rb +0 -102
  72. data/lib/rumale/kernel_machine/kernel_fda.rb +0 -120
  73. data/lib/rumale/kernel_machine/kernel_pca.rb +0 -97
  74. data/lib/rumale/kernel_machine/kernel_ridge.rb +0 -82
  75. data/lib/rumale/kernel_machine/kernel_ridge_classifier.rb +0 -92
  76. data/lib/rumale/kernel_machine/kernel_svc.rb +0 -193
  77. data/lib/rumale/linear_model/base_sgd.rb +0 -285
  78. data/lib/rumale/linear_model/elastic_net.rb +0 -119
  79. data/lib/rumale/linear_model/lasso.rb +0 -115
  80. data/lib/rumale/linear_model/linear_regression.rb +0 -201
  81. data/lib/rumale/linear_model/logistic_regression.rb +0 -275
  82. data/lib/rumale/linear_model/nnls.rb +0 -137
  83. data/lib/rumale/linear_model/ridge.rb +0 -209
  84. data/lib/rumale/linear_model/svc.rb +0 -213
  85. data/lib/rumale/linear_model/svr.rb +0 -132
  86. data/lib/rumale/manifold/mds.rb +0 -155
  87. data/lib/rumale/manifold/tsne.rb +0 -222
  88. data/lib/rumale/metric_learning/fisher_discriminant_analysis.rb +0 -113
  89. data/lib/rumale/metric_learning/mlkr.rb +0 -161
  90. data/lib/rumale/metric_learning/neighbourhood_component_analysis.rb +0 -167
  91. data/lib/rumale/model_selection/cross_validation.rb +0 -125
  92. data/lib/rumale/model_selection/function.rb +0 -42
  93. data/lib/rumale/model_selection/grid_search_cv.rb +0 -225
  94. data/lib/rumale/model_selection/group_k_fold.rb +0 -93
  95. data/lib/rumale/model_selection/group_shuffle_split.rb +0 -115
  96. data/lib/rumale/model_selection/k_fold.rb +0 -81
  97. data/lib/rumale/model_selection/shuffle_split.rb +0 -90
  98. data/lib/rumale/model_selection/stratified_k_fold.rb +0 -99
  99. data/lib/rumale/model_selection/stratified_shuffle_split.rb +0 -118
  100. data/lib/rumale/model_selection/time_series_split.rb +0 -91
  101. data/lib/rumale/multiclass/one_vs_rest_classifier.rb +0 -83
  102. data/lib/rumale/naive_bayes/base_naive_bayes.rb +0 -47
  103. data/lib/rumale/naive_bayes/bernoulli_nb.rb +0 -82
  104. data/lib/rumale/naive_bayes/complement_nb.rb +0 -85
  105. data/lib/rumale/naive_bayes/gaussian_nb.rb +0 -69
  106. data/lib/rumale/naive_bayes/multinomial_nb.rb +0 -74
  107. data/lib/rumale/naive_bayes/negation_nb.rb +0 -71
  108. data/lib/rumale/nearest_neighbors/k_neighbors_classifier.rb +0 -133
  109. data/lib/rumale/nearest_neighbors/k_neighbors_regressor.rb +0 -108
  110. data/lib/rumale/nearest_neighbors/vp_tree.rb +0 -132
  111. data/lib/rumale/neural_network/adam.rb +0 -56
  112. data/lib/rumale/neural_network/base_mlp.rb +0 -248
  113. data/lib/rumale/neural_network/mlp_classifier.rb +0 -120
  114. data/lib/rumale/neural_network/mlp_regressor.rb +0 -90
  115. data/lib/rumale/pairwise_metric.rb +0 -152
  116. data/lib/rumale/pipeline/feature_union.rb +0 -69
  117. data/lib/rumale/pipeline/pipeline.rb +0 -175
  118. data/lib/rumale/preprocessing/bin_discretizer.rb +0 -93
  119. data/lib/rumale/preprocessing/binarizer.rb +0 -60
  120. data/lib/rumale/preprocessing/kernel_calculator.rb +0 -92
  121. data/lib/rumale/preprocessing/l1_normalizer.rb +0 -62
  122. data/lib/rumale/preprocessing/l2_normalizer.rb +0 -63
  123. data/lib/rumale/preprocessing/label_binarizer.rb +0 -89
  124. data/lib/rumale/preprocessing/label_encoder.rb +0 -79
  125. data/lib/rumale/preprocessing/max_abs_scaler.rb +0 -61
  126. data/lib/rumale/preprocessing/max_normalizer.rb +0 -62
  127. data/lib/rumale/preprocessing/min_max_scaler.rb +0 -76
  128. data/lib/rumale/preprocessing/one_hot_encoder.rb +0 -100
  129. data/lib/rumale/preprocessing/ordinal_encoder.rb +0 -109
  130. data/lib/rumale/preprocessing/polynomial_features.rb +0 -109
  131. data/lib/rumale/preprocessing/standard_scaler.rb +0 -71
  132. data/lib/rumale/probabilistic_output.rb +0 -114
  133. data/lib/rumale/tree/base_decision_tree.rb +0 -150
  134. data/lib/rumale/tree/decision_tree_classifier.rb +0 -150
  135. data/lib/rumale/tree/decision_tree_regressor.rb +0 -116
  136. data/lib/rumale/tree/extra_tree_classifier.rb +0 -107
  137. data/lib/rumale/tree/extra_tree_regressor.rb +0 -94
  138. data/lib/rumale/tree/gradient_tree_regressor.rb +0 -202
  139. data/lib/rumale/tree/node.rb +0 -39
  140. data/lib/rumale/utils.rb +0 -42
  141. data/lib/rumale/validation.rb +0 -128
  142. data/lib/rumale/values.rb +0 -13
@@ -1,285 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rumale/base/base_estimator'
4
-
5
- module Rumale
6
- module LinearModel
7
- # @!visibility private
8
- # This module consists of the classes that implement penalty (regularization) term.
9
- module Penalty
10
- # @!visibility private
11
- # L2Penalty is a class that applies L2 penalty to weight vector of linear model.
12
- # This class is used internally.
13
- class L2Penalty
14
- # @!visibility private
15
- def initialize(reg_param:)
16
- @reg_param = reg_param
17
- end
18
-
19
- # @!visibility private
20
- def call(weight, lr)
21
- weight - @reg_param * lr * weight
22
- end
23
- end
24
-
25
- # @!visibility private
26
- # L1Penalty is a class that applies L1 penalty to weight vector of linear model.
27
- # This class is used internally.
28
- class L1Penalty
29
- # @!visibility private
30
- def initialize(reg_param:)
31
- @q_vec = nil
32
- @u = 0
33
- @reg_param = reg_param
34
- end
35
-
36
- # @!visibility private
37
- def call(weight, lr)
38
- @q_vec ||= Numo::DFloat.zeros(weight.shape[0])
39
- @u += @reg_param * lr
40
- z = weight.dup
41
- gt = weight.gt(0)
42
- lt = weight.lt(0)
43
- weight[gt] = Numo::DFloat.maximum(0.0, weight[gt] - (@u + @q_vec[gt])) if gt.count.positive?
44
- weight[lt] = Numo::DFloat.minimum(0.0, weight[lt] + (@u - @q_vec[lt])) if lt.count.positive?
45
- @q_vec += weight - z
46
- weight
47
- end
48
- end
49
- end
50
-
51
- # @!visibility private
52
- # This module consists of the class that implements stochastic gradient descent (SGD) optimizer.
53
- module Optimizer
54
- # @!visibility private
55
- # SGD is a class that implements SGD optimizer.
56
- # This class is used internally.
57
- class SGD
58
- # @!visibility private
59
- # Create a new SGD optimizer.
60
- # @param learning_rate [Float] The initial value of learning rate.
61
- # @param momentum [Float] The initial value of momentum.
62
- # @param decay [Float] The smooting parameter.
63
- def initialize(learning_rate: 0.01, momentum: 0.0, decay: 0.0)
64
- @learning_rate = learning_rate
65
- @momentum = momentum
66
- @decay = decay
67
- @update = nil
68
- @iter = 0
69
- end
70
-
71
- # @!visibility private
72
- def current_learning_rate
73
- @learning_rate / (1.0 + @decay * @iter)
74
- end
75
-
76
- # @!visibility private
77
- def call(weight, gradient)
78
- @update ||= Numo::DFloat.zeros(weight.shape[0])
79
- @update = @momentum * @update - current_learning_rate * gradient
80
- @iter += 1
81
- weight + @update
82
- end
83
- end
84
- end
85
-
86
- # @!visibility private
87
- # This module consists of the classes that implement loss function for linear model.
88
- module Loss
89
- # @!visibility private
90
- # MeanSquaredError is a class that calculates mean squared error for linear regression model.
91
- class MeanSquaredError
92
- # @!visibility private
93
- def loss(out, y)
94
- ((out - y)**2).sum.fdiv(y.shape[0])
95
- end
96
-
97
- # @!visibility private
98
- def dloss(out, y)
99
- 2.fdiv(y.shape[0]) * (out - y)
100
- end
101
- end
102
-
103
- # @!visibility private
104
- # LogLoss is a class that calculates logistic loss for logistic regression.
105
- class LogLoss
106
- # @!visibility private
107
- def loss(out, y)
108
- Numo::NMath.log(1 + Numo::NMath.exp(-y * out)).sum.fdiv(y.shape[0])
109
- end
110
-
111
- # @!visibility private
112
- def dloss(out, y)
113
- y / (1 + Numo::NMath.exp(-y * out)) - y
114
- end
115
- end
116
-
117
- # @!visibility private
118
- # HingeLoss is a class that calculates hinge loss for support vector classifier.
119
- class HingeLoss
120
- # @!visibility private
121
- def loss(out, y)
122
- out.class.maximum(0.0, 1 - y * out).sum.fdiv(y.shape[0])
123
- end
124
-
125
- # @!visibility private
126
- def dloss(out, y)
127
- tids = (y * out).lt(1)
128
- d = Numo::DFloat.zeros(y.shape[0])
129
- d[tids] = -y[tids] if tids.count.positive?
130
- d
131
- end
132
- end
133
-
134
- # @!visibility private
135
- # EpsilonInsensitive is a class that calculates epsilon insensitive for support vector regressor.
136
- class EpsilonInsensitive
137
- # @!visibility private
138
- def initialize(epsilon: 0.1)
139
- @epsilon = epsilon
140
- end
141
-
142
- # @!visibility private
143
- def loss(out, y)
144
- out.class.maximum(0.0, (y - out).abs - @epsilon).sum.fdiv(y.shape[0])
145
- end
146
-
147
- # @!visibility private
148
- def dloss(out, y)
149
- d = Numo::DFloat.zeros(y.shape[0])
150
- tids = (out - y).gt(@epsilon)
151
- d[tids] = 1 if tids.count.positive?
152
- tids = (y - out).gt(@epsilon)
153
- d[tids] = -1 if tids.count.positive?
154
- d
155
- end
156
- end
157
- end
158
-
159
- # BaseSGD is an abstract class for implementation of linear model with mini-batch stochastic gradient descent (SGD) optimization.
160
- # This class is used internally.
161
- class BaseSGD
162
- include Rumale::Base::BaseEstimator
163
-
164
- # Create an initial linear model.
165
- def initialize
166
- @params = {}
167
- @params[:learning_rate] = 0.01
168
- @params[:decay] = nil
169
- @params[:momentum] = 0.0
170
- @params[:bias_scale] = 1.0
171
- @params[:fit_bias] = true
172
- @params[:reg_param] = 0.0
173
- @params[:l1_ratio] = 0.0
174
- @params[:max_iter] = 1000
175
- @params[:batch_size] = 50
176
- @params[:tol] = 0.0001
177
- @params[:verbose] = false
178
- @penalty_type = nil
179
- @loss_func = nil
180
- @weight_vec = nil
181
- @bias_term = nil
182
- @n_iter = nil
183
- @rng = nil
184
- end
185
-
186
- private
187
-
188
- L2_PENALTY = 'l2'
189
- L1_PENALTY = 'l1'
190
- ELASTICNET_PENALTY = 'elasticnet'
191
-
192
- private_constant :L2_PENALTY, :L1_PENALTY, :ELASTICNET_PENALTY
193
-
194
- def partial_fit(x, y)
195
- class_name = self.class.to_s.split('::').last if @params[:verbose]
196
- narr = x.class
197
- # Expand feature vectors for bias term.
198
- x = expand_feature(x) if fit_bias?
199
- # Initialize some variables.
200
- sub_rng = @rng.dup
201
- n_samples, n_features = x.shape
202
- weight = Numo::DFloat.zeros(n_features)
203
- optimizer = LinearModel::Optimizer::SGD.new(
204
- learning_rate: @params[:learning_rate],
205
- momentum: @params[:momentum],
206
- decay: @params[:decay]
207
- )
208
- l2_penalty = LinearModel::Penalty::L2Penalty.new(reg_param: l2_reg_param) if apply_l2_penalty?
209
- l1_penalty = LinearModel::Penalty::L1Penalty.new(reg_param: l1_reg_param) if apply_l1_penalty?
210
- # Optimization.
211
- @params[:max_iter].times do |t|
212
- sample_ids = Array(0...n_samples)
213
- sample_ids.shuffle!(random: sub_rng)
214
- until (subset_ids = sample_ids.shift(@params[:batch_size])).empty?
215
- # sampling
216
- sub_x = x[subset_ids, true]
217
- sub_y = y[subset_ids]
218
- # calculate gradient
219
- dloss = @loss_func.dloss(sub_x.dot(weight), sub_y)
220
- dloss = narr.minimum(1e12, narr.maximum(-1e12, dloss))
221
- gradient = dloss.dot(sub_x)
222
- # update weight
223
- lr = optimizer.current_learning_rate
224
- weight = optimizer.call(weight, gradient)
225
- # l2 regularization
226
- weight = l2_penalty.call(weight, lr) if apply_l2_penalty?
227
- # l1 regularization
228
- weight = l1_penalty.call(weight, lr) if apply_l1_penalty?
229
- end
230
- loss = @loss_func.loss(x.dot(weight), y)
231
- puts "[#{class_name}] Loss after #{t + 1} epochs: #{loss}" if @params[:verbose]
232
- break if loss < @params[:tol]
233
- end
234
- split_weight(weight)
235
- end
236
-
237
- def expand_feature(x)
238
- n_samples = x.shape[0]
239
- Numo::NArray.hstack([x, Numo::DFloat.ones([n_samples, 1]) * @params[:bias_scale]])
240
- end
241
-
242
- def split_weight(weight)
243
- if fit_bias?
244
- [weight[0...-1].dup, weight[-1]]
245
- else
246
- [weight, 0.0]
247
- end
248
- end
249
-
250
- def fit_bias?
251
- @params[:fit_bias] == true
252
- end
253
-
254
- def apply_l2_penalty?
255
- @penalty_type == L2_PENALTY || @penalty_type == ELASTICNET_PENALTY
256
- end
257
-
258
- def apply_l1_penalty?
259
- @penalty_type == L1_PENALTY || @penalty_type == ELASTICNET_PENALTY
260
- end
261
-
262
- def l2_reg_param
263
- case @penalty_type
264
- when ELASTICNET_PENALTY
265
- @params[:reg_param] * (1.0 - @params[:l1_ratio])
266
- when L2_PENALTY
267
- @params[:reg_param]
268
- else
269
- 0.0
270
- end
271
- end
272
-
273
- def l1_reg_param
274
- case @penalty_type
275
- when ELASTICNET_PENALTY
276
- @params[:reg_param] * @params[:l1_ratio]
277
- when L1_PENALTY
278
- @params[:reg_param]
279
- else
280
- 0.0
281
- end
282
- end
283
- end
284
- end
285
- end
@@ -1,119 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rumale/linear_model/base_sgd'
4
- require 'rumale/base/regressor'
5
-
6
- module Rumale
7
- module LinearModel
8
- # ElasticNet is a class that implements Elastic-net Regression
9
- # with stochastic gradient descent (SGD) optimization.
10
- #
11
- # @example
12
- # estimator =
13
- # Rumale::LinearModel::ElasticNet.new(reg_param: 0.1, l1_ratio: 0.5, max_iter: 1000, batch_size: 50, random_seed: 1)
14
- # estimator.fit(training_samples, traininig_values)
15
- # results = estimator.predict(testing_samples)
16
- #
17
- # *Reference*
18
- # - Shalev-Shwartz, S., and Singer, Y., "Pegasos: Primal Estimated sub-GrAdient SOlver for SVM," Proc. ICML'07, pp. 807--814, 2007.
19
- # - Tsuruoka, Y., Tsujii, J., and Ananiadou, S., "Stochastic Gradient Descent Training for L1-regularized Log-linear Models with Cumulative Penalty," Proc. ACL'09, pp. 477--485, 2009.
20
- # - Bottou, L., "Large-Scale Machine Learning with Stochastic Gradient Descent," Proc. COMPSTAT'10, pp. 177--186, 2010.
21
- class ElasticNet < BaseSGD
22
- include Base::Regressor
23
-
24
- # Return the weight vector.
25
- # @return [Numo::DFloat] (shape: [n_outputs, n_features])
26
- attr_reader :weight_vec
27
-
28
- # Return the bias term (a.k.a. intercept).
29
- # @return [Numo::DFloat] (shape: [n_outputs])
30
- attr_reader :bias_term
31
-
32
- # Return the random generator for random sampling.
33
- # @return [Random]
34
- attr_reader :rng
35
-
36
- # Create a new Elastic-net regressor.
37
- #
38
- # @param learning_rate [Float] The initial value of learning rate.
39
- # The learning rate decreases as the iteration proceeds according to the equation: learning_rate / (1 + decay * t).
40
- # @param decay [Float] The smoothing parameter for decreasing learning rate as the iteration proceeds.
41
- # If nil is given, the decay sets to 'reg_param * learning_rate'.
42
- # @param momentum [Float] The momentum factor.
43
- # @param reg_param [Float] The regularization parameter.
44
- # @param l1_ratio [Float] The elastic-net mixing parameter.
45
- # If l1_ratio = 1, the regularization is similar to Lasso.
46
- # If l1_ratio = 0, the regularization is similar to Ridge.
47
- # If 0 < l1_ratio < 1, the regularization is a combination of L1 and L2.
48
- # @param fit_bias [Boolean] The flag indicating whether to fit the bias term.
49
- # @param bias_scale [Float] The scale of the bias term.
50
- # @param max_iter [Integer] The maximum number of epochs that indicates
51
- # how many times the whole data is given to the training process.
52
- # @param batch_size [Integer] The size of the mini batches.
53
- # @param tol [Float] The tolerance of loss for terminating optimization.
54
- # @param n_jobs [Integer] The number of jobs for running the fit method in parallel.
55
- # If nil is given, the method does not execute in parallel.
56
- # If zero or less is given, it becomes equal to the number of processors.
57
- # This parameter is ignored if the Parallel gem is not loaded.
58
- # @param verbose [Boolean] The flag indicating whether to output loss during iteration.
59
- # @param random_seed [Integer] The seed value using to initialize the random generator.
60
- def initialize(learning_rate: 0.01, decay: nil, momentum: 0.9,
61
- reg_param: 1.0, l1_ratio: 0.5, fit_bias: true, bias_scale: 1.0,
62
- max_iter: 1000, batch_size: 50, tol: 1e-4,
63
- n_jobs: nil, verbose: false, random_seed: nil)
64
- check_params_numeric(learning_rate: learning_rate, momentum: momentum,
65
- reg_param: reg_param, l1_ratio: l1_ratio, bias_scale: bias_scale,
66
- max_iter: max_iter, batch_size: batch_size, tol: tol)
67
- check_params_boolean(fit_bias: fit_bias, verbose: verbose)
68
- check_params_numeric_or_nil(decay: decay, n_jobs: n_jobs, random_seed: random_seed)
69
- check_params_positive(learning_rate: learning_rate, reg_param: reg_param, max_iter: max_iter, batch_size: batch_size)
70
- super()
71
- @params.merge!(method(:initialize).parameters.map { |_t, arg| [arg, binding.local_variable_get(arg)] }.to_h)
72
- @params[:decay] ||= @params[:reg_param] * @params[:learning_rate]
73
- @params[:random_seed] ||= srand
74
- @rng = Random.new(@params[:random_seed])
75
- @penalty_type = ELASTICNET_PENALTY
76
- @loss_func = LinearModel::Loss::MeanSquaredError.new
77
- @weight_vec = nil
78
- @bias_term = nil
79
- end
80
-
81
- # Fit the model with given training data.
82
- #
83
- # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
84
- # @param y [Numo::DFloat] (shape: [n_samples, n_outputs]) The target values to be used for fitting the model.
85
- # @return [ElasticNet] The learned regressor itself.
86
- def fit(x, y)
87
- x = check_convert_sample_array(x)
88
- y = check_convert_tvalue_array(y)
89
- check_sample_tvalue_size(x, y)
90
-
91
- n_outputs = y.shape[1].nil? ? 1 : y.shape[1]
92
- n_features = x.shape[1]
93
-
94
- if n_outputs > 1
95
- @weight_vec = Numo::DFloat.zeros(n_outputs, n_features)
96
- @bias_term = Numo::DFloat.zeros(n_outputs)
97
- if enable_parallel?
98
- models = parallel_map(n_outputs) { |n| partial_fit(x, y[true, n]) }
99
- n_outputs.times { |n| @weight_vec[n, true], @bias_term[n] = models[n] }
100
- else
101
- n_outputs.times { |n| @weight_vec[n, true], @bias_term[n] = partial_fit(x, y[true, n]) }
102
- end
103
- else
104
- @weight_vec, @bias_term = partial_fit(x, y)
105
- end
106
- self
107
- end
108
-
109
- # Predict values for samples.
110
- #
111
- # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the values.
112
- # @return [Numo::DFloat] (shape: [n_samples, n_outputs]) Predicted values per sample.
113
- def predict(x)
114
- x = check_convert_sample_array(x)
115
- x.dot(@weight_vec.transpose) + @bias_term
116
- end
117
- end
118
- end
119
- end
@@ -1,115 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rumale/linear_model/base_sgd'
4
- require 'rumale/base/regressor'
5
-
6
- module Rumale
7
- module LinearModel
8
- # Lasso is a class that implements Lasso Regression
9
- # with stochastic gradient descent (SGD) optimization.
10
- #
11
- # @example
12
- # estimator =
13
- # Rumale::LinearModel::Lasso.new(reg_param: 0.1, max_iter: 1000, batch_size: 20, random_seed: 1)
14
- # estimator.fit(training_samples, traininig_values)
15
- # results = estimator.predict(testing_samples)
16
- #
17
- # *Reference*
18
- # - Shalev-Shwartz, S., and Singer, Y., "Pegasos: Primal Estimated sub-GrAdient SOlver for SVM," Proc. ICML'07, pp. 807--814, 2007.
19
- # - Tsuruoka, Y., Tsujii, J., and Ananiadou, S., "Stochastic Gradient Descent Training for L1-regularized Log-linear Models with Cumulative Penalty," Proc. ACL'09, pp. 477--485, 2009.
20
- # - Bottou, L., "Large-Scale Machine Learning with Stochastic Gradient Descent," Proc. COMPSTAT'10, pp. 177--186, 2010.
21
- class Lasso < BaseSGD
22
- include Base::Regressor
23
-
24
- # Return the weight vector.
25
- # @return [Numo::DFloat] (shape: [n_outputs, n_features])
26
- attr_reader :weight_vec
27
-
28
- # Return the bias term (a.k.a. intercept).
29
- # @return [Numo::DFloat] (shape: [n_outputs])
30
- attr_reader :bias_term
31
-
32
- # Return the random generator for random sampling.
33
- # @return [Random]
34
- attr_reader :rng
35
-
36
- # Create a new Lasso regressor.
37
- #
38
- # @param learning_rate [Float] The initial value of learning rate.
39
- # The learning rate decreases as the iteration proceeds according to the equation: learning_rate / (1 + decay * t).
40
- # @param decay [Float] The smoothing parameter for decreasing learning rate as the iteration proceeds.
41
- # If nil is given, the decay sets to 'reg_param * learning_rate'.
42
- # @param momentum [Float] The momentum factor.
43
- # @param reg_param [Float] The regularization parameter.
44
- # @param fit_bias [Boolean] The flag indicating whether to fit the bias term.
45
- # @param bias_scale [Float] The scale of the bias term.
46
- # @param max_iter [Integer] The maximum number of epochs that indicates
47
- # how many times the whole data is given to the training process.
48
- # @param batch_size [Integer] The size of the mini batches.
49
- # @param tol [Float] The tolerance of loss for terminating optimization.
50
- # @param n_jobs [Integer] The number of jobs for running the fit method in parallel.
51
- # If nil is given, the method does not execute in parallel.
52
- # If zero or less is given, it becomes equal to the number of processors.
53
- # This parameter is ignored if the Parallel gem is not loaded.
54
- # @param verbose [Boolean] The flag indicating whether to output loss during iteration.
55
- # @param random_seed [Integer] The seed value using to initialize the random generator.
56
- def initialize(learning_rate: 0.01, decay: nil, momentum: 0.9,
57
- reg_param: 1.0, fit_bias: true, bias_scale: 1.0,
58
- max_iter: 1000, batch_size: 50, tol: 1e-4,
59
- n_jobs: nil, verbose: false, random_seed: nil)
60
- check_params_numeric(learning_rate: learning_rate, momentum: momentum,
61
- reg_param: reg_param, bias_scale: bias_scale,
62
- max_iter: max_iter, batch_size: batch_size, tol: tol)
63
- check_params_boolean(fit_bias: fit_bias, verbose: verbose)
64
- check_params_numeric_or_nil(decay: decay, n_jobs: n_jobs, random_seed: random_seed)
65
- check_params_positive(learning_rate: learning_rate, reg_param: reg_param, max_iter: max_iter, batch_size: batch_size)
66
- super()
67
- @params.merge!(method(:initialize).parameters.map { |_t, arg| [arg, binding.local_variable_get(arg)] }.to_h)
68
- @params[:decay] ||= @params[:reg_param] * @params[:learning_rate]
69
- @params[:random_seed] ||= srand
70
- @rng = Random.new(@params[:random_seed])
71
- @penalty_type = L1_PENALTY
72
- @loss_func = LinearModel::Loss::MeanSquaredError.new
73
- @weight_vec = nil
74
- @bias_term = nil
75
- end
76
-
77
- # Fit the model with given training data.
78
- #
79
- # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
80
- # @param y [Numo::DFloat] (shape: [n_samples, n_outputs]) The target values to be used for fitting the model.
81
- # @return [Lasso] The learned regressor itself.
82
- def fit(x, y)
83
- x = check_convert_sample_array(x)
84
- y = check_convert_tvalue_array(y)
85
- check_sample_tvalue_size(x, y)
86
-
87
- n_outputs = y.shape[1].nil? ? 1 : y.shape[1]
88
- n_features = x.shape[1]
89
-
90
- if n_outputs > 1
91
- @weight_vec = Numo::DFloat.zeros(n_outputs, n_features)
92
- @bias_term = Numo::DFloat.zeros(n_outputs)
93
- if enable_parallel?
94
- models = parallel_map(n_outputs) { |n| partial_fit(x, y[true, n]) }
95
- n_outputs.times { |n| @weight_vec[n, true], @bias_term[n] = models[n] }
96
- else
97
- n_outputs.times { |n| @weight_vec[n, true], @bias_term[n] = partial_fit(x, y[true, n]) }
98
- end
99
- else
100
- @weight_vec, @bias_term = partial_fit(x, y)
101
- end
102
- self
103
- end
104
-
105
- # Predict values for samples.
106
- #
107
- # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the values.
108
- # @return [Numo::DFloat] (shape: [n_samples, n_outputs]) Predicted values per sample.
109
- def predict(x)
110
- x = check_convert_sample_array(x)
111
- x.dot(@weight_vec.transpose) + @bias_term
112
- end
113
- end
114
- end
115
- end