Skip to content
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

Unsupported multioutput stacking regressor #25597

Closed
giuseta opened this issue Feb 13, 2023 · 3 comments · Fixed by #25599
Closed

Unsupported multioutput stacking regressor #25597

giuseta opened this issue Feb 13, 2023 · 3 comments · Fixed by #25599

Comments

@giuseta
Copy link

giuseta commented Feb 13, 2023

Describe the bug

The method fit_transform of sklearn.ensemble.StackingRegressor, according to the documentation, should support as second argument (y) an array-like of shape (n_samples,) or (n_samples, n_outputs). However, if an array of shape (n_sample, n_outputs) is provided the following error is retrieved:

ValueError: y should be a 1d array, got an array of shape (100, 2) instead.

Steps/Code to Reproduce

data = np.random.rand(100, 10)
X = data[:, :8]
y = data[:, 8:]

estimators = [
    ('dt', DecisionTreeRegressor()),
    ('et', ExtraTreesRegressor())    
]
final_estimator = ElasticNet(max_iter=10000)
stacking_regressor = StackingRegressor(estimators=estimators, final_estimator=final_estimator)

stacking_regressor.fit_transform(X, y)

Expected Results

Train stacking model and transform input.

Actual Results

ValueError Traceback (most recent call last)
Cell In [14], line 12
9 final_estimator = ElasticNet(max_iter=10000)
10 stacking_regressor = StackingRegressor(estimators=estimators, final_estimator=final_estimator)
---> 12 stacking_regressor.fit_transform(X, y)

File /lib/python3.10/site-packages/sklearn/utils/_set_output.py:142, in _wrap_method_output..wrapped(self, X, *args, **kwargs)
140 @wraps(f)
141 def wrapped(self, X, *args, **kwargs):
--> 142 data_to_wrap = f(self, X, *args, **kwargs)
143 if isinstance(data_to_wrap, tuple):
144 # only wrap the first output for cross decomposition
145 return (
146 _wrap_data_with_container(method, data_to_wrap[0], X, self),
147 *data_to_wrap[1:],
148 )

File /lib/python3.10/site-packages/sklearn/base.py:862, in TransformerMixin.fit_transform(self, X, y, **fit_params)
859 return self.fit(X, **fit_params).transform(X)
860 else:
861 # fit method of arity 2 (supervised transformation)
--> 862 return self.fit(X, y, **fit_params).transform(X)

File /lib/python3.10/site-packages/sklearn/ensemble/_stacking.py:957, in StackingRegressor.fit(self, X, y, sample_weight)
...
-> 1202 raise ValueError(
1203 "y should be a 1d array, got an array of shape {} instead.".format(shape)
1204 )

ValueError: y should be a 1d array, got an array of shape (100, 2) instead.

Versions

System:
    python: 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]
executable: /bin/python
   machine: Linux-5.15.0-60-generic-x86_64-with-glibc2.35

Python dependencies:
      sklearn: 1.2.1
          pip: 22.0.2
   setuptools: 59.6.0
        numpy: 1.22.4
        scipy: 1.9.1
       Cython: 0.29.32
       pandas: 1.5.0
   matplotlib: 3.6.0
       joblib: 1.2.0
threadpoolctl: 3.1.0

Built with OpenMP: True

threadpoolctl info:
       user_api: openmp
   internal_api: openmp
         prefix: libgomp
       filepath: /lib/python3.10/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
        version: None
    num_threads: 12

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-2f7c42d4.3.18.so
        version: 0.3.18
threading_layer: pthreads
   architecture: Haswell
    num_threads: 12

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /lib/python3.10/site-packages/scipy.libs/libopenblasp-r0-9f9f5dbc.3.18.so
        version: 0.3.18
threading_layer: pthreads
   architecture: Haswell
    num_threads: 12
@giuseta giuseta added Bug Needs Triage Issue requires triage labels Feb 13, 2023
@giuseta giuseta changed the title Multioutput stacking regressor Unsupported bultioutput stacking regressor Feb 13, 2023
@giuseta giuseta changed the title Unsupported bultioutput stacking regressor Unsupported multioutput stacking regressor Feb 13, 2023
@thomasjpfan
Copy link
Member

thomasjpfan commented Feb 13, 2023

Thank you for opening the issue! In this case, the documentation is incorrect, because StackingRegressor can not fit on multioutput, so it can not fit_transform on multioutput either. The incorrect docstring comes from inheriting the docstring form TransformerMixin.fit_transform:

y : array-like of shape (n_samples,) or (n_samples, n_outputs), \
default=None
Target values (None for unsupervised transformations).

I opened #25599 to correct the docstring for StrackingRegressor.

@sanjail3
Copy link
Contributor

Can I work on this issue

@thomasjpfan
Copy link
Member

@sanjail3 As stated in #25597 (comment), I already opened #25599 to fix the docstring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants