Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also allow tuning of the Stacking parameters #253

Closed
rasbt opened this issue Sep 19, 2017 · 2 comments · Fixed by #254
Closed

Also allow tuning of the Stacking parameters #253

rasbt opened this issue Sep 19, 2017 · 2 comments · Fixed by #254

Comments

@rasbt
Copy link
Owner

rasbt commented Sep 19, 2017

Currently, only the parameters of the first and meta-level classifiers in the stacking implementations can currently be tuned. E.g., the following works

sclf = StackingCVClassifier(classifiers=[clf1, clf2], 
                            meta_classifier=lr)

params = {'use_probas': [True, False],  # add the use_probas parameter to the search space
          'kneighborsclassifier__n_neighbors': [1, 5],
          'randomforestclassifier__n_estimators': [10, 50],
          'meta-logisticregression__C': [0.1, 10.0]}

grid = GridSearchCV(estimator=sclf, 
                    param_grid=params, 
                    cv=5,
                    refit=True)

but the use_probas param of the StackingCVClassifier cannot be tuned; the following code will produce an error.

sclf = StackingCVClassifier(classifiers=[clf1, clf2], 
                            meta_classifier=lr)

params = {'use_probas': [True, False],  # add the use_probas parameter to the search space
          'kneighborsclassifier__n_neighbors': [1, 5],
          'randomforestclassifier__n_estimators': [10, 50],
          'meta-logisticregression__C': [0.1, 10.0],
          'use_probas': [True, False]}

grid = GridSearchCV(estimator=sclf, 
                    param_grid=params, 
                    cv=5,
                    refit=True)

Changing

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

            out.update(self.named_meta_classifier.copy())
            for name, step in six.iteritems(self.named_meta_classifier):
                for key, value in six.iteritems(step.get_params(deep=True)):
                    out['%s__%s' % (name, key)] = value
            return out

to

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

            out.update(self.named_meta_classifier.copy())
            for name, step in six.iteritems(self.named_meta_classifier):
                for key, value in six.iteritems(step.get_params(deep=True)):
                    out['%s__%s' % (name, key)] = value
                    
            for key, value in six.iteritems(super(StackingCVClassifier, self).get_params(deep=False)):
                if key in ('classifiers', 'meta-classifier'):
                    continue
                else:
                    out['%s' % key] = value
                
            return out

in the StackingCVClassifier would solve the issue though. Similar modification should be made to the other stacking classifier, the stacking regressors, and the EnsembleVoteClassifier.

@rasbt rasbt added the Bug label Sep 19, 2017
@jrbourbeau
Copy link
Contributor

Hey @rasbt, I can work on this issue

@rasbt
Copy link
Owner Author

rasbt commented Sep 20, 2017

Oh yeah, thanks, that would be nice. I think it's not much, just the replacement like I outlined above (plus an entry in the changelog and inclusion in the unit tests :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants