首页 > 解决方案 > “没有足够的值来解压(预期 3,得到 2)”与 ColumnTransfomer

问题描述

我正在尝试创建一个ColumnTransformer

ct = ColumnTransformer([
    ('ohe1', OneHotEncoder(handle_unknown = "ignore"), "workclass"),
    ('ohe2', OneHotEncoder(handle_unknown = "ignore"), slice(5, 10)),
    ('ohe3', OneHotEncoder(handle_unknown = "ignore"), "native-country"),
    ('le', LabelEncoder(), slice(3,4)),
    ('imputer', SimpleImputer()),
    ('normalizer', StandardScaler(with_mean=False))])

但继续面临这个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-102-8ff2acc2111f> in <module>
----> 1 data = ct.fit_transform(data)

~/anaconda3/lib/python3.8/site-packages/sklearn/compose/_column_transformer.py in fit_transform(self, X, y)
    501         # set n_features_in_ attribute
    502         self._check_n_features(X, reset=True)
--> 503         self._validate_transformers()
    504         self._validate_column_callables(X)
    505         self._validate_remainder(X)

~/anaconda3/lib/python3.8/site-packages/sklearn/compose/_column_transformer.py in _validate_transformers(self)
    279             return
    280 
--> 281         names, transformers, _ = zip(*self.transformers)
    282 
    283         # validate names

ValueError: not enough values to unpack (expected 3, got 2)

这是我的数据信息:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 32561 entries, 0 to 32560
Data columns (total 15 columns):
 #   Column          Non-Null Count  Dtype 
---  ------          --------------  ----- 
 0   age             32561 non-null  int64 
 1   workclass       32561 non-null  object
 2   fnlwgt          32561 non-null  int64 
 3   education       32561 non-null  object
 4   education-num   32561 non-null  int64 
 5   marital-status  32561 non-null  object
 6   occupation      32561 non-null  object
 7   relationship    32561 non-null  object
 8   race            32561 non-null  object
 9   sex             32561 non-null  object
 10  capital-gain    32561 non-null  int64 
 11  capital-loss    32561 non-null  int64 
 12  hours-per-week  32561 non-null  int64 
 13  native-country  32561 non-null  object
 14  income          32561 non-null  object
dtypes: int64(6), object(9)
memory usage: 3.7+ MB

我试过移除ohe2le变形器,但它并没有改变任何东西。

标签: pythonmachine-learningscikit-learn

解决方案


推荐阅读