2

I am trying to fit an EnsembleVoteClassifier according to mlxtend documentation

For normal grid.fit I can use fit_params to set sample_weight, but with the VotingClassifier it does not work. How can this be solved?

from sklearn import datasets
iris = datasets.load_iris()
X, y = iris.data[:, :], iris.target
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB 
from sklearn.ensemble import RandomForestClassifier
from mlxtend.classifier import EnsembleVoteClassifier
from sklearn.pipeline import Pipeline
from mlxtend.feature_selection import SequentialFeatureSelector
clf1 = LogisticRegression(random_state=1)
clf2 = RandomForestClassifier(random_state=1)
clf3 = GaussianNB()
'''Creating a feature-selection-classifier pipeline'''
sfs1 = SequentialFeatureSelector(clf1, 
                                 k_features=4,
                                 forward=True, 
                                 floating=False, 
                                 scoring='accuracy',
                                 verbose=0,
                                 cv=0)
clf1_pipe = Pipeline([('sfs', sfs1),
                      ('logreg', clf1)])
eclf = EnsembleVoteClassifier(clfs=[clf1_pipe, clf2, clf3], 
                              voting='soft')
params = {'pipeline__sfs__k_features': [1, 2, 3],
          'pipeline__logreg__C': [1.0, 100.0],
          'randomforestclassifier__n_estimators': [20, 200]}
grid = GridSearchCV(estimator=eclf, param_grid=params, cv=5)
sample_weights = [1] * len(iris.target)
grid.fit(iris.data, iris.target,**{'pipeline__logreg__sample_weight': sample_weights})
double-beep
  • 5,031
  • 17
  • 33
  • 41
user670186
  • 2,588
  • 6
  • 37
  • 55
  • Try [`VotingClassifier` from scikit](http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.VotingClassifier.html#sklearn.ensemble.VotingClassifier.fit) instead of `EnsembleVoteClassifier` – Vivek Kumar Mar 29 '18 at 10:57
  • 1
    thanks, but what do I do when I have a StackingClassifier https://rasbt.github.io/mlxtend/user_guide/classifier/StackingClassifier/ – user670186 Mar 29 '18 at 11:11
  • Then you need to extend that to a custom class yourself and add the param. – Vivek Kumar Mar 29 '18 at 11:13
  • 1
    I tried it with VotingClassifier, I get ValueError: Underlying estimator 'clf1_pipe' does not support sample weights. – user670186 Mar 29 '18 at 11:29

0 Answers0