Class: Rumale::Ensemble::VotingRegressor

Inherits:
Base::Estimator show all
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.

Examples:

require 'rumale/ensemble/voting_regressor'

estimators = {
  rdg: Rumale::LinearModel::Ridge.new(reg_param: 0.1),
  mlp: Rumale::NeuralNetwork::MLPRegressor.new(hidden_units: [256], random_seed: 1),
  rnd: Rumale::Ensemble::RandomForestRegressor.new(random_seed: 1)
}
weights = { rdg: 0.2, mlp: 0.3, rnd: 0.5 }

regressor = Rumale::Ensemble::VotingRegressor.new(estimators: estimators, weights: weights, voting: 'soft')
regressor.fit(x_train, y_train)
results = regressor.predict(x_test)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Methods included from Base::Regressor

#score

Constructor Details

#initialize(estimators:, weights: nil) ⇒ VotingRegressor

Create a new ensembled regressor with voting rule.

Parameters:

  • estimators (Hash<Symbol,Regressor>)

    The sub-regressors to vote.

  • weights (Hash<Symbol,Float>) (defaults to: nil)

    The weight value for each regressor.



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

#estimatorsHash<Symbol,Regressor> (readonly)

Return the sub-regressors that voted.

Returns:

  • (Hash<Symbol,Regressor>)


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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for fitting the model.

  • y (Numo::Int32)

    (shape: [n_samples]) The labels to be used for fitting the model.

Returns:



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to predict the values.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_outputs]) Predicted value per sample.



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