-
Notifications
You must be signed in to change notification settings - Fork 332
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
explain_weights in Pipelines: minimal version #177
Changes from 1 commit
bc4c20b
90142b3
abd3fa3
1a9e428
c3731bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import numpy as np # type: ignore | ||
|
||
from sklearn.base import BaseEstimator, RegressorMixin # type: ignore | ||
from sklearn.pipeline import Pipeline # type: ignore | ||
from sklearn.linear_model import ( # type: ignore | ||
ElasticNet, # includes Lasso, MultiTaskElasticNet, etc. | ||
ElasticNetCV, | ||
|
@@ -61,6 +62,7 @@ | |
get_default_target_names, | ||
) | ||
from eli5.explain import explain_weights | ||
from eli5.transform import transform_feature_names | ||
from eli5._feature_importances import ( | ||
get_feature_importances_filtered, | ||
get_feature_importance_explanation, | ||
|
@@ -422,3 +424,17 @@ def _features(target_id): | |
method='linear model', | ||
is_regression=True, | ||
) | ||
|
||
|
||
@explain_weights.register(Pipeline) | ||
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. After #170 it should be just |
||
def explain_weights_pipeline(estimator, feature_names=None, **kwargs): | ||
last_estimator = estimator.steps[-1][1] | ||
transform_pipeline = Pipeline(estimator.steps[:-1]) | ||
if 'vec' in kwargs: | ||
feature_names = get_feature_names(feature_names, vec=kwargs.pop('vec')) | ||
feature_names = transform_feature_names(transform_pipeline, feature_names) | ||
out = explain_weights(last_estimator, | ||
feature_names=feature_names, | ||
**kwargs) | ||
out.estimator = repr(estimator) | ||
return out |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""transform_feature_names implementations for scikit-learn transformers | ||
""" | ||
|
||
import numpy as np # type: ignore | ||
from sklearn.pipeline import Pipeline | ||
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. it seems |
||
from sklearn.feature_selection.base import SelectorMixin # type: ignore | ||
|
||
from eli5.transform import transform_feature_names | ||
from eli5.sklearn.utils import get_feature_names as _get_feature_names | ||
|
||
|
||
# Feature selection: | ||
|
||
@transform_feature_names.register(SelectorMixin) | ||
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)] | ||
|
||
|
||
# Pipelines | ||
|
||
@transform_feature_names.register(Pipeline) | ||
def _pipeline_names(est, in_names=None): | ||
names = in_names | ||
for name, trans in est.steps: | ||
if trans is not None: | ||
names = transform_feature_names(trans, names) | ||
return names |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Handling transformation pipelines in explanations""" | ||
|
||
from singledispatch import singledispatch | ||
|
||
|
||
@singledispatch | ||
def transform_feature_names(transformer, in_names=None): | ||
"""Get feature names for transformer output as a function of input names | ||
|
||
Parameters | ||
---------- | ||
transform : scikit-learn-compatible transformer | ||
in_names : list of str, optional | ||
Names for features input to transformer.transform(). | ||
If not provided, the implementation may generate default feature names | ||
if the number of input features is known. | ||
|
||
Returns | ||
------- | ||
feature_names : list of str | ||
""" | ||
if hasattr(transformer, 'get_feature_names'): | ||
return transformer.get_feature_names() | ||
raise NotImplementedError('transform_feature_names not available for ' | ||
'{}'.format(transformer)) |
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.
not certain this belongs here
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.
Yeah, I was also unsure; do you think this function will be used stand-alone, not as an implementation detail of how to make explain.. / show.. functions work?