Skip to content

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Changelog
- Added :class:`linear_model.RANSACRegressor` meta-estimator for the robust
fitting of regression models. By Johannes Schönberger.

- Extended transform and inverse_transform methods from
Copy link
Member

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 and inverse_transform.

:class:`pipeline.Pipeline` to ignore the last step if it does not
implement the corresponding methods, by `Yannick Schwartz`_.


API changes summary
-------------------
Expand Down
38 changes: 32 additions & 6 deletions sklearn/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please edit the docstring of the inverse_transform method to explain that it works even if the last step does not support the inverse_transform method and give motivation for this feature as done for the forward case?

for name, step in inverse_steps:
Xt = step.inverse_transform(Xt)
return Xt

Expand Down
9 changes: 9 additions & 0 deletions sklearn/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,28 @@ def test_pipeline_transform():
# Also test pipeline.transform and pipeline.inverse_transform
iris = load_iris()
X = iris.data
y = iris.target
pca = PCA(n_components=2)
clf = SVC()
pipeline = Pipeline([('pca', pca)])
pipeline2 = Pipeline([('pca', pca), ('clf', clf)])

# test transform and fit_transform:
X_trans = pipeline.fit(X).transform(X)
X_trans2 = pipeline.fit_transform(X)
X_trans3 = pca.fit_transform(X)
X_trans4 = pipeline2.fit(X, y).transform(X)
X_trans5 = pipeline2.fit_transform(X, y)
assert_array_almost_equal(X_trans, X_trans2)
assert_array_almost_equal(X_trans, X_trans3)
assert_array_almost_equal(X_trans, X_trans4)
assert_array_almost_equal(X_trans, X_trans5)

X_back = pipeline.inverse_transform(X_trans)
X_back2 = pca.inverse_transform(X_trans)
X_back3 = pipeline2.inverse_transform(X_trans)
assert_array_almost_equal(X_back, X_back2)
assert_array_almost_equal(X_back, X_back3)


def test_pipeline_fit_transform():
Expand Down