Implementation:Scikit learn Scikit learn VotingClassifier Init
Appearance
Overview
Concrete tool for combining multiple classifiers via majority or soft voting provided by scikit-learn. The Template:Code fits clones of the provided base estimators and aggregates their predictions using either hard voting (majority class label) or soft voting (averaged predicted probabilities). It supports optional per-classifier weighting to emphasize stronger models.
Constructor Signature
from sklearn.ensemble import VotingClassifier
VotingClassifier(
estimators,
*,
voting="hard",
weights=None,
n_jobs=None,
flatten_transform=True,
verbose=False,
)
Parameters
- estimators (list of (str, estimator) tuples) -- Base estimators to be stacked together. Each element is a tuple of a name string and an estimator instance. An estimator can be set to Template:Code using Template:Code to exclude it from the ensemble.
- voting ({"hard", "soft"}, default="hard") -- If "hard", uses predicted class labels for majority rule voting. If "soft", predicts the class label based on the argmax of the sums of the predicted probabilities (recommended for well-calibrated classifiers).
- weights (array-like of shape (n_classifiers,), default=None) -- Sequence of weights to apply to the predicted class labels (hard voting) or class probabilities (soft voting). If None, uniform weights are used.
- n_jobs (int, default=None) -- Number of jobs to run in parallel for Template:Code. None means 1 unless in a Template:Code context. -1 means using all processors.
- flatten_transform (bool, default=True) -- Only affects the shape of Template:Code output when Template:Code. If True, returns a matrix of shape Template:Code. If False, returns shape Template:Code.
- verbose (bool, default=False) -- If True, prints the time elapsed while fitting each estimator.
Fitted Attributes
- estimators_ -- The collection of fitted sub-estimators (excludes any set to "drop").
- named_estimators_ -- A Template:Code object allowing access to fitted sub-estimators by name.
- le_ -- A Template:Code transformer used to encode labels during fit and decode during prediction.
- classes_ -- The class labels (ndarray of shape Template:Code).
- n_features_in_ -- Number of features seen during Template:Code (only defined if the underlying classifiers expose this attribute).
- feature_names_in_ -- Names of features seen during Template:Code (only defined if the underlying estimators expose this attribute).
Example Usage
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=1)
clf2 = RandomForestClassifier(n_estimators=50, random_state=1)
clf3 = GaussianNB()
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
y = np.array([1, 1, 1, 2, 2, 2])
eclf = VotingClassifier(
estimators=[("lr", clf1), ("rf", clf2), ("gnb", clf3)],
voting="soft",
)
eclf.fit(X, y)
print(eclf.predict(X))
# [1 1 1 2 2 2]
Source Location
Template:Code, class Template:Code (lines 194-540).
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment