【发布时间】: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