问题描述
我希望这里有人能够帮助我调试我的部分代码.我正在尝试为爱荷华州艾姆斯市的 Kaggle 比赛房屋设计一个预测模型,我在实施我的管道时遇到了问题,因为我不断收到错误消息.这是我试图运行的代码
im hoping someone here will be able to help me debug a part of my code. I am trying to come up with a predictive model for the Ames, Iowa housing set for a Kaggle competition and im having an issue implementing my pipeline as I keep getting an error.here is the code I am trying to run
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.
我知道问题特别在于 cat_pipeline
.有谁知道问题可能是什么?感谢帮助
I know the issue is specifically the cat_pipeline
. does anyone know what the issue could be?thanks for the help
推荐答案
我想通了.我忘了在 cat_pipeline 中启动管道这就是它应该说的
I figured it out. I forgot to initiate the Pipeline in cat_pipelinethis is what it should say
cat_pipeline = Pipeline([ # HERE
('imputer', SimpleImputer(fill_value='none', strategy='constant')),
('one_hot', OneHotEncoder())
这篇关于ColumnTransformer 在 sklearn 中尝试 fit_transform 管道时生成 TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!