Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MRG + 1] FIX Calling fit_transform instead of transform in Pipeline's fit_predict #7585

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

Merged
merged 3 commits into from
Oct 6, 2016
Merged
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
2 changes: 1 addition & 1 deletion sklearn/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def fit_predict(self, X, y=None, **fit_params):
Xt = X
for name, transform in self.steps[:-1]:
if transform is not None:
Xt = transform.transform(Xt)
Xt = transform.fit_transform(Xt)
return self.steps[-1][-1].fit_predict(Xt, y, **fit_params)

@if_delegate_has_method(delegate='_final_estimator')
Expand Down
9 changes: 8 additions & 1 deletion sklearn/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,20 @@ def test_fit_predict_on_pipeline():
iris = load_iris()
scaler = StandardScaler()
km = KMeans(random_state=0)
# As pipeline doesn't clone estimators on construction,
# it must have its own estimators
scaler_for_pipeline = StandardScaler()
km_for_pipeline = KMeans(random_state=0)

# first compute the transform and clustering step separately
scaled = scaler.fit_transform(iris.data)
separate_pred = km.fit_predict(scaled)

# use a pipeline to do the transform and clustering in one step
pipe = Pipeline([('scaler', scaler), ('Kmeans', km)])
pipe = Pipeline([
('scaler', scaler_for_pipeline),
('Kmeans', km_for_pipeline)
])
pipeline_pred = pipe.fit_predict(iris.data)

assert_array_almost_equal(pipeline_pred, separate_pred)
Expand Down