为什么我在python中创建的类不能识别设置为False的属性



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

我正在编写一个与Scikit Learn功能无缝配合的自定义转换器,该功能添加了组合属性

转换器具有默认设置为True的四个超参数(add_baths, add_bsmt_baths, add_above_grade_baths, add_porch_area(。这些超参数将使我能够很容易地发现添加这些属性是否有助于机器学习算法。

但问题是,当我将其中一个超参数设置为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_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_

最新更新