Skip to content

GradientBoostingRegressor initial estimator does not play together with Pipeline #13466

@Thomasillo

Description

@Thomasillo

Using a pipeline as the initial estimator of GradientBoostingRegressor doesn't work due to incompatible signatures.

import sklearn
import sklearn.pipeline
import sklearn.ensemble
import sklearn.decomposition
import sklearn.linear_model
import numpy as np
init = sklearn.pipeline.make_pipeline(sklearn.decomposition.PCA(), sklearn.linear_model.ElasticNet())
model = sklearn.ensemble.GradientBoostingRegressor(init=init)
x = np.random.rand(12, 3)
y = np.random.rand(12)
model.fit(x, y)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Thomas/.local/miniconda3/envs/4cast/lib/python3.6/site-packages/sklearn/ensemble/gradient_boosting.py", line 1421, in fit
    self.init_.fit(X, y, sample_weight)
TypeError: fit() takes from 2 to 3 positional arguments but 4 were given

The signature of Pipeline.fit is

# sklearn/pipeline.py
...
239 def fit(self, X, y=None, **fit_params):
...

which cannot be called with three positional arguments as above.

So I guess line 1421 in sklearn/ensemble/gradient_boosting.py should read
self.init_.fit(X, y, sample_weight=sample_weight) instead and the issue is solved. Right?

Versions

>>> sklearn.show_versions()

System:
    python: 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:14:59)  [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
executable: /Users/Thomas/.local/miniconda3/envs/test/bin/python
   machine: Darwin-18.2.0-x86_64-i386-64bit

BLAS:
    macros: SCIPY_MKL_H=None, HAVE_CBLAS=None
  lib_dirs: /Users/Thomas/.local/miniconda3/envs/test/lib
cblas_libs: mkl_rt, pthread

Python deps:
       pip: 10.0.1
setuptools: 39.2.0
   sklearn: 0.20.2
     numpy: 1.16.1
     scipy: 1.2.0
    Cython: None
    pandas: 0.24.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions