VotingClassifier
- class sklearn.ensemble.VotingClassifier(estimators, *, voting='hard', weights=None, n_jobs=None, flatten_transform=True, verbose=False)
Soft Voting/Majority Rule classifier for unfitted estimators.
Read more in the User Guide.
Invoking the fit method on the VotingClassifier will fit clones
of those original estimators that will be stored in the class attribute
self.estimators_. An estimator can be set to 'drop' using
set_params.
If ‘hard’, uses predicted class labels for majority rule voting. Else if ‘soft’, predicts the class label based on the argmax of the sums of the predicted probabilities, which is recommended for an ensemble of well-calibrated classifiers.
Sequence of weights (float or int) to weight the occurrences of
predicted class labels (hard voting) or class probabilities
before averaging (soft voting). Uses uniform weights if None.
The number of jobs to run in parallel for fit.
None means 1 unless in a joblib.parallel_backend context.
-1 means using all processors. See Glossary
for more details.
Affects shape of transform output only when voting=’soft’ If voting=’soft’ and flatten_transform=True, transform method returns matrix with shape (n_samples, n_classifiers * n_classes). If flatten_transform=False, it returns (n_classifiers, n_samples, n_classes).
If True, the time elapsed while fitting will be printed as it is completed.
The collection of fitted sub-estimators as defined in estimators
that are not ‘drop’. Note that sub-estimators are always fitted on
integer-encoded labels (see le_ attribute). When y contains
non-integer class labels (e.g. strings), use le_.inverse_transform
to map predictions back to the original label space.
BunchAttribute to access any fitted sub-estimators by name.
>>> import numpy as np
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.naive_bayes import GaussianNB
>>> from sklearn.ensemble import RandomForestClassifier, VotingClassifier
>>> clf1 = LogisticRegression(random_state=
To drop an estimator, set_params can be used to remove it. Here we
dropped one of the estimators, resulting in 2 fitted estimators:
>>> eclf2 = eclf2.set_params(lr='drop')
>>> eclf2 = eclf2.fit(X, y)
>>> len(eclf2.estimators_)
2
Setting flatten_transform=True with voting='soft' flattens output shape of
transform:
>>> eclf3 = VotingClassifier(estimators=[
... ('lr', clf1), ('rf', clf2), ('gnb', clf3)],
... voting='soft', weights=[2,1,1
Fit the estimators.
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
Target values.
Return class labels or probabilities for each estimator.
Return predictions for X for each estimator.
Input samples.
Target values (None for unsupervised transformations).
Get output feature names for transformation.
Not used, present here for API consistency by convention.
Transformed feature names.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
Get the parameters of an estimator from the ensemble.
Returns the parameters given in the constructor as well as the
estimators contained within the estimators parameter.
Setting it to True gets the various estimators and the parameters of the estimators as well.
Dictionary to access any fitted sub-estimators by name.
Predict class labels for X.
The input samples.
Predicted class labels.
Compute probabilities of possible outcomes for samples in X.
The input samples.
Weighted average probability for each class per sample.
Return accuracy on provided data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Test samples.
True labels for .
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
Configure output of transform and .
Set the parameters of an estimator from the ensemble.
Valid parameter keys can be listed with get_params(). Note that you
can directly set the parameters of the estimators contained in
estimators.
Specific parameters using e.g.
set_params(parameter_name=new_value). In addition, to setting the
parameters of the estimator, the individual estimator of the
estimators can also be set, or can be removed by setting them to
‘drop’.
Configure whether metadata should be requested to be passed to the method.
Return class labels or probabilities for X for each estimator.
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
and :Visualizing the probabilistic predictions of a VotingClassifier
Added in version 0.20.
LabelEncoderTransformer used to encode the labels during fit and decode during
prediction. Sub-estimators in estimators_ are fitted on the
integer-encoded labels produced by this encoder.
The classes labels.
n_features_in_ : intNumber of features seen during fit.
n_features_in_,)Names of features seen during fit. Only defined if the underlying estimators expose such an attribute when fit.
Parameters to pass to the underlying estimators.
Returns the instance itself.
Additional fit parameters.
Transformed array.
Parameter and estimator names mapped to their values or parameter names mapped to their values.
Sample weights.
Mean accuracy of self.predict(X) w.r.t. y.
"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Estimator instance.
Note that this method is only relevant when this estimator is used as a
sub-estimator within a meta-estimator and metadata routing is enabled
with enable_metadata_routing=True (see sklearn.set_config).
Please check the User Guide on how the routing
mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.
False: metadata is not requested and the meta-estimator will not pass it to score.
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED) retains the
existing request. This allows you to change the request for some
parameters and not others.
Metadata routing for sample_weight parameter in score.
The updated object.
flatten_transform=Truereturns ndarray of shape (n_samples, n_classifiers * n_classes), being class probabilities calculated by each classifier.
voting='soft' and `flatten_transform=False:ndarray of shape (n_classifiers, n_samples, n_classes)
voting='hard':ndarray of shape (n_samples, n_classifiers), being class labels predicted by each classifier.