python - “没有足够的值来解压(预期 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
我试过移除ohe2
和le
变形器,但它并没有改变任何东西。
解决方案
推荐阅读
- python - 如何在 Python 3 中有效地定义自定义全局比较器?
- php - 如何从数据库中的表中隔离单个结果?
- azure - Azure CosmoDB 部署失败并显示“资源操作已完成,终端预配状态为‘失败’。”
- html - 侧栏列上的 Bootstrap 置顶不起作用
- xml - 如何使用 solr post 工具来索引 xml?
- jquery - JQuery 自动完成显示所有项目,在输入时不会过滤
- mysql - 为列 A 的每个值选择列 B 的 N 个最频繁值
- algorithm - 恒定时间复杂度的大 O
- python - Get the first week in a row of consecutive weeks in DataFrame
- android - 如何启用 android 的状态 LED 或更改其颜色?