如何在Sklearn Pipelines中留下一个编码



我想测试使用Sklearn Pipelines中分类编码软件包中实现的不同编码策略。

我的意思是这样:

num_attribs = list(housing_num)
cat_attribs = ["ocean_proximity"]
num_pipeline = Pipeline([
        ('selector', DataFrameSelector(num_attribs)),
        ('imputer', Imputer(strategy="median")),
        ('std_scaler', StandardScaler()),
    ])
cat_pipeline = Pipeline([
        ('selector', DataFrameSelector(cat_attribs)),
        ('cat_encoder', LeaveOneOutEncoder()),
    ])
from sklearn.pipeline import FeatureUnion
full_pipeline = FeatureUnion(transformer_list=[
        ("num_pipeline", num_pipeline),
        ("cat_pipeline", cat_pipeline),
    ])
housing_prepared = full_pipeline.fit_transform(housing)
housing_prepared

但是我有一个错误:

TypeError: fit() missing 1 required positional argument: 'y'

有人可以建议解决方案吗?

让我像我一样显示代码的一部分。我添加XGBRegressor,因为我认为您可能会预测住房价格

class MultiColumn(BaseEstimator, TransformerMixin):
    def __init__(self,columns = None):
        self.columns = columns # array of column names to encode
    def fit(self,X,y=None):
        return self
    def transform(self, X):                                                           
        return X[self.columns]
NUMERIC = df[['var1', 'var2']]
CATEGORICAL = df[['var3', 'var4']]
class Imputation(BaseEstimator, TransformerMixin):
    def transform(self, X, y=None, **fit_params):
        return X.fillna(NUMERIC.median())
    def fit_transform(self, X, y=None, **fit_params):
        self.fit(X, y, **fit_params)
        return self.transform(X)
    def fit(self, X, y=None, **fit_params):
        return self
class Cat(BaseEstimator, TransformerMixin):
    def transform(self, X, y=None, **fit_params):
        enc = DictVectorizer(sparse = False)
        encc = enc.fit(CATEGORICAL.T.to_dict().values())
        enc_data = encc.transform(X.T.to_dict().values())
        enc_data[np.isnan(enc_data)] = 1
        return enc_data
    def fit_transform(self, X, y=None, **fit_params):
        self.fit(X, y, **fit_params)
        return self.transform(X)
    def fit(self, X, y=None, **fit_params):
        return self

和管道

pipeline = Pipeline([
# Use FeatureUnion to combine the features
('union', FeatureUnion(
    transformer_list=[
                # numeric
        ('numeric', Pipeline([
            ('selector', MultiColumn(columns=['var1', 'var2'])),
            ('imp', Imputation()),
       ('scaling', preprocessing.StandardScaler(with_mean = 0.))
        ])),
         # categorical
        ('categorical', Pipeline([
            ('selector', MultiColumn(columns=['var3', 'var4'])),
            ('one_hot', Cat()),
            (CategoricalImputer())
        ])),

    ])),

 ('model_fitting', xgb.XGBRegressor()),
])

您的分类编码器(LeaveOneOutEncoder(需要目标变量来调整和替换cat_attribs中定义的变量的新标签(级别(。因此,您只需要调用fit_transform方法与y_train

housing_prepared = full_pipeline.fit_transform(housing, y_train)

相关内容

  • 没有找到相关文章

最新更新