scikit-learn/sklearn/ensemble/voting_classifier.py

225 lines
7.8 KiB
Python
Raw Normal View History

2015-04-21 07:27:30 +08:00
"""
Soft Voting/Majority Rule classifier.
This module contains a Soft Voting/Majority Rule classifier for
classification estimators.
"""
2015-05-08 16:10:47 +08:00
# Authors: Sebastian Raschka <se.raschka@gmail.com>,
# Gilles Louppe <g.louppe@gmail.com>
2015-04-21 07:27:30 +08:00
#
# Licence: BSD 3 clause
2015-05-08 16:10:47 +08:00
import numpy as np
2015-04-21 07:27:30 +08:00
from ..base import BaseEstimator
from ..base import ClassifierMixin
from ..base import TransformerMixin
from ..base import clone
from ..preprocessing import LabelEncoder
from ..externals import six
class VotingClassifier(BaseEstimator, ClassifierMixin, TransformerMixin):
2015-05-08 16:10:47 +08:00
"""Soft Voting/Majority Rule classifier for unfitted estimators.
2015-04-21 07:27:30 +08:00
2015-06-03 12:24:04 +08:00
Read more in the :ref:`User Guide <voting_classifier>`.
2015-04-21 07:27:30 +08:00
Parameters
----------
estimators : list of (string, estimator) tuples
2015-05-08 16:10:47 +08:00
Invoking the `fit` method on the `VotingClassifier` will fit clones
of those original estimators that will be stored in the class attribute
`self.estimators_`.
2015-04-21 07:27:30 +08:00
voting : str, {'hard', 'soft'} (default='hard')
2015-05-08 16:10:47 +08:00
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 probalities, which is recommended for
an ensemble of well-calibrated classifiers.
2015-04-21 07:27:30 +08:00
weights : array-like, shape = [n_classifiers], optional (default=`None`)
2015-05-08 16:10:47 +08:00
Sequence of weights (`float` or `int`) to weight the occurances of
predicted class labels (`hard` voting) or class probabilities
before averaging (`soft` voting). Uses uniform weights if `None`.
2015-04-21 07:27:30 +08:00
Attributes
----------
classes_ : array-like, shape = [n_predictions]
Examples
--------
>>> import numpy as np
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.naive_bayes import GaussianNB
>>> from sklearn.ensemble import RandomForestClassifier
>>> clf1 = LogisticRegression(random_state=1)
>>> clf2 = RandomForestClassifier(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])
>>> eclf1 = VotingClassifier(estimators=[
... ('lr', clf1), ('rf', clf2), ('gnb', clf3)], voting='hard')
>>> eclf1 = eclf1.fit(X, y)
>>> print(eclf1.predict(X))
[1 1 1 2 2 2]
>>> eclf2 = VotingClassifier(estimators=[
... ('lr', clf1), ('rf', clf2), ('gnb', clf3)],
... voting='soft')
>>> eclf2 = eclf2.fit(X, y)
>>> print(eclf2.predict(X))
[1 1 1 2 2 2]
>>> eclf3 = VotingClassifier(estimators=[
... ('lr', clf1), ('rf', clf2), ('gnb', clf3)],
... voting='soft', weights=[2,1,1])
>>> eclf3 = eclf3.fit(X, y)
>>> print(eclf3.predict(X))
[1 1 1 2 2 2]
>>>
"""
def __init__(self, estimators, voting='hard', weights=None):
self.estimators = estimators
self.named_estimators = dict(estimators)
self.voting = voting
self.weights = weights
def fit(self, X, y):
""" Fit the estimators.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.
Returns
-------
self : object
"""
if isinstance(y, np.ndarray) and len(y.shape) > 1 and y.shape[1] > 1:
2015-05-08 16:10:47 +08:00
raise NotImplementedError('Multilabel and multi-output'
2015-04-21 07:27:30 +08:00
' classification is not supported.')
if self.voting not in ('soft', 'hard'):
raise ValueError("Voting must be 'soft' or 'hard'; got (voting=%r)"
2015-05-08 16:10:47 +08:00
% self.voting)
2015-04-21 07:27:30 +08:00
if self.weights and len(self.weights) != len(self.estimators):
raise ValueError('Number of classifiers and weights must be equal'
'; got %d weights, %d estimators'
% (len(self.weights), len(self.estimators)))
self.le_ = LabelEncoder()
self.le_.fit(y)
self.classes_ = self.le_.classes_
self.estimators_ = []
2015-05-08 16:10:47 +08:00
2015-04-21 07:27:30 +08:00
for name, clf in self.estimators:
fitted_clf = clone(clf).fit(X, self.le_.transform(y))
self.estimators_.append(fitted_clf)
2015-05-08 16:10:47 +08:00
2015-04-21 07:27:30 +08:00
return self
def predict(self, X):
""" Predict class labels for X.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
Returns
----------
maj : array-like, shape = [n_samples]
Predicted class labels.
"""
if self.voting == 'soft':
maj = np.argmax(self.predict_proba(X), axis=1)
2015-05-08 16:10:47 +08:00
2015-04-21 07:27:30 +08:00
else: # 'hard' voting
predictions = self._predict(X)
2015-06-03 12:24:04 +08:00
maj = np.apply_along_axis(lambda x:
2015-04-21 07:27:30 +08:00
np.argmax(np.bincount(x,
weights=self.weights)),
axis=1,
arr=predictions)
maj = self.le_.inverse_transform(maj)
2015-05-08 16:10:47 +08:00
2015-04-21 07:27:30 +08:00
return maj
2015-05-08 16:10:47 +08:00
def _collect_probas(self, X):
2015-05-08 16:10:47 +08:00
"""Collect results from clf.predict calls. """
return np.asarray([clf.predict_proba(X) for clf in self.estimators_])
def _predict_proba(self, X):
2015-05-08 16:10:47 +08:00
"""Predict class probabilities for X in 'soft' voting """
avg = np.average(self._collect_probas(X), axis=0, weights=self.weights)
return avg
2015-04-21 07:27:30 +08:00
@property
def predict_proba(self):
2015-05-08 16:10:47 +08:00
"""Compute probabilities of possible outcomes for samples in X.
2015-04-21 07:27:30 +08:00
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
Returns
----------
avg : array-like, shape = [n_samples, n_classes]
Weighted average probability for each class per sample.
"""
2015-05-02 06:45:46 +08:00
if self.voting == 'hard':
raise AttributeError("predict_proba is not available when"
2015-05-08 16:10:47 +08:00
" voting=%r" % self.voting)
return self._predict_proba
2015-05-08 16:10:47 +08:00
2015-04-21 07:27:30 +08:00
def transform(self, X):
2015-05-08 16:10:47 +08:00
"""Return class labels or probabilities for X for each estimator.
2015-04-21 07:27:30 +08:00
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
Returns
-------
If `voting='soft'`:
array-like = [n_classifiers, n_samples, n_classes]
Class probabilties calculated by each classifier.
If `voting='hard'`:
array-like = [n_classifiers, n_samples]
Class labels predicted by each classifier.
"""
if self.voting == 'soft':
return self._collect_probas(X)
2015-04-21 07:27:30 +08:00
else:
return self._predict(X)
def get_params(self, deep=True):
2015-05-08 16:10:47 +08:00
"""Return estimator parameter names for GridSearch support"""
2015-04-21 07:27:30 +08:00
if not deep:
return super(VotingClassifier, self).get_params(deep=False)
else:
out = super(VotingClassifier, self).get_params(deep=False)
out.update(self.named_estimators.copy())
2015-04-21 07:27:30 +08:00
for name, step in six.iteritems(self.named_estimators):
for key, value in six.iteritems(step.get_params(deep=True)):
out['%s__%s' % (name, key)] = value
return out
def _predict(self, X):
2015-05-08 16:10:47 +08:00
"""Collect results from clf.predict calls. """
2015-04-21 07:27:30 +08:00
return np.asarray([clf.predict(X) for clf in self.estimators_]).T