rumale 0.18.5 → 0.19.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +15 -3
- data/.travis.yml +3 -3
- data/CHANGELOG.md +44 -0
- data/Gemfile +9 -0
- data/README.md +6 -44
- data/lib/rumale.rb +3 -0
- data/lib/rumale/base/base_estimator.rb +2 -0
- data/lib/rumale/clustering/dbscan.rb +5 -1
- data/lib/rumale/clustering/gaussian_mixture.rb +2 -0
- data/lib/rumale/clustering/hdbscan.rb +5 -3
- data/lib/rumale/clustering/k_means.rb +2 -1
- data/lib/rumale/clustering/k_medoids.rb +5 -1
- data/lib/rumale/clustering/mini_batch_k_means.rb +139 -0
- data/lib/rumale/clustering/power_iteration.rb +3 -1
- data/lib/rumale/clustering/single_linkage.rb +3 -1
- data/lib/rumale/clustering/snn.rb +2 -2
- data/lib/rumale/clustering/spectral_clustering.rb +2 -2
- data/lib/rumale/dataset.rb +2 -0
- data/lib/rumale/decomposition/factor_analysis.rb +3 -1
- data/lib/rumale/decomposition/fast_ica.rb +2 -2
- data/lib/rumale/decomposition/nmf.rb +1 -1
- data/lib/rumale/decomposition/pca.rb +25 -6
- data/lib/rumale/ensemble/ada_boost_classifier.rb +4 -1
- data/lib/rumale/ensemble/ada_boost_regressor.rb +4 -2
- data/lib/rumale/ensemble/extra_trees_classifier.rb +1 -1
- data/lib/rumale/ensemble/extra_trees_regressor.rb +1 -1
- data/lib/rumale/ensemble/gradient_boosting_classifier.rb +4 -4
- data/lib/rumale/ensemble/gradient_boosting_regressor.rb +4 -4
- data/lib/rumale/evaluation_measure/adjusted_rand_score.rb +1 -1
- data/lib/rumale/evaluation_measure/calinski_harabasz_score.rb +1 -1
- data/lib/rumale/evaluation_measure/davies_bouldin_score.rb +1 -1
- data/lib/rumale/evaluation_measure/function.rb +2 -1
- data/lib/rumale/evaluation_measure/mutual_information.rb +1 -1
- data/lib/rumale/evaluation_measure/normalized_mutual_information.rb +4 -2
- data/lib/rumale/evaluation_measure/precision_recall.rb +5 -0
- data/lib/rumale/evaluation_measure/purity.rb +1 -1
- data/lib/rumale/evaluation_measure/roc_auc.rb +3 -0
- data/lib/rumale/evaluation_measure/silhouette_score.rb +3 -1
- data/lib/rumale/feature_extraction/feature_hasher.rb +14 -1
- data/lib/rumale/feature_extraction/hash_vectorizer.rb +1 -0
- data/lib/rumale/feature_extraction/tfidf_transformer.rb +113 -0
- data/lib/rumale/kernel_approximation/nystroem.rb +1 -1
- data/lib/rumale/kernel_approximation/rbf.rb +1 -1
- data/lib/rumale/kernel_machine/kernel_fda.rb +1 -1
- data/lib/rumale/kernel_machine/kernel_pca.rb +1 -1
- data/lib/rumale/kernel_machine/kernel_ridge.rb +2 -0
- data/lib/rumale/kernel_machine/kernel_svc.rb +1 -1
- data/lib/rumale/linear_model/base_linear_model.rb +2 -0
- data/lib/rumale/linear_model/elastic_net.rb +3 -3
- data/lib/rumale/linear_model/lasso.rb +3 -3
- data/lib/rumale/linear_model/linear_regression.rb +2 -1
- data/lib/rumale/linear_model/logistic_regression.rb +3 -3
- data/lib/rumale/linear_model/ridge.rb +2 -1
- data/lib/rumale/linear_model/svc.rb +3 -3
- data/lib/rumale/linear_model/svr.rb +3 -3
- data/lib/rumale/manifold/mds.rb +3 -1
- data/lib/rumale/manifold/tsne.rb +6 -2
- data/lib/rumale/metric_learning/neighbourhood_component_analysis.rb +14 -1
- data/lib/rumale/model_selection/grid_search_cv.rb +1 -0
- data/lib/rumale/naive_bayes/bernoulli_nb.rb +1 -1
- data/lib/rumale/naive_bayes/multinomial_nb.rb +1 -1
- data/lib/rumale/nearest_neighbors/k_neighbors_classifier.rb +1 -0
- data/lib/rumale/nearest_neighbors/k_neighbors_regressor.rb +2 -0
- data/lib/rumale/nearest_neighbors/vp_tree.rb +1 -1
- data/lib/rumale/neural_network/adam.rb +2 -2
- data/lib/rumale/neural_network/base_mlp.rb +1 -0
- data/lib/rumale/optimizer/ada_grad.rb +4 -1
- data/lib/rumale/optimizer/adam.rb +4 -1
- data/lib/rumale/optimizer/nadam.rb +6 -1
- data/lib/rumale/optimizer/rmsprop.rb +5 -2
- data/lib/rumale/optimizer/sgd.rb +3 -0
- data/lib/rumale/optimizer/yellow_fin.rb +4 -1
- data/lib/rumale/pipeline/pipeline.rb +3 -0
- data/lib/rumale/polynomial_model/base_factorization_machine.rb +5 -0
- data/lib/rumale/polynomial_model/factorization_machine_classifier.rb +7 -2
- data/lib/rumale/polynomial_model/factorization_machine_regressor.rb +7 -2
- data/lib/rumale/preprocessing/l1_normalizer.rb +62 -0
- data/lib/rumale/preprocessing/l2_normalizer.rb +2 -1
- data/lib/rumale/preprocessing/one_hot_encoder.rb +3 -0
- data/lib/rumale/preprocessing/ordinal_encoder.rb +2 -0
- data/lib/rumale/preprocessing/polynomial_features.rb +1 -0
- data/lib/rumale/probabilistic_output.rb +4 -2
- data/lib/rumale/tree/base_decision_tree.rb +2 -0
- data/lib/rumale/tree/decision_tree_classifier.rb +1 -0
- data/lib/rumale/tree/extra_tree_classifier.rb +1 -1
- data/lib/rumale/tree/extra_tree_regressor.rb +1 -1
- data/lib/rumale/tree/gradient_tree_regressor.rb +5 -5
- data/lib/rumale/utils.rb +1 -0
- data/lib/rumale/validation.rb +7 -0
- data/lib/rumale/version.rb +1 -1
- data/rumale.gemspec +1 -13
- metadata +10 -133
@@ -156,6 +156,7 @@ module Rumale
|
|
156
156
|
|
157
157
|
def valid_param_grid(grid)
|
158
158
|
raise TypeError, 'Expect class of param_grid to be Hash or Array' unless grid.is_a?(Hash) || grid.is_a?(Array)
|
159
|
+
|
159
160
|
grid = [grid] if grid.is_a?(Hash)
|
160
161
|
grid.each do |h|
|
161
162
|
raise TypeError, 'Expect class of elements in param_grid to be Hash' unless h.is_a?(Hash)
|
@@ -12,7 +12,7 @@ module Rumale
|
|
12
12
|
# results = estimator.predict(testing_samples)
|
13
13
|
#
|
14
14
|
# *Reference*
|
15
|
-
# - C D
|
15
|
+
# - Manning, C D., Raghavan, P., and Schutze, H., "Introduction to Information Retrieval," Cambridge University Press., 2008.
|
16
16
|
class BernoulliNB < BaseNaiveBayes
|
17
17
|
# Return the class labels.
|
18
18
|
# @return [Numo::Int32] (size: n_classes)
|
@@ -12,7 +12,7 @@ module Rumale
|
|
12
12
|
# results = estimator.predict(testing_samples)
|
13
13
|
#
|
14
14
|
# *Reference*
|
15
|
-
# - C D
|
15
|
+
# - Manning, C D., Raghavan, P., and Schutze, H., "Introduction to Information Retrieval," Cambridge University Press., 2008.
|
16
16
|
class MultinomialNB < BaseNaiveBayes
|
17
17
|
# Return the class labels.
|
18
18
|
# @return [Numo::Int32] (size: n_classes)
|
@@ -67,6 +67,7 @@ module Rumale
|
|
67
67
|
y = check_convert_label_array(y)
|
68
68
|
check_sample_label_size(x, y)
|
69
69
|
raise ArgumentError, 'Expect the input distance matrix to be square.' if @params[:metric] == 'precomputed' && x.shape[0] != x.shape[1]
|
70
|
+
|
70
71
|
@prototypes = if @params[:metric] == 'euclidean'
|
71
72
|
if @params[:algorithm] == 'vptree'
|
72
73
|
VPTree.new(x)
|
@@ -61,6 +61,7 @@ module Rumale
|
|
61
61
|
y = check_convert_tvalue_array(y)
|
62
62
|
check_sample_tvalue_size(x, y)
|
63
63
|
raise ArgumentError, 'Expect the input distance matrix to be square.' if @params[:metric] == 'precomputed' && x.shape[0] != x.shape[1]
|
64
|
+
|
64
65
|
@prototypes = if @params[:metric] == 'euclidean'
|
65
66
|
if @params[:algorithm] == 'vptree'
|
66
67
|
VPTree.new(x)
|
@@ -82,6 +83,7 @@ module Rumale
|
|
82
83
|
if @params[:metric] == 'precomputed' && x.shape[1] != @values.shape[0]
|
83
84
|
raise ArgumentError, 'Expect the size input matrix to be n_testing_samples-by-n_training_samples.'
|
84
85
|
end
|
86
|
+
|
85
87
|
# Initialize some variables.
|
86
88
|
n_samples = x.shape[0]
|
87
89
|
n_prototypes, n_outputs = @values.shape
|
@@ -11,7 +11,7 @@ module Rumale
|
|
11
11
|
# This class is used internally for k-nearest neighbor estimators.
|
12
12
|
#
|
13
13
|
# *Reference*
|
14
|
-
# P N
|
14
|
+
# - Yianilos, P N., "Data Structures and Algorithms for Nearest Neighbor Search in General Metric Spaces," Proc. SODA'93, pp. 311--321, 1993.
|
15
15
|
class VPTree
|
16
16
|
include Validation
|
17
17
|
include Base::BaseEstimator
|
@@ -11,7 +11,7 @@ module Rumale
|
|
11
11
|
# Adam is a class that implements Adam optimizer.
|
12
12
|
#
|
13
13
|
# *Reference*
|
14
|
-
# - D P
|
14
|
+
# - Kingma, D P., and Ba, J., "Adam: A Method for Stochastic Optimization," Proc. ICLR'15, 2015.
|
15
15
|
class Adam
|
16
16
|
include Base::BaseEstimator
|
17
17
|
|
@@ -32,7 +32,7 @@ module Rumale
|
|
32
32
|
end
|
33
33
|
|
34
34
|
# @!visibility private
|
35
|
-
# Calculate the updated weight with
|
35
|
+
# Calculate the updated weight with Adam adaptive learning rate.
|
36
36
|
#
|
37
37
|
# @param weight [Numo::DFloat] (shape: [n_features]) The weight to be updated.
|
38
38
|
# @param gradient [Numo::DFloat] (shape: [n_features]) The gradient for updating the weight.
|
@@ -7,8 +7,10 @@ module Rumale
|
|
7
7
|
module Optimizer
|
8
8
|
# AdaGrad is a class that implements AdaGrad optimizer.
|
9
9
|
#
|
10
|
+
# @deprecated AdaGrad will be deleted in version 0.20.0.
|
11
|
+
#
|
10
12
|
# *Reference*
|
11
|
-
# -
|
13
|
+
# - Duchi, J., Hazan, E., and Singer, Y., "Adaptive Subgradient Methods for Online Learning and Stochastic Optimization," J. Machine Learning Research, vol. 12, pp. 2121--2159, 2011.
|
12
14
|
class AdaGrad
|
13
15
|
include Base::BaseEstimator
|
14
16
|
include Validation
|
@@ -17,6 +19,7 @@ module Rumale
|
|
17
19
|
#
|
18
20
|
# @param learning_rate [Float] The initial value of learning rate.
|
19
21
|
def initialize(learning_rate: 0.01)
|
22
|
+
warn 'warning: AdaGrad is deprecated. This class will be deleted in version 0.20.0.'
|
20
23
|
check_params_numeric(learning_rate: learning_rate)
|
21
24
|
check_params_positive(learning_rate: learning_rate)
|
22
25
|
@params = {}
|
@@ -7,8 +7,10 @@ module Rumale
|
|
7
7
|
module Optimizer
|
8
8
|
# Adam is a class that implements Adam optimizer.
|
9
9
|
#
|
10
|
+
# @deprecated Adam will be deleted in version 0.20.0.
|
11
|
+
#
|
10
12
|
# *Reference*
|
11
|
-
# - D P
|
13
|
+
# - Kingma, D P., and Ba, J., "Adam: A Method for Stochastic Optimization," Proc. ICLR'15, 2015.
|
12
14
|
class Adam
|
13
15
|
include Base::BaseEstimator
|
14
16
|
include Validation
|
@@ -19,6 +21,7 @@ module Rumale
|
|
19
21
|
# @param decay1 [Float] The smoothing parameter for the first moment.
|
20
22
|
# @param decay2 [Float] The smoothing parameter for the second moment.
|
21
23
|
def initialize(learning_rate: 0.001, decay1: 0.9, decay2: 0.999)
|
24
|
+
warn 'warning: Adam is deprecated. This class will be deleted in version 0.20.0.'
|
22
25
|
check_params_numeric(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
|
23
26
|
check_params_positive(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
|
24
27
|
@params = {}
|
@@ -5,11 +5,15 @@ require 'rumale/base/base_estimator'
|
|
5
5
|
|
6
6
|
module Rumale
|
7
7
|
# This module consists of the classes that implement optimizers adaptively tuning hyperparameters.
|
8
|
+
#
|
9
|
+
# @deprecated Optimizer module will be deleted in version 0.20.0.
|
8
10
|
module Optimizer
|
9
11
|
# Nadam is a class that implements Nadam optimizer.
|
10
12
|
#
|
13
|
+
# @deprecated Nadam will be deleted in version 0.20.0.
|
14
|
+
#
|
11
15
|
# *Reference*
|
12
|
-
# -
|
16
|
+
# - Dozat, T., "Incorporating Nesterov Momentum into Adam," Tech. Repo. Stanford University, 2015.
|
13
17
|
class Nadam
|
14
18
|
include Base::BaseEstimator
|
15
19
|
include Validation
|
@@ -20,6 +24,7 @@ module Rumale
|
|
20
24
|
# @param decay1 [Float] The smoothing parameter for the first moment.
|
21
25
|
# @param decay2 [Float] The smoothing parameter for the second moment.
|
22
26
|
def initialize(learning_rate: 0.01, decay1: 0.9, decay2: 0.999)
|
27
|
+
warn 'warning: Nadam is deprecated. This class will be deleted in version 0.20.0.'
|
23
28
|
check_params_numeric(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
|
24
29
|
check_params_positive(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
|
25
30
|
@params = {}
|
@@ -7,9 +7,11 @@ module Rumale
|
|
7
7
|
module Optimizer
|
8
8
|
# RMSProp is a class that implements RMSProp optimizer.
|
9
9
|
#
|
10
|
+
# @deprecated RMSProp will be deleted in version 0.20.0.
|
11
|
+
#
|
10
12
|
# *Reference*
|
11
|
-
# -
|
12
|
-
# -
|
13
|
+
# - Sutskever, I., Martens, J., Dahl, G., and Hinton, G., "On the importance of initialization and momentum in deep learning," Proc. ICML' 13, pp. 1139--1147, 2013.
|
14
|
+
# - Hinton, G., Srivastava, N., and Swersky, K., "Lecture 6e rmsprop," Neural Networks for Machine Learning, 2012.
|
13
15
|
class RMSProp
|
14
16
|
include Base::BaseEstimator
|
15
17
|
include Validation
|
@@ -20,6 +22,7 @@ module Rumale
|
|
20
22
|
# @param momentum [Float] The initial value of momentum.
|
21
23
|
# @param decay [Float] The smooting parameter.
|
22
24
|
def initialize(learning_rate: 0.01, momentum: 0.9, decay: 0.9)
|
25
|
+
warn 'warning: RMSProp is deprecated. This class will be deleted in version 0.20.0.'
|
23
26
|
check_params_numeric(learning_rate: learning_rate, momentum: momentum, decay: decay)
|
24
27
|
check_params_positive(learning_rate: learning_rate, momentum: momentum, decay: decay)
|
25
28
|
@params = {}
|
data/lib/rumale/optimizer/sgd.rb
CHANGED
@@ -6,6 +6,8 @@ require 'rumale/base/base_estimator'
|
|
6
6
|
module Rumale
|
7
7
|
module Optimizer
|
8
8
|
# SGD is a class that implements SGD optimizer.
|
9
|
+
#
|
10
|
+
# @deprecated SGD will be deleted in version 0.20.0.
|
9
11
|
class SGD
|
10
12
|
include Base::BaseEstimator
|
11
13
|
include Validation
|
@@ -16,6 +18,7 @@ module Rumale
|
|
16
18
|
# @param momentum [Float] The initial value of momentum.
|
17
19
|
# @param decay [Float] The smooting parameter.
|
18
20
|
def initialize(learning_rate: 0.01, momentum: 0.0, decay: 0.0)
|
21
|
+
warn 'warning: SGD is deprecated. This class will be deleted in version 0.20.0.'
|
19
22
|
check_params_numeric(learning_rate: learning_rate, momentum: momentum, decay: decay)
|
20
23
|
check_params_positive(learning_rate: learning_rate, momentum: momentum, decay: decay)
|
21
24
|
@params = {}
|
@@ -7,8 +7,10 @@ module Rumale
|
|
7
7
|
module Optimizer
|
8
8
|
# YellowFin is a class that implements YellowFin optimizer.
|
9
9
|
#
|
10
|
+
# @deprecated YellowFin will be deleted in version 0.20.0.
|
11
|
+
#
|
10
12
|
# *Reference*
|
11
|
-
# - J
|
13
|
+
# - Zhang, J., and Mitliagkas, I., "YellowFin and the Art of Momentum Tuning," CoRR abs/1706.03471, 2017.
|
12
14
|
class YellowFin
|
13
15
|
include Base::BaseEstimator
|
14
16
|
include Validation
|
@@ -20,6 +22,7 @@ module Rumale
|
|
20
22
|
# @param decay [Float] The smooting parameter.
|
21
23
|
# @param window_width [Integer] The sliding window width for searching curvature range.
|
22
24
|
def initialize(learning_rate: 0.01, momentum: 0.9, decay: 0.999, window_width: 20)
|
25
|
+
warn 'warning: YellowFin is deprecated. This class will be deleted in version 0.20.0.'
|
23
26
|
check_params_numeric(learning_rate: learning_rate, momentum: momentum, decay: decay, window_width: window_width)
|
24
27
|
check_params_positive(learning_rate: learning_rate, momentum: momentum, decay: decay, window_width: window_width)
|
25
28
|
@params = {}
|
@@ -119,6 +119,7 @@ module Rumale
|
|
119
119
|
@steps.keys.reverse_each do |name|
|
120
120
|
transformer = @steps[name]
|
121
121
|
next if transformer.nil?
|
122
|
+
|
122
123
|
itrans_z = transformer.inverse_transform(itrans_z)
|
123
124
|
end
|
124
125
|
itrans_z
|
@@ -140,6 +141,7 @@ module Rumale
|
|
140
141
|
steps.keys[0...-1].each do |name|
|
141
142
|
transformer = steps[name]
|
142
143
|
next if transformer.nil? || %i[fit transform].all? { |m| transformer.class.method_defined?(m) }
|
144
|
+
|
143
145
|
raise TypeError,
|
144
146
|
'Class of intermediate step in pipeline should be implemented fit and transform methods: ' \
|
145
147
|
"#{name} => #{transformer.class}"
|
@@ -158,6 +160,7 @@ module Rumale
|
|
158
160
|
@steps.keys[0...-1].each do |name|
|
159
161
|
transformer = @steps[name]
|
160
162
|
next if transformer.nil?
|
163
|
+
|
161
164
|
transformer.fit(trans_x, y) if fit
|
162
165
|
trans_x = transformer.transform(trans_x)
|
163
166
|
end
|
@@ -5,9 +5,13 @@ require 'rumale/optimizer/nadam'
|
|
5
5
|
|
6
6
|
module Rumale
|
7
7
|
# This module consists of the classes that implement polynomial models.
|
8
|
+
#
|
9
|
+
# @deprecated PolynomialModel module will be deleted in version 0.20.0.
|
8
10
|
module PolynomialModel
|
9
11
|
# BaseFactorizationMachine is an abstract class for implementation of Factorization Machine-based estimators.
|
10
12
|
# This class is used internally.
|
13
|
+
#
|
14
|
+
# @deprecated BaseFactorizationMachine will be deleted in version 0.20.0.
|
11
15
|
class BaseFactorizationMachine
|
12
16
|
include Base::BaseEstimator
|
13
17
|
|
@@ -75,6 +79,7 @@ module Rumale
|
|
75
79
|
# Calculate gradients for loss function.
|
76
80
|
loss_grad = loss_gradient(sub_x, ex_sub_x, sub_y, factor_mat, weight_vec)
|
77
81
|
next if loss_grad.ne(0.0).count.zero?
|
82
|
+
|
78
83
|
# Update each parameter.
|
79
84
|
weight_vec = weight_optimizer.call(weight_vec, weight_gradient(loss_grad, ex_sub_x, weight_vec))
|
80
85
|
@params[:n_factors].times do |n|
|
@@ -10,6 +10,10 @@ module Rumale
|
|
10
10
|
# with stochastic gradient descent (SGD) optimization.
|
11
11
|
# For multiclass classification problem, it uses one-vs-the-rest strategy.
|
12
12
|
#
|
13
|
+
# @deprecated
|
14
|
+
# FactorizationMachineClassifier will be deleted in version 0.20.0.
|
15
|
+
# The Ruamle author recommends using the xlearn gem instead.
|
16
|
+
#
|
13
17
|
# @example
|
14
18
|
# estimator =
|
15
19
|
# Rumale::PolynomialModel::FactorizationMachineClassifier.new(
|
@@ -19,8 +23,8 @@ module Rumale
|
|
19
23
|
# results = estimator.predict(testing_samples)
|
20
24
|
#
|
21
25
|
# *Reference*
|
22
|
-
# -
|
23
|
-
# -
|
26
|
+
# - Rendle, S., "Factorization Machines with libFM," ACM TIST, vol. 3 (3), pp. 57:1--57:22, 2012.
|
27
|
+
# - Rendle, S., "Factorization Machines," Proc. ICDM'10, pp. 995--1000, 2010.
|
24
28
|
class FactorizationMachineClassifier < BaseFactorizationMachine
|
25
29
|
include Base::Classifier
|
26
30
|
|
@@ -65,6 +69,7 @@ module Rumale
|
|
65
69
|
def initialize(n_factors: 2, loss: 'hinge', reg_param_linear: 1.0, reg_param_factor: 1.0,
|
66
70
|
max_iter: 200, batch_size: 50, tol: 1e-4,
|
67
71
|
optimizer: nil, n_jobs: nil, verbose: false, random_seed: nil)
|
72
|
+
warn 'warning: FactorizationMachineClassifier is deprecated. This class will be deleted in version 0.20.0.'
|
68
73
|
check_params_numeric(reg_param_linear: reg_param_linear, reg_param_factor: reg_param_factor,
|
69
74
|
n_factors: n_factors, max_iter: max_iter, batch_size: batch_size, tol: tol)
|
70
75
|
check_params_string(loss: loss)
|
@@ -8,6 +8,10 @@ module Rumale
|
|
8
8
|
# FactorizationMachineRegressor is a class that implements Factorization Machine
|
9
9
|
# with stochastic gradient descent (SGD) optimization.
|
10
10
|
#
|
11
|
+
# @deprecated
|
12
|
+
# FactorizationMachineRegressor will be deleted in version 0.20.0.
|
13
|
+
# The Ruamle author recommends using the xlearn gem instead.
|
14
|
+
#
|
11
15
|
# @example
|
12
16
|
# estimator =
|
13
17
|
# Rumale::PolynomialModel::FactorizationMachineRegressor.new(
|
@@ -17,8 +21,8 @@ module Rumale
|
|
17
21
|
# results = estimator.predict(testing_samples)
|
18
22
|
#
|
19
23
|
# *Reference*
|
20
|
-
# -
|
21
|
-
# -
|
24
|
+
# - Rendle, S., "Factorization Machines with libFM," ACM TIST, vol. 3 (3), pp. 57:1--57:22, 2012.
|
25
|
+
# - Rendle, S., "Factorization Machines," Proc. ICDM'10, pp. 995--1000, 2010.
|
22
26
|
class FactorizationMachineRegressor < BaseFactorizationMachine
|
23
27
|
include Base::Regressor
|
24
28
|
|
@@ -58,6 +62,7 @@ module Rumale
|
|
58
62
|
def initialize(n_factors: 2, reg_param_linear: 1.0, reg_param_factor: 1.0,
|
59
63
|
max_iter: 200, batch_size: 50, tol: 1e-4,
|
60
64
|
optimizer: nil, n_jobs: nil, verbose: false, random_seed: nil)
|
65
|
+
warn 'warning: FactorizationMachineClassifier is deprecated. This class will be deleted in version 0.20.0.'
|
61
66
|
check_params_numeric(reg_param_linear: reg_param_linear, reg_param_factor: reg_param_factor,
|
62
67
|
n_factors: n_factors, max_iter: max_iter, batch_size: batch_size, tol: tol)
|
63
68
|
check_params_boolean(verbose: verbose)
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rumale/base/base_estimator'
|
4
|
+
require 'rumale/base/transformer'
|
5
|
+
|
6
|
+
module Rumale
|
7
|
+
module Preprocessing
|
8
|
+
# Normalize samples to unit L1-norm.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# normalizer = Rumale::Preprocessing::L1Normalizer.new
|
12
|
+
# new_samples = normalizer.fit_transform(samples)
|
13
|
+
class L1Normalizer
|
14
|
+
include Base::BaseEstimator
|
15
|
+
include Base::Transformer
|
16
|
+
|
17
|
+
# Return the vector consists of L1-norm for each sample.
|
18
|
+
# @return [Numo::DFloat] (shape: [n_samples])
|
19
|
+
attr_reader :norm_vec # :nodoc:
|
20
|
+
|
21
|
+
# Create a new normalizer for normaliing to L1-norm.
|
22
|
+
def initialize
|
23
|
+
@params = {}
|
24
|
+
@norm_vec = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
# Calculate L1-norms of each sample.
|
28
|
+
#
|
29
|
+
# @overload fit(x) -> L1Normalizer
|
30
|
+
#
|
31
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to calculate L1-norms.
|
32
|
+
# @return [L1Normalizer]
|
33
|
+
def fit(x, _y = nil)
|
34
|
+
x = check_convert_sample_array(x)
|
35
|
+
@norm_vec = x.abs.sum(1)
|
36
|
+
@norm_vec[@norm_vec.eq(0)] = 1
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
# Calculate L1-norms of each sample, and then normalize samples to L1-norm.
|
41
|
+
#
|
42
|
+
# @overload fit_transform(x) -> Numo::DFloat
|
43
|
+
#
|
44
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to calculate L1-norms.
|
45
|
+
# @return [Numo::DFloat] The normalized samples.
|
46
|
+
def fit_transform(x, _y = nil)
|
47
|
+
x = check_convert_sample_array(x)
|
48
|
+
fit(x)
|
49
|
+
x / @norm_vec.expand_dims(1)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Calculate L1-norms of each sample, and then normalize samples to L1-norm.
|
53
|
+
# This method calls the fit_transform method. This method exists for the Pipeline class.
|
54
|
+
#
|
55
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to calculate L1-norms.
|
56
|
+
# @return [Numo::DFloat] The normalized samples.
|
57
|
+
def transform(x)
|
58
|
+
fit_transform(x)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -34,6 +34,7 @@ module Rumale
|
|
34
34
|
def fit(x, _y = nil)
|
35
35
|
x = check_convert_sample_array(x)
|
36
36
|
@norm_vec = Numo::NMath.sqrt((x**2).sum(1))
|
37
|
+
@norm_vec[@norm_vec.eq(0)] = 1
|
37
38
|
self
|
38
39
|
end
|
39
40
|
|
@@ -46,7 +47,7 @@ module Rumale
|
|
46
47
|
def fit_transform(x, _y = nil)
|
47
48
|
x = check_convert_sample_array(x)
|
48
49
|
fit(x)
|
49
|
-
x / @norm_vec.
|
50
|
+
x / @norm_vec.expand_dims(1)
|
50
51
|
end
|
51
52
|
|
52
53
|
# Calculate L2-norms of each sample, and then normalize samples to unit L2-norm.
|
@@ -51,6 +51,7 @@ module Rumale
|
|
51
51
|
def fit(x, _y = nil)
|
52
52
|
x = Numo::Int32.cast(x) unless x.is_a?(Numo::Int32)
|
53
53
|
raise ArgumentError, 'Expected the input samples only consists of non-negative integer values.' if x.lt(0).any?
|
54
|
+
|
54
55
|
@n_values = x.max(0) + 1
|
55
56
|
@feature_indices = Numo::Int32.hstack([[0], @n_values]).cumsum
|
56
57
|
@active_features = encode(x, @feature_indices).sum(0).ne(0).where
|
@@ -67,6 +68,7 @@ module Rumale
|
|
67
68
|
x = Numo::Int32.cast(x) unless x.is_a?(Numo::Int32)
|
68
69
|
raise ArgumentError, 'Expected the input samples only consists of non-negative integer values.' if x.lt(0).any?
|
69
70
|
raise ArgumentError, 'Expected the input samples only consists of non-negative integer values.' if x.lt(0).any?
|
71
|
+
|
70
72
|
fit(x).transform(x)
|
71
73
|
end
|
72
74
|
|
@@ -77,6 +79,7 @@ module Rumale
|
|
77
79
|
def transform(x)
|
78
80
|
x = Numo::Int32.cast(x) unless x.is_a?(Numo::Int32)
|
79
81
|
raise ArgumentError, 'Expected the input samples only consists of non-negative integer values.' if x.lt(0).any?
|
82
|
+
|
80
83
|
codes = encode(x, @feature_indices)
|
81
84
|
codes[true, @active_features].dup
|
82
85
|
end
|
@@ -51,6 +51,7 @@ module Rumale
|
|
51
51
|
def fit(x, _y = nil)
|
52
52
|
raise TypeError, 'Expect class of sample matrix to be Numo::NArray' unless x.is_a?(Numo::NArray)
|
53
53
|
raise ArgumentError, 'Expect sample matrix to be 2-D array' unless x.shape.size == 2
|
54
|
+
|
54
55
|
n_features = x.shape[1]
|
55
56
|
@categories = Array.new(n_features) { |n| x[true, n].to_a.uniq.sort }
|
56
57
|
self
|
@@ -65,6 +66,7 @@ module Rumale
|
|
65
66
|
def fit_transform(x, _y = nil)
|
66
67
|
raise TypeError, 'Expect class of sample matrix to be Numo::NArray' unless x.is_a?(Numo::NArray)
|
67
68
|
raise ArgumentError, 'Expect sample matrix to be 2-D array' unless x.shape.size == 2
|
69
|
+
|
68
70
|
fit(x).transform(x)
|
69
71
|
end
|
70
72
|
|
@@ -41,6 +41,7 @@ module Rumale
|
|
41
41
|
def initialize(degree: 2)
|
42
42
|
check_params_numeric(degree: degree)
|
43
43
|
raise ArgumentError, 'Expect the value of degree parameter greater than or eqaul to 1.' if degree < 1
|
44
|
+
|
44
45
|
@params = {}
|
45
46
|
@params[:degree] = degree
|
46
47
|
@n_output_features = nil
|