Esempio n. 1
0
 def __init__(self, clfs, voting, weights=None, threshold=None):
     self.clfs = clfs
     self.named_clfs = {key:value for key,value in _name_estimators(clfs)}
     self.voting=voting
     if voting is 'weighted':
         self.combiner=WeightedVote(weights=weights, threshold=threshold)
     elif voting is 'majority':
         self.combiner=MajorityVote()
     else:
         raise AttributeError('Unrecognized voting method')
Esempio n. 2
0
class EnsembleSKLearn(BaseEstimator, ClassifierMixin, TransformerMixin):
    def __init__(self, clfs, voting, weights=None, threshold=None):
        self.clfs = clfs
        self.named_clfs = {key:value for key,value in _name_estimators(clfs)}
        self.voting=voting
        if voting is 'weighted':
            self.combiner=WeightedVote(weights=weights, threshold=threshold)
        elif voting is 'majority':
            self.combiner=MajorityVote()
        else:
            raise AttributeError('Unrecognized voting method')

    def fit(self, X, y):
        self.clfs_ = []
        for clf in self.clfs:
            fitted_clf = clone(clf).fit(X, y)
            self.clfs_.append(fitted_clf)
        return self

    def predict(self, X):
        return self._predict(X)

    def _predict(self, X):
        """ Collect results from clf.predict calls. """
        predictions =  np.asarray([clf.predict(X) for clf in self.clfs_]).T
        predicted_labels = self.combiner.combine(predictions)
        return predicted_labels

    def get_params(self, deep=True):
        """ Return estimator parameter names for GridSearch support"""
        if not deep:
            return super(EnsembleSKLearn, self).get_params(deep=False)
        else:
            out = self.named_clfs.copy()
            for name, step in six.iteritems(self.named_clfs):
                for key, value in six.iteritems(step.get_params(deep=True)):
                    out['%s__%s' % (name, key)] = value
            return out