Skip to content

SelectFromModel.transform is very slow: > 0.25 seconds to run on a single row #7478

Open

Description

@ClimbsRocks

Description

When profiling unexpectedly slow production code, I was shocked to find out that SelectFromModel.transform() took over a quarter of a second to run on a single row, while everything else in the Pipeline took very roughly a microsecond to run on a single row.

We're using this in production with private data, so I can't copy exactly the example we're using. But the dataset that we feed into SelectFromModel.fit has many hundreds of data points, while the pruned version of the dataset after feature selection has only dozens of data points.

The compute time required for SelectFromModel.transform() over tens of thousands of rows is only a fraction of a second longer than the compute time required for a single row. We can probably pre-calculate a lot of what SelectFromModel.transform is calculating on the fly each time.

At some point, I'll dive into the code a little more and submit a PR to speed this up. This slowness would have prevented us from using a pipeline built using scikit-learn in production. Everything else runs very quickly (that's one of the reasons I like scikit-learn so much: a very active community of developers optimizing the performance all the time), but this one bottleneck took orders of magnitude longer than all the other calculations combined when getting a prediction on a single row.

Versions

>>> import platform; print(platform.platform())
Darwin-15.6.0-x86_64-i386-64bit
>>> import sys; print("Python", sys.version)
('Python', '2.7.12 (default, Jun 29 2016, 14:05:02) \n[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)]')
>>> import numpy; print("NumPy", numpy.__version__)
('NumPy', '1.11.1')
>>> import scipy; print("SciPy", scipy.__version__)
('SciPy', '0.18.0')
>>> import sklearn; print("Scikit-Learn", sklearn.__version__)
('Scikit-Learn', '0.17.1')

Thanks, as always, for running such an awesome project! Hopefully this will help speed up other peoples' production code as well, and encourage even wider adoption of scikit-learn. It seems a shame to have such a highly optimized library bottlenecked by such a primitive piece of functionality.

Activity

  1. jnothman commented on Sep 24, 2016

    @jnothman
    Member

    A very interesting problem, thanks!

    In terms of the bigger picture, in production, feature selection routines will often be better off rewritten to compress the data as early as possible / avoid generating certain features. Our feature selectors do not feed back to the feature extraction process, so cannot do this but, for instance, DictVectorizer.restrict can help doing this in a custom way and it would be interesting to have examples of this in the library, at least while we don't have an API-integrated way to do it. (I now see that ClimbsRocks/auto_ml#91 may take this approach, although I've not looked at the code as it's not linked from the issue.)

    Also, in production, feature selection routines among others would be better off being run by a PMML executor similar that only takes the functional abstraction of a model without worrying about the learning machinery or parametrisation.

    But you're right that we may be doing a few things wrong:

    • feature_importances_ is in some cases defined as a property, and such descriptors should ideally be quick to run. (A user presumes that getting an attribute is a quick operation.) This is not the case for some ensembles. This probably an API design flaw: feature_importances_ should be a function get_feature_importances(). Given the design flaw, should we be memoising those feature importances so that only the first call is slow?
    • hasattr is as expensive as getting the attribute. As long as feature_importances_ (or, indeed, coef_) may be a descriptor we shouldn't ever be using hasattr when what we mean is getattr(obj, feature_importances_, DUMMY). This bug appears in SelectFromModel and RFE. Fixed in [MRG + 1] Fix _get_importances. #7487
    • SelectFromModel calculates the support mask, and even the effective threshold, at transform time, which is really anti-conventional, but something that most of our feature selection tools have done for a long time. Is there a way to memoize this to help?
  2. jnothman commented on Nov 7, 2016

    @jnothman
    Member

    @ClimbsRocks, could you please confirm to what extent you feel this issue has been fixed?

  3. jnothman commented on Jun 28, 2017

    @jnothman
    Member
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