为什么我在 python 中创建的 class 无法识别设置为 False 的属性?

Why the class I created in python is not recognizing the attributes set to False?

我正在尝试参加房价竞赛 - 高级回归技术 Kaggle 竞赛

我正在编写一个自定义转换器,它可以与添加组合属性的 Scikit-Learn 功能无缝协作

transformer 有四个超参数 (add_baths, add_bsmt_baths, add_above_grade_baths, add_porch_area) 默认设置为 True。这个超参数将使我能够轻松地找出添加这个属性是否有助于机器学习算法。

但问题是,当我将其中一个超参数设置为 False 时,class 仍然 return 将列发送给我,就好像我将它设置为 True

class CombinedAttributesAdder(BaseEstimator, TransformerMixin):
        def __init__(self, add_baths=True, add_bsmt_baths=True, add_above_grade_baths=True, add_porch_area=True):
            self.add_baths = add_baths
            self.add_bsmt_baths = add_bsmt_baths
            self.add_above_grade_baths = add_above_grade_baths
            self.add_porch_area = add_porch_area
        def fit(self, X, y=None):
            return self
        def transform(self, X):
            X['T_FlrSF'] = X['1stFlrSF'] + X['2ndFlrSF']
            if self.add_baths:
                X['T_Bath'] = X['BsmtFullBath'] + X['BsmtHalfBath'] + X['FullBath'] + X['HalfBath']
            if self.add_bsmt_baths:
                X['T_BsmtBath'] = X['BsmtFullBath'] + X['BsmtHalfBath']
            if self.add_above_grade_baths:
                X['T_agBath'] = X['FullBath'] + X['HalfBath']
            if self.add_porch_area:
                X['T_Porch'] = X['OpenPorchSF'] + X['EnclosedPorch'] + X['3SsnPorch'] + X['ScreenPorch']
            return X

    attr_adder = CombinedAttributesAdder(add_baths=False, add_bsmt_baths=False)
    housing_extra_attribs = attr_adder.transform(housing)

这里应该 return 所有的列,因为我设置了参数 add_baths=False, add_bsmt_baths=False 它不应该创建 T_BathT_BsmtBath

housing_extra_attribs.columns

...

 Index(['Id', 'MSSubClass', 'MSZoning', 'LotFrontage', 'LotArea', 'Street',
   'Alley', 'LotShape', 'LandContour', 'Utilities', 'LotConfig',
   'LandSlope', 'Neighborhood', 'Condition1', 'Condition2', 'BldgType',
   'HouseStyle', 'OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd',
   'RoofStyle', 'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType',
   'MasVnrArea', 'ExterQual', 'ExterCond', 'Foundation', 'BsmtQual',
   'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinSF1',
   'BsmtFinType2', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'Heating',
   'HeatingQC', 'CentralAir', 'Electrical', '1stFlrSF', '2ndFlrSF',
   'LowQualFinSF', 'GrLivArea', 'BsmtFullBath', 'BsmtHalfBath', 'FullBath',
   'HalfBath', 'BedroomAbvGr', 'KitchenAbvGr', 'KitchenQual',
   'TotRmsAbvGrd', 'Functional', 'Fireplaces', 'FireplaceQu', 'GarageType',
   'GarageYrBlt', 'GarageFinish', 'GarageCars', 'GarageArea', 'GarageQual',
   'GarageCond', 'PavedDrive', 'WoodDeckSF', 'OpenPorchSF',
   'EnclosedPorch', '3SsnPorch', 'ScreenPorch', 'PoolArea', 'PoolQC',
   'MiscFeature', 'MiscVal', 'MoSold', 'YrSold', 'SaleType',
   'SaleCondition', 'T_FlrSF', 'T_Bath', 'T_BsmtBath', 'T_agBath',
   'T_Porch'],
  dtype='object')

在不复制转换部分中的训练数据的情况下,您正在修改原始训练数据,因此如果您之前已经使用 True 属性对其进行了转换,那么列已经在较早的时候创建了。 请尝试以下操作,但要确保之前未修改训练数据:

class CombinedAttributesAdder(BaseEstimator, TransformerMixin):
        def __init__(self, add_baths=True, add_bsmt_baths=True, add_above_grade_baths=True, add_porch_area=True):
            self.add_baths = add_baths
            self.add_bsmt_baths = add_bsmt_baths
            self.add_above_grade_baths = add_above_grade_baths
            self.add_porch_area = add_porch_area
        def fit(self, X, y=None):
            return self
        def transform(self, X):
            X_=X.copy()
            X_['T_FlrSF'] = X_['1stFlrSF'] + X_['2ndFlrSF']
            if self.add_baths:
                X_['T_Bath'] = X_['BsmtFullBath'] + X_['BsmtHalfBath'] + X_['FullBath'] + X_['HalfBath']
            if self.add_bsmt_baths:
                X_['T_BsmtBath'] = X_['BsmtFullBath'] + X_['BsmtHalfBath']
            if self.add_above_grade_baths:
                X_['T_agBath'] = X_['FullBath'] + X_['HalfBath']
            if self.add_porch_area:
                X_['T_Porch'] = X_['OpenPorchSF'] + X_['EnclosedPorch'] + X_['3SsnPorch'] + X_['ScreenPorch']
            return X_