-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from jnothman/pipeline-minimal
explain_weights in Pipelines: minimal version
- Loading branch information
Showing
8 changed files
with
126 additions
and
1 deletion.
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