Elffuss BraidHub

kikocis/auto-code-rover public

main · 5.5 KiBRawPermalink
1ColumnTransformer with pandas output can't handle transformers with no features
2### Describe the bug
3
4Hi,
5
6ColumnTransformer doesn't deal well with transformers that apply to 0 features (categorical_features in the example below) when using "pandas" as output. It seems steps with 0 features are not fitted, hence don't appear in `self._iter(fitted=True)` (_column_transformer.py l.856) and hence break the input to the `_add_prefix_for_feature_names_out` function (l.859).
7
8
9### Steps/Code to Reproduce
10
11Here is some code to reproduce the error. If you remove .set_output(transform="pandas") on the line before last, all works fine. If you remove the ("categorical", ...) step, it works fine too.
12
13```python
14import numpy as np
15import pandas as pd
16from lightgbm import LGBMClassifier
17from sklearn.compose import ColumnTransformer
18from sklearn.impute import SimpleImputer
19from sklearn.pipeline import Pipeline
20from sklearn.preprocessing import RobustScaler
21
22X = pd.DataFrame(data=[[1.0, 2.0, 3.0, 4.0], [4, 2, 2, 5]],
23 columns=["a", "b", "c", "d"])
24y = np.array([0, 1])
25categorical_features = []
26numerical_features = ["a", "b", "c"]
27model_preprocessing = ("preprocessing",
28 ColumnTransformer([
29 ('categorical', 'passthrough', categorical_features),
30 ('numerical', Pipeline([("scaler", RobustScaler()),
31 ("imputer", SimpleImputer(strategy="median"))
32 ]), numerical_features),
33 ], remainder='drop'))
34pipeline = Pipeline([model_preprocessing, ("classifier", LGBMClassifier())]).set_output(transform="pandas")
35pipeline.fit(X, y)
36```
37
38### Expected Results
39
40The step with no features should be ignored.
41
42### Actual Results
43
44Here is the error message:
45```pytb
46Traceback (most recent call last):
47 File "/home/philippe/workspace/script.py", line 22, in <module>
48 pipeline.fit(X, y)
49 File "/home/philippe/.anaconda3/envs/deleteme/lib/python3.9/site-packages/sklearn/pipeline.py", line 402, in fit
50 Xt = self._fit(X, y, **fit_params_steps)
51 File "/home/philippe/.anaconda3/envs/deleteme/lib/python3.9/site-packages/sklearn/pipeline.py", line 360, in _fit
52 X, fitted_transformer = fit_transform_one_cached(
53 File "/home/philippe/.anaconda3/envs/deleteme/lib/python3.9/site-packages/joblib/memory.py", line 349, in __call__
54 return self.func(*args, **kwargs)
55 File "/home/philippe/.anaconda3/envs/deleteme/lib/python3.9/site-packages/sklearn/pipeline.py", line 894, in _fit_transform_one
56 res = transformer.fit_transform(X, y, **fit_params)
57 File "/home/philippe/.anaconda3/envs/deleteme/lib/python3.9/site-packages/sklearn/utils/_set_output.py", line 142, in wrapped
58 data_to_wrap = f(self, X, *args, **kwargs)
59 File "/home/philippe/.anaconda3/envs/deleteme/lib/python3.9/site-packages/sklearn/compose/_column_transformer.py", line 750, in fit_transform
60 return self._hstack(list(Xs))
61 File "/home/philippe/.anaconda3/envs/deleteme/lib/python3.9/site-packages/sklearn/compose/_column_transformer.py", line 862, in _hstack
62 output.columns = names_out
63 File "/home/philippe/.anaconda3/envs/deleteme/lib/python3.9/site-packages/pandas/core/generic.py", line 5596, in __setattr__
64 return object.__setattr__(self, name, value)
65 File "pandas/_libs/properties.pyx", line 70, in pandas._libs.properties.AxisProperty.__set__
66 File "/home/philippe/.anaconda3/envs/deleteme/lib/python3.9/site-packages/pandas/core/generic.py", line 769, in _set_axis
67 self._mgr.set_axis(axis, labels)
68 File "/home/philippe/.anaconda3/envs/deleteme/lib/python3.9/site-packages/pandas/core/internals/managers.py", line 214, in set_axis
69 self._validate_set_axis(axis, new_labels)
70 File "/home/philippe/.anaconda3/envs/deleteme/lib/python3.9/site-packages/pandas/core/internals/base.py", line 69, in _validate_set_axis
71 raise ValueError(
72ValueError: Length mismatch: Expected axis has 3 elements, new values have 0 elements
73
74Process finished with exit code 1
75```
76
77### Versions
78
79```shell
80System:
81 python: 3.9.15 (main, Nov 24 2022, 14:31:59) [GCC 11.2.0]
82executable: /home/philippe/.anaconda3/envs/strategy-training/bin/python
83 machine: Linux-5.15.0-57-generic-x86_64-with-glibc2.31
84
85Python dependencies:
86 sklearn: 1.2.0
87 pip: 22.2.2
88 setuptools: 62.3.2
89 numpy: 1.23.5
90 scipy: 1.9.3
91 Cython: None
92 pandas: 1.4.1
93 matplotlib: 3.6.3
94 joblib: 1.2.0
95threadpoolctl: 3.1.0
96
97Built with OpenMP: True
98
99threadpoolctl info:
100 user_api: openmp
101 internal_api: openmp
102 prefix: libgomp
103 filepath: /home/philippe/.anaconda3/envs/strategy-training/lib/python3.9/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
104 version: None
105 num_threads: 12
106
107 user_api: blas
108 internal_api: openblas
109 prefix: libopenblas
110 filepath: /home/philippe/.anaconda3/envs/strategy-training/lib/python3.9/site-packages/numpy.libs/libopenblas64_p-r0-742d56dc.3.20.so
111 version: 0.3.20
112threading_layer: pthreads
113 architecture: Haswell
114 num_threads: 12
115
116 user_api: blas
117 internal_api: openblas
118 prefix: libopenblas
119 filepath: /home/philippe/.anaconda3/envs/strategy-training/lib/python3.9/site-packages/scipy.libs/libopenblasp-r0-41284840.3.18.so
120 version: 0.3.18
121threading_layer: pthreads
122 architecture: Haswell
123 num_threads: 12
124```
125