【问题标题】:Why the class I created in python is not recognizing the attributes set to False?为什么我在 python 中创建的类无法识别设置为 False 的属性?
【发布时间】:2021-09-18 21:22:46
【问题描述】:

我正在尝试在房价上竞争 - 高级回归技术 Kaggle 比赛

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

转换器有四个超参数 (add_baths, add_bsmt_baths, add_above_grade_baths, add_porch_area) 默认设置为 True。这个超参数可以让我轻松找出添加这个属性是否有助于机器学习算法。

但问题是,当我将这些超参数之一设置为 False 时,该类仍会返回该列,就像我将其设置为 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)

这里应该返回所有列,因为我设置了参数add_baths=False, add_bsmt_baths=False 它不应该创建T_Bath 也不应该创建T_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')

【问题讨论】:

  • 我们无法运行它,因此我们无法测试导致问题的原因 - 所以我们无法为您提供帮助。
  • 首先您可以使用print() 查看变量中的内容以及执行的代码部分。它被称为"print debuging"。也许你有一些与你期望不同的东西。或者它运行的功能可能与您预期的不同。
  • 数据集来自 kaggle 竞赛“房价 - 高级回归技术”
  • 您可以添加本次比赛的链接(但我记得可能需要登录 kaggle 才能访问数据)
  • 请查看minimal reproducible example 帮助页面。由于格式不正确,那里的代码至少有一个 SyntaxError;即使修复了明显的错误,也不清楚如何重现错误——请提供一些(小)示例输入和处理它所需的代码。理想情况下,我们应该能够直接复制/粘贴它以运行并重现错误。

标签: python oop scikit-learn data-transform


【解决方案1】:

如果不复制转换部分中的训练数据,您正在修改原始训练数据,因此如果您之前已经使用 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_

【讨论】:

  • 简单而优雅,正是我想要的。非常感谢!我不知道为什么我没有想出这个解决方案,也许我需要更多的练习
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 2015-04-09
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
相关资源
最近更新 更多