Balance model weaknesses
Several different classifiers have comparable performance but make different errors.
Scikit-Learn · Class
VotingClassifier combines named classifiers by majority labels or aggregated probabilities. It supports hard and soft voting, optional classifier weights, and parallel fitting.
VotingClassifier clones and fits the supplied named classifiers, then combines their outputs. Hard voting selects the majority class label, while soft voting selects the class with the largest sum of predicted probabilities and is intended for well-calibrated classifiers.
class sklearn.ensemble.VotingClassifier(*estimators*, ***, voting='hard', weights=None, n_jobs=None, flatten_transform=True, verbose=False)| Name | Type | Description |
|---|---|---|
estimators | list of (str, estimator) tuples | Named estimator pairs to be cloned and fitted. |
voting | {'hard', 'soft'}, default='hard' | Voting rule: hard majority labels or soft probabilities; default is hard. |
weights | array-like of shape (n_classifiers,), default=None | Numeric classifier weights; default is None. |
n_jobs | int, default=None | Parallel jobs used during fit; default is None. |
flatten_transform | bool, default=True | Whether transform output is flattened; default is True. |
verbose | bool, default=False | Print elapsed fitting time when true; default is False. |
A fitted VotingClassifier with prediction, probability, scoring, and transformation methods documented on the API page.
from sklearn.datasets import load_iris
from sklearn.ensemble import VotingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
X, y = load_iris(return_X_y=True)
clf = VotingClassifier(
estimators=[
("lr", make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))),
("svc", make_pipeline(StandardScaler(), SVC(probability=True))),
],
voting="soft",
)
clf.fit(X, y)
print(clf.predict(X[:3]))Trains two probability-producing classifiers and combines their predictions with soft voting.
Several different classifiers have comparable performance but make different errors.
Class probabilities are meaningful and calibrated across component models.
Some classifiers should contribute more strongly than others.
It combines multiple unfitted classifiers using either majority labels or probabilities. With hard voting, the selected class is the mode of the component predictions; with soft voting, it is the class corresponding to the largest sum of predicted probabilities.
Follow-up: How would classifier importance be changed? Supply weights; they weight hard-vote label occurrences or soft-vote probabilities before aggregation.
The estimators argument is a list of name-estimator pairs. Calling fit clones those original estimators and stores the fitted clones in estimators_.
Follow-up: Why are names needed? They identify estimators for parameter access and model configuration in the estimator interface.
weights accepts one numeric weight per classifier. If it is None, all classifiers receive uniform weight; otherwise weights influence label counts in hard voting or probability averaging in soft voting.
Follow-up: What does n_jobs control? It controls the number of parallel jobs used during fit; None means one job unless a joblib backend changes the context, and -1 uses all processors.
VotingRegressorClassifierMixinPipelineGridSearchCV