Class: Rumale::Ensemble::VotingClassifier
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Ensemble::VotingClassifier
- Includes:
- Base::Classifier
- Defined in:
- rumale-ensemble/lib/rumale/ensemble/voting_classifier.rb
Overview
VotingClassifier is a class that implements classifier 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
-
#classes ⇒ Numo::Int32
readonly
Return the class labels.
-
#estimators ⇒ Hash<Symbol,Classifier>
readonly
Return the sub-classifiers that voted.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
-
#fit(x, y) ⇒ VotingClassifier
Fit the model with given training data.
-
#initialize(estimators:, weights: nil, voting: 'hard') ⇒ VotingClassifier
constructor
Create a new ensembled classifier with voting rule.
-
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
-
#predict_proba(x) ⇒ Numo::DFloat
Predict probability for samples.
Methods included from Base::Classifier
Constructor Details
#initialize(estimators:, weights: nil, voting: 'hard') ⇒ VotingClassifier
Create a new ensembled classifier with voting rule.
46 47 48 49 50 51 52 53 |
# File 'rumale-ensemble/lib/rumale/ensemble/voting_classifier.rb', line 46 def initialize(estimators:, weights: nil, voting: 'hard') super() @estimators = estimators @params = { weights: weights || estimators.each_key.with_object({}) { |name, w| w[name] = 1.0 }, voting: voting } end |
Instance Attribute Details
#classes ⇒ Numo::Int32 (readonly)
Return the class labels.
37 38 39 |
# File 'rumale-ensemble/lib/rumale/ensemble/voting_classifier.rb', line 37 def classes @classes end |
#estimators ⇒ Hash<Symbol,Classifier> (readonly)
Return the sub-classifiers that voted.
33 34 35 |
# File 'rumale-ensemble/lib/rumale/ensemble/voting_classifier.rb', line 33 def estimators @estimators end |
Instance Method Details
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'rumale-ensemble/lib/rumale/ensemble/voting_classifier.rb', line 77 def decision_function(x) x = ::Rumale::Validation.check_convert_sample_array(x) return predict_proba(x) if soft_voting? n_samples = x.shape[0] n_classes = @classes.size z = Numo::DFloat.zeros(n_samples, n_classes) @estimators.each do |name, estimator| estimator.predict(x).to_a.each_with_index { |c, i| z[i, c] += @params[:weights][name] } end z end |
#fit(x, y) ⇒ VotingClassifier
Fit the model with given training data.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'rumale-ensemble/lib/rumale/ensemble/voting_classifier.rb', line 60 def fit(x, y) x = ::Rumale::Validation.check_convert_sample_array(x) y = ::Rumale::Validation.check_convert_label_array(y) ::Rumale::Validation.check_sample_size(x, y) @encoder = ::Rumale::Preprocessing::LabelEncoder.new y_encoded = @encoder.fit_transform(y) @classes = Numo::NArray[*@encoder.classes] @estimators.each_key { |name| @estimators[name].fit(x, y_encoded) } self end |
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
95 96 97 98 99 100 101 102 103 |
# File 'rumale-ensemble/lib/rumale/ensemble/voting_classifier.rb', line 95 def predict(x) x = ::Rumale::Validation.check_convert_sample_array(x) n_samples = x.shape[0] n_classes = @classes.size z = decision_function(x) predicted = z.max_index(axis: 1) - Numo::Int32.new(n_samples).seq * n_classes Numo::Int32.cast(@encoder.inverse_transform(predicted)) end |
#predict_proba(x) ⇒ Numo::DFloat
Predict probability for samples.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'rumale-ensemble/lib/rumale/ensemble/voting_classifier.rb', line 109 def predict_proba(x) x = ::Rumale::Validation.check_convert_sample_array(x) n_samples = x.shape[0] n_classes = @classes.size z = Numo::DFloat.zeros(n_samples, n_classes) sum_weight = @params[:weights].each_value.sum @estimators.each do |name, estimator| z += @params[:weights][name] * estimator.predict_proba(x) end z /= sum_weight end |