-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
WIP: extend Pipeline transform and inverse_transform behavior #2561
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
Closed
Closed
Changes from all commits
Commits
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -138,8 +138,10 @@ def fit_transform(self, X, y=None, **fit_params): | |
Xt, fit_params = self._pre_transform(X, y, **fit_params) | ||
if hasattr(self.steps[-1][-1], 'fit_transform'): | ||
return self.steps[-1][-1].fit_transform(Xt, y, **fit_params) | ||
else: | ||
elif hasattr(self.steps[-1][-1], 'transform'): | ||
return self.steps[-1][-1].fit(Xt, y, **fit_params).transform(Xt) | ||
else: | ||
return Xt | ||
|
||
def predict(self, X): | ||
"""Applies transforms to the data, and the predict method of the | ||
|
@@ -175,19 +177,43 @@ def predict_log_proba(self, X): | |
return self.steps[-1][-1].predict_log_proba(Xt) | ||
|
||
def transform(self, X): | ||
"""Applies transforms to the data, and the transform method of the | ||
final estimator. Valid only if the final estimator implements | ||
transform.""" | ||
"""Applies transforms to the data. | ||
|
||
All the estimators in the pipeline need to implement | ||
a transform, except for the final one that is ignored | ||
in the case it lacks the method. | ||
|
||
Calling transform in that case can be useful to extract | ||
the features before the last step for debugging purposes. | ||
""" | ||
Xt = X | ||
for name, transform in self.steps: | ||
# test if the last step implements a transform method | ||
steps = self.steps | ||
if not hasattr(self.steps[-1][-1], 'transform'): | ||
steps = self.steps[:-1] | ||
for name, transform in steps: | ||
Xt = transform.transform(Xt) | ||
return Xt | ||
|
||
def inverse_transform(self, X): | ||
"""Applies inverse transforms to the data. | ||
|
||
All the estimators in the pipeline need to implement | ||
an inverse transform, except for the final one that | ||
is ignored in the case it lacks the method. | ||
|
||
As an example, calling inverse transform can be useful to | ||
map back to the original space the `coef_` attribute | ||
from a linear classifier. | ||
""" | ||
if X.ndim == 1: | ||
X = X[None, :] | ||
Xt = X | ||
for name, step in self.steps[::-1]: | ||
# test the last step implements an inverse_transform method | ||
inverse_steps = self.steps[::-1] | ||
if not hasattr(self.steps[-1][-1], 'inverse_transform'): | ||
inverse_steps = self.steps[:-1][::-1] | ||
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. Could you please edit the docstring of the |
||
for name, step in inverse_steps: | ||
Xt = step.inverse_transform(Xt) | ||
return Xt | ||
|
||
|
This file contains hidden or 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.
Please add double back-ticks around code words like
transform
andinverse_transform
.