Python ColumnTransformer尝试在sklearn中拟合_变换管道时生成类型错误
我希望这里有人能帮我调试部分代码。我正试图为爱荷华州艾姆斯的住房市场建立一个预测模型,以应对卡格尔竞争。我在实施我的管道时遇到了一个问题,因为我不断地遇到一个错误。 下面是我试图运行的代码Python ColumnTransformer尝试在sklearn中拟合_变换管道时生成类型错误,python,scikit-learn,Python,Scikit Learn,我希望这里有人能帮我调试部分代码。我正试图为爱荷华州艾姆斯的住房市场建立一个预测模型,以应对卡格尔竞争。我在实施我的管道时遇到了一个问题,因为我不断地遇到一个错误。 下面是我试图运行的代码 from sklearn.preprocessing import OneHotEncoder from sklearn.pipeline import Pipeline from sklearn.compose import ColumnTransformer from sklearn.preprocess
from sklearn.preprocessing import OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
num_attributes = list(train_set.select_dtypes(exclude=['object'])) #to select all num columns, we exclude any column with object types
cat_attributes = list(train_set.select_dtypes(include=['object'])) #here we select all columns with object types
cat_pipeline = ([
('imputer', SimpleImputer(fill_value='none', strategy='constant')),
('one_hot', OneHotEncoder())
])
full_pipeline = ColumnTransformer([
('num', StandardScaler(), num_attributes),
('cat', cat_pipeline, cat_attributes)
])
train_set_prepared = full_pipeline.fit_transform(train_set)
这是我收到的错误信息
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-14-abf9d30bdc2b> in <module>
20 ])
21
---> 22 train_set_prepared = full_pipeline.fit_transform(train_set)
~\Anaconda3\envs\ml_book\lib\site-packages\sklearn\compose\_column_transformer.py in fit_transform(self, X, y)
470 """
471 X = _check_X(X)
--> 472 self._validate_transformers()
473 self._validate_column_callables(X)
474 self._validate_remainder(X)
~\Anaconda3\envs\ml_book\lib\site-packages\sklearn\compose\_column_transformer.py in _validate_transformers(self)
277 "transform, or can be 'drop' or 'passthrough' "
278 "specifiers. '%s' (type %s) doesn't." %
--> 279 (t, type(t)))
280
281 def _validate_column_callables(self, X):
TypeError: All estimators should implement fit and transform, or can be 'drop' or 'passthrough' specifiers. '[('imputer', SimpleImputer(add_indicator=False, copy=True, fill_value='none',
missing_values=nan, strategy='constant', verbose=0)), ('one_hot', OneHotEncoder(categorical_features=None, categories=None, drop=None,
dtype=<class 'numpy.float64'>, handle_unknown='error',
n_values=None, sparse=True))]' (type <class 'list'>) doesn't.
--------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
20 ])
21
--->22列车组准备好=完整的管道。拟合变换(列车组)
~\Anaconda3\envs\ml\u book\lib\site packages\sklearn\compose\u column\u transformer.py in fit\u transform(self,X,y)
470 """
471 X=_check_X(X)
-->472自验证变压器()
473自我验证列可调用项(X)
474自验证余数(X)
~\Anaconda3\envs\ml\u book\lib\site packages\sklearn\compose\\u column\u transformer.py in\u validate\u transformers(self)
277“转换,也可以是“删除”或“传递”
278“说明符”。%s'(类型%s)不存在。“%
-->279(t,类型(t)))
280
281定义验证列可调用项(self,X):
TypeError:所有估计器都应该实现拟合和转换,或者可以是“drop”或“PASSTHROUP”说明符。[(“输入者”,SimpleInputer(添加指示符=False,复制=True,填充值=none),
缺少_值=nan,strategy='constant',verbose=0)),('one_hot',OneHotEncoder(Category_features=None,categories=None,drop=None,
数据类型=,句柄_unknown='error',
n_values=None,sparse=True))]'(类型)没有。
我知道问题具体是cat_管道
。有人知道可能是什么问题吗?
谢谢你的帮助我找到了答案。我忘了启动cat_管道中的管道 这是它应该说的
cat_pipeline = Pipeline([ # HERE
('imputer', SimpleImputer(fill_value='none', strategy='constant')),
('one_hot', OneHotEncoder())