如何在管道中进行正确的索引引用?

2024-09-30 19:36:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个类来转换一些类型,然后在管道中使用它。我的问题是,由于索引,我在转换类型时遇到了一些问题:

    IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

这是我的班级:

    class ColumnTreatment(BaseEstimator, TransformerMixin):
def __init__(self, column = True):
    self.column = column

def fit(self, X,y = None):
    return self

def transform(self, X, y = None):
    if self.column:
        X.loc[:,'BsmtFinSF1'] = X['BsmtFinSF1'].astype('int64')
        X.loc[:,'BsmtFinSF2'] = X['BsmtFinSF2'].astype('int64')
        X.loc[:,'BsmtUnfSF'] = X['BsmtUnfSF'].astype('int64')
        X.loc[:,'TotalBsmtSF'] = X['TotalBsmtSF'].astype('int64')
        X.loc[:,'BsmtFullBath'] = X['BsmtFullBath'].astype('int64')
        X.loc[:,'BsmtHalfBath'] = X['BsmtHalfBath'].astype('int64')
        X.loc[:,'GarageCars'] = X['GarageCars'].astype('int64')
        X.loc[:,'GarageArea'] = X['GarageArea'].astype('int64')

而且,这是管道。我还注意确保在转换之前填写NA:

    preprocessing_pipeline = make_pipeline(
ColumnSelector(columns=['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', 'Fence', 'MiscFeature', 'MiscVal',
   'MoSold', 'YrSold', 'SaleType', 'SaleCondition']),
FeatureUnion(transformer_list = [
('Numbers', make_pipeline(
    SelectType(np.number), SimpleImputer(strategy='constant', fill_value = 0), ColumnTreatment(), StandardScaler())),
('Object', make_pipeline(
    SelectType('object'), SimpleImputer(strategy='constant', fill_value = 'No'), OneHotEncoder(handle_unknown="ignore")))]))

这里的任何指导都会非常有用

谢谢


Tags: selfnonepipelinedefcolumnlocastypeint64