简体   繁体   English

ColumnTransformer 在 sklearn 中尝试 fit_transform 管道时生成 TypeError

[英]ColumnTransformer generating a TypeError when trying to fit_transform pipeline in sklearn

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.我正在尝试为爱荷华州 Ames 的 Kaggle 比赛设计一个预测 model,但由于我不断收到错误,我在实施我的管道时遇到了问题。 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)

and this is the error message I am getting这是我收到的错误消息

--------------------------------------------------------------------------
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.

I know the issue is specifically the cat_pipeline .我知道这个问题特别是cat_pipeline does anyone know what the issue could be?有谁知道可能是什么问题? thanks for the help谢谢您的帮助

I figured it out.我想到了。 I forgot to initiate the Pipeline in cat_pipeline this is what it should say我忘了在 cat_pipeline 中启动管道这是它应该说的

cat_pipeline = Pipeline([ # HERE
    ('imputer', SimpleImputer(fill_value='none', strategy='constant')),
    ('one_hot', OneHotEncoder())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ColumnTransformer fit_transform 不适用于管道 - ColumnTransformer fit_transform not working with pipeline sklearn 中的 ColumnTransformer 实现没有定义 fit 方法,它只是自动调用 fit_transform? - ColumnTransformer implementation in sklearn doesn't have a fit method defined, it just automatically calls fit_transform? 为什么fit_transform在此sklearn Pipeline示例中不起作用? - Why doesn't fit_transform work in this sklearn Pipeline example? sklearn.compose.ColumnTransformer:fit_transform() 需要 2 个位置参数,但给出了 3 个 - sklearn.compose.ColumnTransformer: fit_transform() takes 2 positional arguments but 3 were given 矢量化fit_transform如何在sklearn中工作? - How vectorizer fit_transform work in sklearn? 使用sklearn时python中的fit,transform和fit_transform有什么区别? - What is difference between fit, transform and fit_transform in python when using sklearn? 在 sklearn 的管道中使用 LabelEncoder 给出:fit_transform 需要 2 个位置参数,但给出了 3 个 - Using a LabelEncoder in sklearn's Pipeline gives: fit_transform takes 2 positional arguments but 3 were given Sklearn-FeatureUnion-变形金刚:TypeError:fit_transform()接受2个位置参数,但给出了3个 - Sklearn - FeatureUnion - Transformer: TypeError: fit_transform() takes 2 positional arguments but 3 were given 管道中的项目何时调用fit_transform(),何时调用transform()? (scikit学习,管道) - When do items in the Pipeline call fit_transform(), and when do they call transform()? (scikit-learn, Pipeline) sklearn countvectorizer 中的 fit_transform 和 transform 有什么区别? - What is the difference between fit_transform and transform in sklearn countvectorizer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM