-
Notifications
You must be signed in to change notification settings - Fork 333
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bc4c20b
explain_weights in Pipelines through transform_feature_names singledi…
jnothman 90142b3
More comment on purpose in transform_feature_names docs
jnothman abd3fa3
Ignore Pipeline type
jnothman 1a9e428
Register for both explain_weights variants
jnothman c3731bf
Merge branch 'master' into pipeline-minimal
kmike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ | |
FeatureUnhasher, | ||
invert_hashing_and_fit, | ||
) | ||
from . import transform as _ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 # type: ignore | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""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 | ||
|
||
Used by :func:`explain_weights` when applied to a scikit-learn Pipeline, | ||
this ``singledispatch`` should be registered with custom name | ||
transformations for each class of transformer. | ||
|
||
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?