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

Skip to content

Unsupported multioutput stacking regressor #25597

Closed
@giuseta

Description

@giuseta

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

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