rumale 0.13.8 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -0
  3. data/README.md +8 -10
  4. data/lib/rumale.rb +3 -0
  5. data/lib/rumale/base/classifier.rb +2 -2
  6. data/lib/rumale/base/cluster_analyzer.rb +2 -2
  7. data/lib/rumale/base/regressor.rb +2 -2
  8. data/lib/rumale/clustering/dbscan.rb +3 -4
  9. data/lib/rumale/clustering/gaussian_mixture.rb +5 -6
  10. data/lib/rumale/clustering/hdbscan.rb +4 -4
  11. data/lib/rumale/clustering/k_means.rb +5 -6
  12. data/lib/rumale/clustering/k_medoids.rb +5 -6
  13. data/lib/rumale/clustering/power_iteration.rb +4 -6
  14. data/lib/rumale/clustering/single_linkage.rb +3 -3
  15. data/lib/rumale/clustering/snn.rb +1 -1
  16. data/lib/rumale/clustering/spectral_clustering.rb +4 -6
  17. data/lib/rumale/dataset.rb +6 -10
  18. data/lib/rumale/decomposition/factor_analysis.rb +4 -4
  19. data/lib/rumale/decomposition/fast_ica.rb +6 -7
  20. data/lib/rumale/decomposition/nmf.rb +6 -7
  21. data/lib/rumale/decomposition/pca.rb +6 -7
  22. data/lib/rumale/ensemble/ada_boost_classifier.rb +8 -8
  23. data/lib/rumale/ensemble/ada_boost_regressor.rb +7 -7
  24. data/lib/rumale/ensemble/extra_trees_classifier.rb +8 -8
  25. data/lib/rumale/ensemble/extra_trees_regressor.rb +7 -7
  26. data/lib/rumale/ensemble/gradient_boosting_classifier.rb +8 -8
  27. data/lib/rumale/ensemble/gradient_boosting_regressor.rb +8 -8
  28. data/lib/rumale/ensemble/random_forest_classifier.rb +8 -8
  29. data/lib/rumale/ensemble/random_forest_regressor.rb +7 -7
  30. data/lib/rumale/evaluation_measure/accuracy.rb +2 -2
  31. data/lib/rumale/evaluation_measure/adjusted_rand_score.rb +2 -2
  32. data/lib/rumale/evaluation_measure/calinski_harabasz_score.rb +2 -2
  33. data/lib/rumale/evaluation_measure/davies_bouldin_score.rb +2 -2
  34. data/lib/rumale/evaluation_measure/explained_variance_score.rb +2 -2
  35. data/lib/rumale/evaluation_measure/f_score.rb +2 -2
  36. data/lib/rumale/evaluation_measure/log_loss.rb +2 -2
  37. data/lib/rumale/evaluation_measure/mean_absolute_error.rb +2 -2
  38. data/lib/rumale/evaluation_measure/mean_squared_error.rb +2 -2
  39. data/lib/rumale/evaluation_measure/mean_squared_log_error.rb +2 -2
  40. data/lib/rumale/evaluation_measure/median_absolute_error.rb +2 -2
  41. data/lib/rumale/evaluation_measure/mutual_information.rb +2 -2
  42. data/lib/rumale/evaluation_measure/normalized_mutual_information.rb +2 -2
  43. data/lib/rumale/evaluation_measure/precision.rb +2 -2
  44. data/lib/rumale/evaluation_measure/purity.rb +2 -2
  45. data/lib/rumale/evaluation_measure/r2_score.rb +2 -2
  46. data/lib/rumale/evaluation_measure/recall.rb +2 -2
  47. data/lib/rumale/evaluation_measure/roc_auc.rb +6 -3
  48. data/lib/rumale/evaluation_measure/silhouette_score.rb +2 -2
  49. data/lib/rumale/kernel_approximation/rbf.rb +5 -6
  50. data/lib/rumale/kernel_machine/kernel_pca.rb +4 -4
  51. data/lib/rumale/kernel_machine/kernel_ridge.rb +3 -3
  52. data/lib/rumale/kernel_machine/kernel_svc.rb +7 -8
  53. data/lib/rumale/linear_model/lasso.rb +5 -6
  54. data/lib/rumale/linear_model/linear_regression.rb +5 -6
  55. data/lib/rumale/linear_model/logistic_regression.rb +16 -15
  56. data/lib/rumale/linear_model/ridge.rb +5 -6
  57. data/lib/rumale/linear_model/svc.rb +34 -28
  58. data/lib/rumale/linear_model/svr.rb +5 -6
  59. data/lib/rumale/manifold/mds.rb +3 -4
  60. data/lib/rumale/manifold/tsne.rb +3 -5
  61. data/lib/rumale/model_selection/cross_validation.rb +6 -5
  62. data/lib/rumale/model_selection/grid_search_cv.rb +6 -6
  63. data/lib/rumale/model_selection/k_fold.rb +3 -3
  64. data/lib/rumale/model_selection/shuffle_split.rb +3 -5
  65. data/lib/rumale/model_selection/stratified_k_fold.rb +4 -4
  66. data/lib/rumale/model_selection/stratified_shuffle_split.rb +4 -6
  67. data/lib/rumale/multiclass/one_vs_rest_classifier.rb +4 -4
  68. data/lib/rumale/naive_bayes/naive_bayes.rb +14 -14
  69. data/lib/rumale/nearest_neighbors/k_neighbors_classifier.rb +5 -5
  70. data/lib/rumale/nearest_neighbors/k_neighbors_regressor.rb +4 -4
  71. data/lib/rumale/neural_network/base_mlp.rb +244 -0
  72. data/lib/rumale/neural_network/mlp_classifier.rb +119 -0
  73. data/lib/rumale/neural_network/mlp_regressor.rb +89 -0
  74. data/lib/rumale/optimizer/ada_grad.rb +1 -1
  75. data/lib/rumale/optimizer/adam.rb +3 -3
  76. data/lib/rumale/optimizer/nadam.rb +1 -1
  77. data/lib/rumale/optimizer/rmsprop.rb +1 -1
  78. data/lib/rumale/optimizer/sgd.rb +1 -1
  79. data/lib/rumale/optimizer/yellow_fin.rb +1 -2
  80. data/lib/rumale/pairwise_metric.rb +17 -19
  81. data/lib/rumale/pipeline/pipeline.rb +10 -10
  82. data/lib/rumale/polynomial_model/factorization_machine_classifier.rb +29 -21
  83. data/lib/rumale/polynomial_model/factorization_machine_regressor.rb +6 -6
  84. data/lib/rumale/preprocessing/bin_discretizer.rb +3 -3
  85. data/lib/rumale/preprocessing/l2_normalizer.rb +2 -2
  86. data/lib/rumale/preprocessing/label_binarizer.rb +2 -2
  87. data/lib/rumale/preprocessing/label_encoder.rb +1 -1
  88. data/lib/rumale/preprocessing/max_abs_scaler.rb +3 -3
  89. data/lib/rumale/preprocessing/min_max_scaler.rb +3 -3
  90. data/lib/rumale/preprocessing/one_hot_encoder.rb +4 -3
  91. data/lib/rumale/preprocessing/ordinal_encoder.rb +1 -1
  92. data/lib/rumale/preprocessing/standard_scaler.rb +3 -3
  93. data/lib/rumale/tree/base_decision_tree.rb +1 -1
  94. data/lib/rumale/tree/decision_tree_classifier.rb +7 -7
  95. data/lib/rumale/tree/decision_tree_regressor.rb +6 -6
  96. data/lib/rumale/tree/extra_tree_classifier.rb +7 -7
  97. data/lib/rumale/tree/extra_tree_regressor.rb +6 -6
  98. data/lib/rumale/tree/gradient_tree_regressor.rb +9 -9
  99. data/lib/rumale/validation.rb +32 -2
  100. data/lib/rumale/version.rb +1 -1
  101. data/rumale.gemspec +7 -7
  102. metadata +11 -7
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rumale/base/classifier'
4
+ require 'rumale/neural_network/base_mlp'
5
+ require 'rumale/preprocessing/label_binarizer'
6
+
7
+ module Rumale
8
+ module NeuralNetwork
9
+ # MLPClassifier is a class that implements classifier based on multi-layer perceptron.
10
+ # MLPClassifier use ReLu as the activation function and Adam as the optimization method
11
+ # and softmax cross entropy as the loss function.
12
+ #
13
+ # @example
14
+ # estimator = Rumale::NeuralNetwork::MLPClassifier.new(hidden_units: [100, 100], dropout_rate: 0.3)
15
+ # estimator.fit(training_samples, traininig_labels)
16
+ # results = estimator.predict(testing_samples)
17
+ class MLPClassifier < BaseMLP
18
+ include Base::Classifier
19
+
20
+ # Return the network.
21
+ # @return [Rumale::NeuralNetwork::Model::Sequential]
22
+ attr_reader :network
23
+
24
+ # Return the class labels.
25
+ # @return [Numo::Int32] (size: n_classes)
26
+ attr_reader :classes
27
+
28
+ # Return the number of iterations run for optimization
29
+ # @return [Integer]
30
+ attr_reader :n_iter
31
+
32
+ # Return the random generator.
33
+ # @return [Random]
34
+ attr_reader :rng
35
+
36
+ # Create a new classifier with multi-layer preceptron.
37
+ #
38
+ # @param hidden_units [Array] The number of units in the i-th hidden layer.
39
+ # @param dropout_rate [Float] The rate of the units to drop.
40
+ # @param learning_rate [Float] The initial value of learning rate in Adam optimizer.
41
+ # @param decay1 [Float] The smoothing parameter for the first moment in Adam optimizer.
42
+ # @param decay2 [Float] The smoothing parameter for the second moment in Adam optimizer.
43
+ # @param max_iter [Integer] The maximum number of iterations.
44
+ # @param batch_size [Intger] The size of the mini batches.
45
+ # @param tol [Float] The tolerance of loss for terminating optimization.
46
+ # @param verbose [Boolean] The flag indicating whether to output loss during iteration.
47
+ # @param random_seed [Integer] The seed value using to initialize the random generator.
48
+ def initialize(hidden_units: [128, 128], dropout_rate: 0.4, learning_rate: 0.001, decay1: 0.9, decay2: 0.999,
49
+ max_iter: 10000, batch_size: 50, tol: 1e-4, verbose: false, random_seed: nil)
50
+ check_params_type(Array, hidden_units: hidden_units)
51
+ check_params_numeric(dropout_rate: dropout_rate, learning_rate: learning_rate, decay1: decay1, decay2: decay2,
52
+ max_iter: max_iter, batch_size: batch_size, tol: tol)
53
+ check_params_boolean(verbose: verbose)
54
+ check_params_numeric_or_nil(random_seed: random_seed)
55
+ super
56
+ @classes = nil
57
+ @network = nil
58
+ end
59
+
60
+ # Fit the model with given training data.
61
+ #
62
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
63
+ # @param y [Numo::Int32] (shape: [n_samples]) The labels to be used for fitting the model.
64
+ # @return [MLPClassifier] The learned classifier itself.
65
+ def fit(x, y)
66
+ x = check_convert_sample_array(x)
67
+ y = check_convert_label_array(y)
68
+ check_sample_label_size(x, y)
69
+
70
+ @classes = Numo::Int32[*y.to_a.uniq.sort]
71
+ n_labels = @classes.size
72
+ n_features = x.shape[1]
73
+ sub_rng = @rng.dup
74
+
75
+ loss = Loss::SoftmaxCrossEntropy.new
76
+ @network = buld_network(n_features, n_labels, sub_rng)
77
+ @network = train(x, one_hot_encode(y), @network, loss, sub_rng)
78
+ @network.delete_dropout
79
+
80
+ self
81
+ end
82
+
83
+ # Predict class labels for samples.
84
+ #
85
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
86
+ # @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.
87
+ def predict(x)
88
+ x = check_convert_sample_array(x)
89
+ n_samples = x.shape[0]
90
+ decision_values = predict_proba(x)
91
+ predicted = Array.new(n_samples) { |n| @classes[decision_values[n, true].max_index] }
92
+ Numo::Int32.asarray(predicted)
93
+ end
94
+
95
+ # Predict probability for samples.
96
+ #
97
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the probailities.
98
+ # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.
99
+ def predict_proba(x)
100
+ x = check_convert_sample_array(x)
101
+ out, = @network.forward(x)
102
+ softmax(out)
103
+ end
104
+
105
+ private
106
+
107
+ def one_hot_encode(y)
108
+ encoder = Rumale::Preprocessing::LabelBinarizer.new
109
+ encoder.fit_transform(y)
110
+ end
111
+
112
+ def softmax(x)
113
+ clip = x.max(-1).expand_dims(-1)
114
+ exp_x = Numo::NMath.exp(x - clip)
115
+ exp_x / exp_x.sum(-1).expand_dims(-1)
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rumale/base/regressor'
4
+ require 'rumale/neural_network/base_mlp'
5
+
6
+ module Rumale
7
+ module NeuralNetwork
8
+ # MLPRegressor is a class that implements regressor based on multi-layer perceptron.
9
+ # MLPRegressor use ReLu as the activation function and Adam as the optimization method
10
+ # and mean squared error as the loss function.
11
+ #
12
+ # @example
13
+ # estimator = Rumale::NeuralNetwork::MLPRegressor.new(hidden_units: [100, 100], dropout_rate: 0.3)
14
+ # estimator.fit(training_samples, traininig_labels)
15
+ # results = estimator.predict(testing_samples)
16
+ class MLPRegressor < BaseMLP
17
+ include Base::Regressor
18
+
19
+ # Return the network.
20
+ # @return [Rumale::NeuralNetwork::Model::Sequential]
21
+ attr_reader :network
22
+
23
+ # Return the number of iterations run for optimization
24
+ # @return [Integer]
25
+ attr_reader :n_iter
26
+
27
+ # Return the random generator.
28
+ # @return [Random]
29
+ attr_reader :rng
30
+
31
+ # Create a new regressor with multi-layer perceptron.
32
+ #
33
+ # @param hidden_units [Array] The number of units in the i-th hidden layer.
34
+ # @param dropout_rate [Float] The rate of the units to drop.
35
+ # @param learning_rate [Float] The initial value of learning rate in Adam optimizer.
36
+ # @param decay1 [Float] The smoothing parameter for the first moment in Adam optimizer.
37
+ # @param decay2 [Float] The smoothing parameter for the second moment in Adam optimizer.
38
+ # @param max_iter [Integer] The maximum number of iterations.
39
+ # @param batch_size [Intger] The size of the mini batches.
40
+ # @param tol [Float] The tolerance of loss for terminating optimization.
41
+ # @param verbose [Boolean] The flag indicating whether to output loss during iteration.
42
+ # @param random_seed [Integer] The seed value using to initialize the random generator.
43
+ def initialize(hidden_units: [128, 128], dropout_rate: 0.4, learning_rate: 0.001, decay1: 0.9, decay2: 0.999,
44
+ max_iter: 10000, batch_size: 50, tol: 1e-4, verbose: false, random_seed: nil)
45
+ check_params_type(Array, hidden_units: hidden_units)
46
+ check_params_numeric(dropout_rate: dropout_rate, learning_rate: learning_rate, decay1: decay1, decay2: decay2,
47
+ max_iter: max_iter, batch_size: batch_size, tol: tol)
48
+ check_params_boolean(verbose: verbose)
49
+ check_params_numeric_or_nil(random_seed: random_seed)
50
+ super
51
+ @network = nil
52
+ end
53
+
54
+ # Fit the model with given training data.
55
+ #
56
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
57
+ # @param y [Numo::DFloat] (shape: [n_samples, n_outputs]) The taget values to be used for fitting the model.
58
+ # @return [MLPRegressor] The learned regressor itself.
59
+ def fit(x, y)
60
+ x = check_convert_sample_array(x)
61
+ y = check_convert_tvalue_array(y)
62
+ check_sample_tvalue_size(x, y)
63
+
64
+ y = y.expand_dims(1) if y.ndim == 1
65
+ n_targets = y.shape[1]
66
+ n_features = x.shape[1]
67
+ sub_rng = @rng.dup
68
+
69
+ loss = Loss::MeanSquaredError.new
70
+ @network = buld_network(n_features, n_targets, sub_rng)
71
+ @network = train(x, y, @network, loss, sub_rng)
72
+ @network.delete_dropout
73
+
74
+ self
75
+ end
76
+
77
+ # Predict values for samples.
78
+ #
79
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the values.
80
+ # @return [Numo::DFloat] (shape: [n_samples, n_outputs]) Predicted values per sample.
81
+ def predict(x)
82
+ x = check_convert_sample_array(x)
83
+ out, = @network.forward(x)
84
+ out = out[true, 0] if out.shape[1] == 1
85
+ out
86
+ end
87
+ end
88
+ end
89
+ end
@@ -22,7 +22,7 @@ module Rumale
22
22
  #
23
23
  # @param learning_rate [Float] The initial value of learning rate.
24
24
  def initialize(learning_rate: 0.01)
25
- check_params_float(learning_rate: learning_rate)
25
+ check_params_numeric(learning_rate: learning_rate)
26
26
  check_params_positive(learning_rate: learning_rate)
27
27
  @params = {}
28
28
  @params[:learning_rate] = learning_rate
@@ -24,7 +24,7 @@ module Rumale
24
24
  # @param decay1 [Float] The smoothing parameter for the first moment.
25
25
  # @param decay2 [Float] The smoothing parameter for the second moment.
26
26
  def initialize(learning_rate: 0.001, decay1: 0.9, decay2: 0.999)
27
- check_params_float(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
27
+ check_params_numeric(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
28
28
  check_params_positive(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
29
29
  @params = {}
30
30
  @params[:learning_rate] = learning_rate
@@ -41,8 +41,8 @@ module Rumale
41
41
  # @param gradient [Numo::DFloat] (shape: [n_features]) The gradient for updating the weight.
42
42
  # @return [Numo::DFloat] (shape: [n_feautres]) The updated weight.
43
43
  def call(weight, gradient)
44
- @fst_moment ||= Numo::DFloat.zeros(weight.shape[0])
45
- @sec_moment ||= Numo::DFloat.zeros(weight.shape[0])
44
+ @fst_moment ||= Numo::DFloat.zeros(weight.shape)
45
+ @sec_moment ||= Numo::DFloat.zeros(weight.shape)
46
46
 
47
47
  @iter += 1
48
48
 
@@ -25,7 +25,7 @@ module Rumale
25
25
  # @param decay1 [Float] The smoothing parameter for the first moment.
26
26
  # @param decay2 [Float] The smoothing parameter for the second moment.
27
27
  def initialize(learning_rate: 0.01, decay1: 0.9, decay2: 0.999)
28
- check_params_float(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
28
+ check_params_numeric(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
29
29
  check_params_positive(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
30
30
  @params = {}
31
31
  @params[:learning_rate] = learning_rate
@@ -25,7 +25,7 @@ module Rumale
25
25
  # @param momentum [Float] The initial value of momentum.
26
26
  # @param decay [Float] The smooting parameter.
27
27
  def initialize(learning_rate: 0.01, momentum: 0.9, decay: 0.9)
28
- check_params_float(learning_rate: learning_rate, momentum: momentum, decay: decay)
28
+ check_params_numeric(learning_rate: learning_rate, momentum: momentum, decay: decay)
29
29
  check_params_positive(learning_rate: learning_rate, momentum: momentum, decay: decay)
30
30
  @params = {}
31
31
  @params[:learning_rate] = learning_rate
@@ -21,7 +21,7 @@ module Rumale
21
21
  # @param momentum [Float] The initial value of momentum.
22
22
  # @param decay [Float] The smooting parameter.
23
23
  def initialize(learning_rate: 0.01, momentum: 0.0, decay: 0.0)
24
- check_params_float(learning_rate: learning_rate, momentum: momentum, decay: decay)
24
+ check_params_numeric(learning_rate: learning_rate, momentum: momentum, decay: decay)
25
25
  check_params_positive(learning_rate: learning_rate, momentum: momentum, decay: decay)
26
26
  @params = {}
27
27
  @params[:learning_rate] = learning_rate
@@ -25,8 +25,7 @@ module Rumale
25
25
  # @param decay [Float] The smooting parameter.
26
26
  # @param window_width [Integer] The sliding window width for searching curvature range.
27
27
  def initialize(learning_rate: 0.01, momentum: 0.9, decay: 0.999, window_width: 20)
28
- check_params_float(learning_rate: learning_rate, momentum: momentum, decay: decay)
29
- check_params_integer(window_width: window_width)
28
+ check_params_numeric(learning_rate: learning_rate, momentum: momentum, decay: decay, window_width: window_width)
30
29
  check_params_positive(learning_rate: learning_rate, momentum: momentum, decay: decay, window_width: window_width)
31
30
  @params = {}
32
31
  @params[:learning_rate] = learning_rate
@@ -13,8 +13,8 @@ module Rumale
13
13
  # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)
14
14
  def euclidean_distance(x, y = nil)
15
15
  y = x if y.nil?
16
- Rumale::Validation.check_sample_array(x)
17
- Rumale::Validation.check_sample_array(y)
16
+ x = Rumale::Validation.check_convert_sample_array(x)
17
+ y = Rumale::Validation.check_convert_sample_array(y)
18
18
  Numo::NMath.sqrt(squared_error(x, y).abs)
19
19
  end
20
20
 
@@ -25,8 +25,8 @@ module Rumale
25
25
  # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)
26
26
  def manhattan_distance(x, y = nil)
27
27
  y = x if y.nil?
28
- Rumale::Validation.check_sample_array(x)
29
- Rumale::Validation.check_sample_array(y)
28
+ x = Rumale::Validation.check_convert_sample_array(x)
29
+ y = Rumale::Validation.check_convert_sample_array(y)
30
30
  n_samples_x = x.shape[0]
31
31
  n_samples_y = y.shape[0]
32
32
  distance_mat = Numo::DFloat.zeros(n_samples_x, n_samples_y)
@@ -43,8 +43,8 @@ module Rumale
43
43
  # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)
44
44
  def squared_error(x, y = nil)
45
45
  y = x if y.nil?
46
- Rumale::Validation.check_sample_array(x)
47
- Rumale::Validation.check_sample_array(y)
46
+ x = Rumale::Validation.check_convert_sample_array(x)
47
+ y = Rumale::Validation.check_convert_sample_array(y)
48
48
  n_features = x.shape[1]
49
49
  one_vec = Numo::DFloat.ones(n_features).expand_dims(1)
50
50
  sum_x_vec = (x**2).dot(one_vec)
@@ -62,9 +62,9 @@ module Rumale
62
62
  def rbf_kernel(x, y = nil, gamma = nil)
63
63
  y = x if y.nil?
64
64
  gamma ||= 1.0 / x.shape[1]
65
- Rumale::Validation.check_sample_array(x)
66
- Rumale::Validation.check_sample_array(y)
67
- Rumale::Validation.check_params_float(gamma: gamma)
65
+ x = Rumale::Validation.check_convert_sample_array(x)
66
+ y = Rumale::Validation.check_convert_sample_array(y)
67
+ Rumale::Validation.check_params_numeric(gamma: gamma)
68
68
  Numo::NMath.exp(-gamma * squared_error(x, y).abs)
69
69
  end
70
70
 
@@ -75,8 +75,8 @@ module Rumale
75
75
  # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)
76
76
  def linear_kernel(x, y = nil)
77
77
  y = x if y.nil?
78
- Rumale::Validation.check_sample_array(x)
79
- Rumale::Validation.check_sample_array(y)
78
+ x = Rumale::Validation.check_convert_sample_array(x)
79
+ y = Rumale::Validation.check_convert_sample_array(y)
80
80
  x.dot(y.transpose)
81
81
  end
82
82
 
@@ -91,10 +91,9 @@ module Rumale
91
91
  def polynomial_kernel(x, y = nil, degree = 3, gamma = nil, coef = 1)
92
92
  y = x if y.nil?
93
93
  gamma ||= 1.0 / x.shape[1]
94
- Rumale::Validation.check_sample_array(x)
95
- Rumale::Validation.check_sample_array(y)
96
- Rumale::Validation.check_params_float(gamma: gamma)
97
- Rumale::Validation.check_params_integer(degree: degree, coef: coef)
94
+ x = Rumale::Validation.check_convert_sample_array(x)
95
+ y = Rumale::Validation.check_convert_sample_array(y)
96
+ Rumale::Validation.check_params_numeric(gamma: gamma, degree: degree, coef: coef)
98
97
  (x.dot(y.transpose) * gamma + coef)**degree
99
98
  end
100
99
 
@@ -108,10 +107,9 @@ module Rumale
108
107
  def sigmoid_kernel(x, y = nil, gamma = nil, coef = 1)
109
108
  y = x if y.nil?
110
109
  gamma ||= 1.0 / x.shape[1]
111
- Rumale::Validation.check_sample_array(x)
112
- Rumale::Validation.check_sample_array(y)
113
- Rumale::Validation.check_params_float(gamma: gamma)
114
- Rumale::Validation.check_params_integer(coef: coef)
110
+ x = Rumale::Validation.check_convert_sample_array(x)
111
+ y = Rumale::Validation.check_convert_sample_array(y)
112
+ Rumale::Validation.check_params_numeric(gamma: gamma, coef: coef)
115
113
  Numo::NMath.tanh(x.dot(y.transpose) * gamma + coef)
116
114
  end
117
115
  end
@@ -40,7 +40,7 @@ module Rumale
40
40
  # @param y [Numo::NArray] (shape: [n_samples, n_outputs]) The target values or labels to be used for fitting the model.
41
41
  # @return [Pipeline] The learned pipeline itself.
42
42
  def fit(x, y)
43
- check_sample_array(x)
43
+ x = check_convert_sample_array(x)
44
44
  trans_x = apply_transforms(x, y, fit: true)
45
45
  last_estimator&.fit(trans_x, y)
46
46
  self
@@ -52,7 +52,7 @@ module Rumale
52
52
  # @param y [Numo::NArray] (shape: [n_samples, n_outputs], default: nil) The target values or labels to be used for fitting the model.
53
53
  # @return [Numo::NArray] The predicted results by last estimator.
54
54
  def fit_predict(x, y = nil)
55
- check_sample_array(x)
55
+ x = check_convert_sample_array(x)
56
56
  trans_x = apply_transforms(x, y, fit: true)
57
57
  last_estimator.fit_predict(trans_x)
58
58
  end
@@ -63,7 +63,7 @@ module Rumale
63
63
  # @param y [Numo::NArray] (shape: [n_samples, n_outputs], default: nil) The target values or labels to be used for fitting the model.
64
64
  # @return [Numo::NArray] The predicted results by last estimator.
65
65
  def fit_transform(x, y = nil)
66
- check_sample_array(x)
66
+ x = check_convert_sample_array(x)
67
67
  trans_x = apply_transforms(x, y, fit: true)
68
68
  last_estimator.fit_transform(trans_x, y)
69
69
  end
@@ -73,7 +73,7 @@ module Rumale
73
73
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to compute the scores.
74
74
  # @return [Numo::DFloat] (shape: [n_samples]) Confidence score per sample.
75
75
  def decision_function(x)
76
- check_sample_array(x)
76
+ x = check_convert_sample_array(x)
77
77
  trans_x = apply_transforms(x)
78
78
  last_estimator.decision_function(trans_x)
79
79
  end
@@ -83,7 +83,7 @@ module Rumale
83
83
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to obtain prediction result.
84
84
  # @return [Numo::NArray] The predicted results by last estimator.
85
85
  def predict(x)
86
- check_sample_array(x)
86
+ x = check_convert_sample_array(x)
87
87
  trans_x = apply_transforms(x)
88
88
  last_estimator.predict(trans_x)
89
89
  end
@@ -93,7 +93,7 @@ module Rumale
93
93
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the log-probailities.
94
94
  # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted log-probability of each class per sample.
95
95
  def predict_log_proba(x)
96
- check_sample_array(x)
96
+ x = check_convert_sample_array(x)
97
97
  trans_x = apply_transforms(x)
98
98
  last_estimator.predict_log_proba(trans_x)
99
99
  end
@@ -103,7 +103,7 @@ module Rumale
103
103
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the probailities.
104
104
  # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.
105
105
  def predict_proba(x)
106
- check_sample_array(x)
106
+ x = check_convert_sample_array(x)
107
107
  trans_x = apply_transforms(x)
108
108
  last_estimator.predict_proba(trans_x)
109
109
  end
@@ -113,7 +113,7 @@ module Rumale
113
113
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to be transformed.
114
114
  # @return [Numo::DFloat] (shape: [n_samples, n_components]) The transformed samples.
115
115
  def transform(x)
116
- check_sample_array(x)
116
+ x = check_convert_sample_array(x)
117
117
  trans_x = apply_transforms(x)
118
118
  last_estimator.nil? ? trans_x : last_estimator.transform(trans_x)
119
119
  end
@@ -123,7 +123,7 @@ module Rumale
123
123
  # @param z [Numo::DFloat] (shape: [n_samples, n_components]) The transformed samples to be restored into original space.
124
124
  # @return [Numo::DFloat] (shape: [n_samples, n_featuress]) The restored samples.
125
125
  def inverse_transform(z)
126
- check_sample_array(z)
126
+ z = check_convert_sample_array(z)
127
127
  itrans_z = z
128
128
  @steps.keys.reverse_each do |name|
129
129
  transformer = @steps[name]
@@ -139,7 +139,7 @@ module Rumale
139
139
  # @param y [Numo::NArray] (shape: [n_samples, n_outputs]) True target values or labels for testing data.
140
140
  # @return [Float] The score of last estimator
141
141
  def score(x, y)
142
- check_sample_array(x)
142
+ x = check_convert_sample_array(x)
143
143
  trans_x = apply_transforms(x)
144
144
  last_estimator.score(trans_x, y)
145
145
  end