Class: Rumale::Ensemble::VotingRegressor
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Ensemble::VotingRegressor
- Includes:
- Base::Regressor
- Defined in:
- rumale-ensemble/lib/rumale/ensemble/voting_regressor.rb
Overview
VotingRegressor is a class that implements regressor with voting ensemble method.
Reference
-
Zhou, Z-H., “Ensemble Methods - Foundations and Algorithms,” CRC Press Taylor and Francis Group, Chapman and Hall/CRC, 2012.
Instance Attribute Summary collapse
-
#estimators ⇒ Hash<Symbol,Regressor>
readonly
Return the sub-regressors that voted.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x, y) ⇒ VotingRegressor
Fit the model with given training data.
-
#initialize(estimators:, weights: nil) ⇒ VotingRegressor
constructor
Create a new ensembled regressor with voting rule.
-
#predict(x) ⇒ Numo::DFloat
Predict values for samples.
Methods included from Base::Regressor
Constructor Details
#initialize(estimators:, weights: nil) ⇒ VotingRegressor
Create a new ensembled regressor with voting rule.
38 39 40 41 42 43 44 |
# File 'rumale-ensemble/lib/rumale/ensemble/voting_regressor.rb', line 38 def initialize(estimators:, weights: nil) super() @estimators = estimators @params = { weights: weights || estimators.each_key.with_object({}) { |name, w| w[name] = 1.0 } } end |
Instance Attribute Details
#estimators ⇒ Hash<Symbol,Regressor> (readonly)
Return the sub-regressors that voted.
32 33 34 |
# File 'rumale-ensemble/lib/rumale/ensemble/voting_regressor.rb', line 32 def estimators @estimators end |
Instance Method Details
#fit(x, y) ⇒ VotingRegressor
Fit the model with given training data.
51 52 53 54 55 56 57 58 59 60 |
# File 'rumale-ensemble/lib/rumale/ensemble/voting_regressor.rb', line 51 def fit(x, y) x = ::Rumale::Validation.check_convert_sample_array(x) y = ::Rumale::Validation.check_convert_target_value_array(y) ::Rumale::Validation.check_sample_size(x, y) @n_outputs = y.ndim > 1 ? y.shape[1] : 1 @estimators.each_key { |name| @estimators[name].fit(x, y) } self end |
#predict(x) ⇒ Numo::DFloat
Predict values for samples.
66 67 68 69 70 71 72 73 74 75 |
# File 'rumale-ensemble/lib/rumale/ensemble/voting_regressor.rb', line 66 def predict(x) x = ::Rumale::Validation.check_convert_sample_array(x) z = single_target? ? Numo::DFloat.zeros(x.shape[0]) : Numo::DFloat.zeros(x.shape[0], @n_outputs) sum_weight = @params[:weights].each_value.sum @estimators.each do |name, estimator| z += @params[:weights][name] * estimator.predict(x) end z / sum_weight end |