-
Notifications
You must be signed in to change notification settings - Fork 3
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
Adding make_column_transformer #14
Open
wlandecker
wants to merge
2
commits into
jnothman:master
Choose a base branch
from
wlandecker:add_column_transformer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
from collections import defaultdict as _defaultdict | ||
import itertools as _itertools | ||
|
||
from sklearn.compose import ColumnTransformer as _ColumnTransformer | ||
from sklearn.model_selection import GridSearchCV as _GridSearchCV | ||
from sklearn.pipeline import Pipeline as _Pipeline | ||
from sklearn.pipeline import FeatureUnion as _FeatureUnion | ||
|
@@ -147,7 +148,7 @@ def _name_steps(steps, default='alt'): | |
if len(estimators) > 1: | ||
while None in estimators: | ||
estimators.remove(None) | ||
step_names = {type(estimator).__name__.lower() | ||
step_names = {_name_of_estimator(estimator) | ||
for estimator in estimators} | ||
if len(step_names) > 1: | ||
names.append(default) | ||
|
@@ -173,6 +174,19 @@ def _name_steps(steps, default='alt'): | |
return named_steps, grid | ||
|
||
|
||
def _name_of_estimator(estimator): | ||
if isinstance(estimator, tuple): | ||
# tuples comes from ColumnTransformers. At the moment, sklearn accepts | ||
# both (estimator, list_of_columns) and (list_of_columns, estimator) | ||
tuple_types = {type(tuple_entry) for tuple_entry in estimator} | ||
tuple_types.discard(list) | ||
estimator_type = tuple_types.pop() | ||
else: | ||
estimator_type = type(estimator) | ||
|
||
return estimator_type.__name__.lower() | ||
|
||
|
||
def make_pipeline(*steps, **kwargs): | ||
"""Construct a Pipeline with alternative estimators to search over | ||
|
||
|
@@ -257,3 +271,33 @@ def make_union(*transformers, **kwargs): | |
""" | ||
steps, grid = _name_steps(transformers) | ||
return set_grid(_FeatureUnion(steps, **kwargs), **grid) | ||
|
||
|
||
def make_column_transformer(*transformers, **kwargs): | ||
"""Construct a ColumnTransformer with alternative estimators to search over | ||
|
||
Parameters | ||
---------- | ||
steps | ||
Each step is specified as one of: | ||
|
||
* an (estimator, [column_names]) or ([column_names], estimator) tuple | ||
* None (meaning no features) | ||
* a list of the above, indicating that a grid search should alternate | ||
over the estimators (or None) in the list | ||
kwargs | ||
Keyword arguments to the constructor of | ||
:class:`sklearn.pipeline.FeatureUnion`. | ||
|
||
Notes | ||
----- | ||
Each step is named according to the set of estimator types in its list: | ||
|
||
* if a step has only one type of estimator (disregarding None), it takes | ||
that estimator's class name (lowercased) | ||
* if a step has estimators of mixed type, the step is named 'alt' | ||
* if there are multiple steps of the same name using the above rules, | ||
a suffix '-1', '-2', etc. is added. | ||
""" | ||
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. Please add a usage example. |
||
steps, grid = _name_steps(transformers) | ||
return set_grid(_ColumnTransformer(steps, **kwargs), **grid) |
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.
Let's make this code scikit-learn 0.19-compatible by importing this in
make_column_transformer
and skipping the corresponding tests in old versions.