Skip to content

Add forward selection to scikit-learn #6545

Closed

Description

@hlin117

I was surprised that scikit-learn doesn't have forward selection, even though it has recursive feature elimination. Are there plans to add forward selection into the API in the near future?

I'm referencing the stackexchange post here.

Activity

  1. agramfort commented on Mar 15, 2016

    @agramfort
    Member
  2. amueller commented on Oct 8, 2016

    @amueller
    Member

    Theres forward and one-step-ahead backward selection in mlxtend by @rasbt. I would kind of like to add all of them, but I would also like to see some empirical evidence (or reference) that they are actually useful.

  3. rasbt commented on Oct 8, 2016

    @rasbt
    Contributor

    Happy to contribute/port my SequentialFeatureSelector from mlxtend -- I could simplify it a bit and send a PR. As reference, maybe

    I remember a better paper from a class that I took back in 2013 where it was compared vs. genetic algorithms and exhaustive search (empirical study) -- the point was that it's close to the optimal solution but not nearly as expensive (have to search my notes).

  4. amueller commented on Oct 8, 2016

    @amueller
    Member

    Was it also compared to lasso and, say random forest based approaches?

  5. rasbt commented on Oct 8, 2016

    @rasbt
    Contributor

    No, it wasn't. Spontaneously, I'd say that it's different enough from either of them to be useful in many applications. I.e., Lasso does not let you control the number of features -- sometimes that's desired. Random forests' feature importance interpretations has problems when there's a high covariance between features. Furthermore, sequential feature selection can be applied to/with any model. So, where are random forest answers "what are the most important features by rank," sequential feature selection kind of addresses more the question of "what is the best combination of features" -- i.e., the coupling/relationship of features. Or in other words, the best subset of features is not necessarily an additive sum of the most important features.

  6. amueller commented on Oct 8, 2016

    @amueller
    Member

    True. It would be nice to have a recent study, but maybe that's a paper to write lol.
    Also lasso totally let's you control the number of features. A bit indirectly, but still...

  7. rasbt commented on Oct 8, 2016

    @rasbt
    Contributor

    True. It would be nice to have a recent study

    we should do some search, but I think this is one of these "trivial" ideas that no one really writes about.

    but maybe that's a paper to write lol.

    haha, empirical benchmark studies sound like a lot of work but are generally easy to write I'd say :D.

    Also lasso totally let's you control the number of features. A bit indirectly, but still...

    hm, yeah, but it's not like "hey, I tell me the 3 most important features in combination." Also, it's for linear models only.

  8. rasbt commented on Oct 10, 2016

    @rasbt
    Contributor

    Here's another study by Kohavi & John that may be relevant: Wrappers for feature subset selection

    The optimal features depend on the specific biases and heuristics of the learning algorithm, and hence the wrapper approach naturally fits with this definition. Feature relevance helped motivate compound operators, which work well in practice and are currently the only practical way to conduct backward searches for feature subsets using the wrapper approach when the datasets have many features.

    Best-first search with compound operators seems to be a strong performer and improves ID3, C4.5, and Naive-Bayes, both in accuracy, and in comprehensibility, as measured by the number of features used.

  9. rasbt commented on Oct 12, 2016

    @rasbt
    Contributor

    I did some more search yesterday, and I found dozens of papers when looking for the terms "forward selection" and "backward elimination." However, these papers were mostly about "new" or "alternative" ways of doing feature selection, i.e., these kind of tiny tweaks or improvements where researchers showed that it could perform better on certain datasets. However, I still think that the idea sequential feature selection has been around for a while and is pretty established these days -- the idea is probably so old that no one really write about it unless they want to demonstrate a point that would favor their particular tweak.

    In any case, would you still think that's worth adding to sklearn?

  10. agramfort commented on Oct 13, 2016

    @agramfort
    Member
  11. rasbt commented on Oct 13, 2016

    @rasbt
    Contributor

    decision of inclusion of new variable is based on p-values would be better in statsmodels

    Typically, it's just using the predictive performance (accuracy, F1, R2, etc.) of the fitted model to include or eliminate features. In my particular implementation these are the average k-fold performances (I think back in the day, resubstitution error or holdout was used more often). I'd think the p-values would be more of an add-on, e.g., post-selection / re-evaluation of the feature sets generated by forward selection/backward elimination

  12. agramfort commented on Oct 13, 2016

    @agramfort
    Member
  13. amueller commented on Oct 14, 2016

    @amueller
    Member

    I'm +1. @GaelVaroquaux @ogrisel @jnothman or anyone else have opinions?

    Feature selection is becoming more and more important. The LIME stuff about understanding machine learning models is basically about extracting feature importances / doing feature selection.

  14. jnothman commented on Oct 15, 2016

    @jnothman
    Member

    I agree feature selection is important and that this kind of approach is common in practice. To the extent that scikit-learn should be a Kaggler's best friend, it should perhaps be included.

    At the same time, I'm feeling the growing maintenance burden of the project. It's becoming urgent that we have a way to help people find estimators outside of the core project that suit their needs. Part of this is a cultural shift: people should know to ask on SO about estimators in the scikit-learn ecosystem rather than in the core project. Part of it is making a directory available as part of contrib, where users can get a list of all estimators supporting feature selection (linked to from our feature selection docs), for instance. This mitigates the need for One Implementation to Rule Them All when that's not appropriate.

    Still not sure that answers the question, though.

  15. 1 remaining item

  16. amueller commented on Oct 17, 2016

    @amueller
    Member

    @jnothman I agree. Though what many people like about python /sklearn compared to R is the "one implementation to rule them all" approach. It's not scalable (without more maintainers), but I think for core methods we should stay with it. The question is what "core" means ;)

  17. rasbt commented on Oct 17, 2016

    @rasbt
    Contributor

    @jnothman Yeah, cluttering the library with non-core stuff is definitely a problem to avoid. I could set up a contrib project for feature selection if that's a better fit (if there isn't already one). I thought that this may be one of these non-fancy "classics" or "essentials," which is why I thought it may be useful to have it in core. I can definitely see the pros and cons on both sides though.

    In any case, I'd be happy to either contribute this to scikit-learn directly (maybe leaving out the "floating" functions and only focussing on plain forward and backward selection) or setting up/adding it to a contrib project for feature selection.

  18. amueller commented on Oct 17, 2016

    @amueller
    Member

    Does the "floating" help in practice?

  19. rasbt commented on Oct 17, 2016

    @rasbt
    Contributor

    Does the "floating" help in practice?

    Yeah, it can potentially be better because it is a tad closer to exhaustive search, yet the trade-off is that it is also a bit more expensive. Btw. I finally found the original paper:

    It's a bit hard to see (not sure why the authors didn't include an "optimal" line in the lower SFFS/SFBS plot), but looks like the predictive performance is a bit better (optimal?) in floating vs. non-floating and the computational cost is only a tiny bit higher (but in practice, this highly depends on the classifier and the number of samples and features in the dataset of course)

    screen shot 2016-10-17 at 4 40 00 pm

    screen shot 2016-10-17 at 4 40 14 pm

  20. amueller commented on Oct 17, 2016

    @amueller
    Member

    I haven't read the paper (should) and I'm kinda on the fence about floating. Isn't "Branch-and-Bound" optimal?

  21. rasbt commented on Oct 18, 2016

    @rasbt
    Contributor

    Ah, you are right, since they already have the bounds, that makes sense! Have to read the paper (again) as well, has been a few years (I went through it in a class a few years ago). Although the floating versions are certainly appealing based on the performance, I'd also suggest focussing only on regular forward and backward selection for simplicity (simpler implementation and less confusing documentation, plus it should be easier to maintain).

  22. GaelVaroquaux commented on Oct 18, 2016

    @GaelVaroquaux
    Member
  23. jnothman commented on Nov 9, 2016

    @jnothman
    Member

    I get the impression that @amueller is playing to the idea that "people will only use it if it's in scikit-learn". I think this is true to a great extent, and it motivates including standard techniques, where they are well-defined and tricky to DIY. I also think it is unfortunate to some extent, and we need to increasingly encourage users to think of scikit-learn as an API, and to help them find high-quality API compliant implementations. I don't think we're doing that enough yet.

    If definition of these forward selection techniques is clear / relatively undisputed, I'd be okay to see them contributed.

  24. rasbt commented on Nov 16, 2016

    @rasbt
    Contributor

    I also think it is unfortunate to some extent, and we need to increasingly encourage users to think of scikit-learn as an API, and to help them find high-quality API compliant implementations. I don't think we're doing that enough yet.

    I agree! However, I think that it's getting "better" these days; there are a lot of people/projects gearing towards implementing additional algorithms and tools with the scikit-learn API in mind, e.g., XGBoost, pomegranate, and Keras to some extent. However, I also think that there's always this certain "user convenience" associated with scikit-learn -- I mean this as a big compliment ;). I.e., everything works well together and out of the box, and there's a level of trust that everything is reasonably well tested, there are deprecation warning and no unexpected changes in behaviors, etc. In any case, this level of convenience for the user also comes with a cost in development and maintenance time; that's for sure. I think that the problem with some contrib projects is that they are not as "stable" or "trustworthy" as scikit-learn, yet, but I believe that it may get better over time.

    But yeah, back to the topic. I am happy to add the forward selection then. Like discussed earlier, I will probably drop the "floating" variants of these algorithms to avoid making it too complicated. I think that having the general sequential feature selection algorithm will be useful as a general option, being a "wrapper-type algorithm," to other feature selection algorithms already implemented. I would leave everything fancier, and all the different flavors of sequential feature selection, to contrib projects.

    I will be a bit busy the next 2 weeks, but I will try to put together a PR sometime early December.

  25. amueller commented on Nov 22, 2016

    @amueller
    Member

    Cool thanks @rasbt :)

  26. rasbt commented on Apr 1, 2017

    @rasbt
    Contributor

    Wow, November ... I had this on my "someday" to-do list, but I procrastinated quite a bit here ... sry! Was an almost boring Fri night and I felt like coding, just submitted an early PR (#8684) for this where we may continue the discussion regarding the SFS. (I know that you are all very busy with 0.18.2 and there's GSoC... so no need to get to this soon since this is prob. low priority for now)

  27. eggie5 commented on Apr 18, 2017

    @eggie5
    Contributor

    @rasbt I noticed Recursive feature elimination (RFE) sort of does this by on models that implement the coef_ i.e. linear models. I think it would be a lot more useful to quantify important features by a validation-set score or CV score. Then it can be used by all models, etc. I was reading about stepwise selection in "An introduction to Statistical Learning" and they suggest this method.

  28. rasbt commented on Apr 18, 2017

    @rasbt
    Contributor

    @eggie5

    The RFE uses model parameters though, whereas the SFS optimizes for an extrinsic metric (e.g., classification accuracy, ROC auc, R^2, etc.). Just glancing at the algorithms in ISLR, it looks identical to Sequential Forward/Backward selection, they just gave it another name (Forward/Backward stepwise selection); so this is what was submitted at #8684 :)

  29. eggie5 commented on Apr 18, 2017

    @eggie5
    Contributor

    @rasbt yes, we are saying the same thing about RFEusing: coef_ and "model parameters". I'm looking forward to #8684 where I can do feature selection using validation set accuracy or R^2 (adjusted R^2 that is, see ISLR p 210 6.1.3)

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions