【问题标题】:ColumnTransformer inside a Pipeline管道内的 ColumnTransformer
【发布时间】:2021-03-05 01:28:24
【问题描述】:

我正在 scikit-learn 中构建管道。我必须对不同的特性进行不同的转换,然后将它们全部标准化。所以我为每组列构建了一个带有自定义转换器的ColumnTransformer

transformation_pipeline = ColumnTransformer([
    ('adoption', TransformAdoptionFeatures, features_adoption),
    ('census', TransformCensusFeaturesRegr, features_census),
    ('climate', TransformClimateFeatures, features_climate),
    ('soil', TransformSoilFeatures, features_soil),
    ('economic', TransformEconomicFeatures, features_economic)
],
    remainder='drop')

然后,由于我想创建两个不同的管道来标准化和规范化我的功能,我正在考虑将transformation_pipeline 和缩放器合并到一个管道中:

full_pipeline_stand = Pipeline([
    ('transformation', transformation_pipeline()),
    ('scaling', StandardScaler())
])

但是,我收到以下错误:

TypeError: 'ColumnTransformer' object is not callable

有没有办法在不为每组列构建单独的管道(结合自定义转换器和缩放器)的情况下做到这一点?这显然有效,但对我来说似乎是无用的重复......谢谢!

【问题讨论】:

    标签: python scikit-learn pipeline


    【解决方案1】:

    我发现了我的错误,我正在切换类的实例化:自定义转换器必须在 ColumnTransformer 内实例化,而 ColumnTransformer 不必在管道内实例化。

    正确的代码如下:

    transformation_pipeline = ColumnTransformer([
        ('adoption', TransformAdoptionFeatures(), features_adoption),
        ('census', TransformCensusFeaturesRegr(), features_census),
        ('climate', TransformClimateFeatures(), features_climate),
        ('soil', TransformSoilFeatures(), features_soil),
        ('economic', TransformEconomicFeatures(), features_economic)
    ],
        remainder='drop')
    
    full_pipeline_stand = Pipeline([
        ('transformation', transformation_pipeline),
        ('scaling', StandardScaler())
    ])
    

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 1970-01-01
      • 2021-09-21
      • 2021-09-26
      • 1970-01-01
      • 2019-06-15
      • 2021-02-09
      • 2019-09-29
      • 2020-03-08
      相关资源
      最近更新 更多