You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SelectFromModel.transform is very slow: > 0.25 seconds to run on a single row #7478
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.
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.
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?
Activity
jnothman commentedon Sep 24, 2016
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.restrictcan 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 functionget_feature_importances(). Given the design flaw, should we be memoising those feature importances so that only the first call is slow?hasattris as expensive as getting the attribute. As long asfeature_importances_(or, indeed,coef_) may be a descriptor we shouldn't ever be usinghasattrwhen what we mean isgetattr(obj, feature_importances_, DUMMY).This bug appears inFixed in [MRG + 1] Fix _get_importances. #7487SelectFromModelandRFE.SelectFromModelcalculates the support mask, and even the effective threshold, attransformtime, 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?jnothman commentedon Nov 7, 2016
@ClimbsRocks, could you please confirm to what extent you feel this issue has been fixed?
jnothman commentedon Jun 28, 2017
Ping @ClimbsRocks