Skip to content

Commit

Permalink
verifies fit_predict is available on last step of pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven De Gryze committed Apr 13, 2015
1 parent 01da5c2 commit 16d1322
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
39 changes: 20 additions & 19 deletions sklearn/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,6 @@ def fit(self, X, y=None, **fit_params):
self.steps[-1][-1].fit(Xt, y, **fit_params)
return self

def fit_predict(self, X, y=None, **fit_params):
"""Exposes fit_predict on pipelines.
Fit all the transforms one after the other and transform the
data, then run fit_predict of the final estimator on the transformed
data.
Parameters
----------
X : iterable
Training data. Must fulfill input requirements of first step of
the pipeline.
y : iterable, default=None
Training targets. Must fulfill label requirements for all steps
of the pipeline.
"""
Xt, fit_params = self._pre_transform(X, y, **fit_params)
return self.steps[-1][-1].fit_predict(Xt, y, **fit_params)

def fit_transform(self, X, y=None, **fit_params):
"""Fit all the transforms one after the other and transform the
data, then use fit_transform on transformed data using the final
Expand Down Expand Up @@ -202,6 +183,26 @@ def predict(self, X):
Xt = transform.transform(Xt)
return self.steps[-1][-1].predict(Xt)

@if_delegate_has_method(delegate='_final_estimator')
def fit_predict(self, X, y=None, **fit_params):
"""Exposes fit_predict on pipelines.
Fit all the transforms one after the other and transform the
data, then run fit_predict of the final estimator on the transformed
data.
Parameters
----------
X : iterable
Training data. Must fulfill input requirements of first step of
the pipeline.
y : iterable, default=None
Training targets. Must fulfill label requirements for all steps
of the pipeline.
"""
Xt, fit_params = self._pre_transform(X, y, **fit_params)
return self.steps[-1][-1].fit_predict(Xt, y, **fit_params)

@if_delegate_has_method(delegate='_final_estimator')
def predict_proba(self, X):
"""Applies transforms to the data, and the predict_proba method of the
Expand Down
15 changes: 14 additions & 1 deletion sklearn/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from scipy import sparse

from sklearn.externals.six.moves import zip
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import assert_raises, assert_raises_regex
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_false
from sklearn.utils.testing import assert_true
Expand Down Expand Up @@ -222,6 +222,19 @@ def test_fit_predict_on_pipeline():
assert_array_almost_equal(pipeline_pred, separate_pred)


def test_fit_predict_on_pipeline_without_fit_predict():
# tests that applying fit_predict on pipeline where final step does not
# have fit_predict implemented raises AttributeError
iris = load_iris()
scaler = StandardScaler()
pca = PCA()
pipe = Pipeline([('scaler', scaler), ('pca', pca)])

assert_raises_regex(AttributeError,
"'PCA' object has no attribute 'fit_predict'",
lambda: pipe.fit_predict(iris.data))


def test_feature_union():
# basic sanity check for feature union
iris = load_iris()
Expand Down

0 comments on commit 16d1322

Please sign in to comment.