-
Notifications
You must be signed in to change notification settings - Fork 331
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
Fix #298 Handle removal of sklearn randomized l1 models #302
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,7 @@ | |
import numpy as np # type: ignore | ||
from sklearn.pipeline import Pipeline, FeatureUnion # type: ignore | ||
from sklearn.feature_selection.base import SelectorMixin # type: ignore | ||
from sklearn.linear_model import ( # type: ignore | ||
RandomizedLogisticRegression, | ||
RandomizedLasso, | ||
) | ||
|
||
from sklearn.preprocessing import ( # type: ignore | ||
MinMaxScaler, | ||
StandardScaler, | ||
|
@@ -21,15 +18,23 @@ | |
|
||
# Feature selection: | ||
|
||
@transform_feature_names.register(SelectorMixin) | ||
@transform_feature_names.register(RandomizedLogisticRegression) | ||
@transform_feature_names.register(RandomizedLasso) | ||
def _select_names(est, in_names=None): | ||
mask = est.get_support(indices=False) | ||
in_names = _get_feature_names(est, feature_names=in_names, | ||
num_features=len(mask)) | ||
return [in_names[i] for i in np.flatnonzero(mask)] | ||
|
||
try: | ||
from sklearn.linear_model import ( # type: ignore | ||
RandomizedLogisticRegression, | ||
RandomizedLasso, | ||
) | ||
_select_names = transform_feature_names.register(RandomizedLasso)(_select_names) | ||
_select_names = transform_feature_names.register(RandomizedLogisticRegression)(_select_names) | ||
except ImportError: # Removed in scikit-learn 0.21 | ||
pass | ||
_select_names = transform_feature_names.register(SelectorMixin)(_select_names) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are correct. I expanded out and placed |
||
|
||
|
||
# Scaling | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had an idea for another approach here, but after some thought it looks worse than what is implemented here, still let me put it here:
RandomizedLasso
etc. toNone
if they fail to import@transform_feature_names.register(RandomizedLasso)
which would do nothing ifRandomizedLasso
isNone
. But it seems that we'd need a wrapper for that, something like@register(transform_feature_names, RandomizedLasso)
Advantage is that the flow would be more natural (imports at the top, decorators above the function). Disadvantage is that it might be a bit confusing that
RandomizedLasso
can beNone
, and also we would need to introduce the wrapper. So to me it seems that it would make sense only if we get a lot of similar conditional registers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestions @lopuhin. Looking at the recently deprecated in sklearn there doesn't seem to be too many removals of existing models. But I would certainly prefer your approach if we face more issues like this down the line.