Skip to content

check_estimator does not play well with Pipelines #9768

@ramhiser

Description

@ramhiser

Description

When calling sklearn.utils.estimator_checks.check_estimator on a simple pipeline, an AttributeError exception is thrown.

Either the check_estimator should be altered to support Pipeline objects or a separate check_pipeline utility should be created.

Steps/Code to Reproduce

from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.utils.estimator_checks import check_estimator

pipe = make_pipeline(
    StandardScaler(),
    LinearRegression()
)

# BOOM!
check_estimator(pipe)

AttributeError: 'LinearRegression' object has no attribute 'transform'

Expected Results

The expected result is to return None, following this example:

foo = check_estimator(LinearRegression())
foo is None
True

Actual Results

Traceback from Jupyter Notebook:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-52-d525f0d3391c> in <module>()
      4 )
      5 
----> 6 check_estimator(pipe)

/opt/conda/lib/python3.5/site-packages/sklearn/utils/estimator_checks.py in check_estimator(Estimator)
    263     for check in _yield_all_checks(name, estimator):
    264         try:
--> 265             check(name, estimator)
    266         except SkipTest as message:
    267             # the only SkipTest thrown currently results from not

/opt/conda/lib/python3.5/site-packages/sklearn/utils/testing.py in wrapper(*args, **kwargs)
    289             with warnings.catch_warnings():
    290                 warnings.simplefilter("ignore", self.category)
--> 291                 return fn(*args, **kwargs)
    292 
    293         return wrapper

/opt/conda/lib/python3.5/site-packages/sklearn/utils/estimator_checks.py in check_fit_score_takes_y(name, estimator_orig)
    812         func = getattr(estimator, func_name, None)
    813         if func is not None:
--> 814             func(X, y)
    815             args = [p.name for p in signature(func).parameters.values()]
    816             if args[0] == "self":

/opt/conda/lib/python3.5/site-packages/sklearn/pipeline.py in fit_transform(self, X, y, **fit_params)
    294             return Xt
    295         else:
--> 296             return last_step.fit(Xt, y, **fit_params).transform(Xt)
    297 
    298     @if_delegate_has_method(delegate='_final_estimator')

AttributeError: 'LinearRegression' object has no attribute 'transform'

Versions

Linux-4.11.6-coreos-r1-x86_64-with-debian-8.8
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
NumPy 1.13.1
SciPy 0.19.1
Scikit-Learn 0.19.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions