0

下午,我正在尝试运行我的管道,但我一直遇到错误'numpy.ndarray' object has no attribute 'fit',我不知道为什么。下面是转换器、管道和数据帧的代码。任何提示表示赞赏:

    import pandas as pd
    import numpy as np
    data = pd.read_csv("CustomTransformerData.csv")
    data

    from sklearn.base import BaseEstimator, TransformerMixin

    #column index
    x1_ix, x2_ix, x3_ix, x4_ix, x5_ix = 0,1,2,3,4

    class Assignment4Transformer(BaseEstimator, TransformerMixin):
        def __init__(self, add_x6 = True, y = None):
          self.add_x6 = add_x6
        def fit(self, data, y=None):
          return self
        def transform(self, data):
          if self.add_x6:
            y=[]
            x4 = (x1_ix**3) / (x5_ix)
            y.append(x4)
            x1 = (x1_ix**3) / (x5_ix)
            y.append(x1)
            x2 = (x1_ix**3) / (x5_ix)
            y.append(x2)
            x3 = (x1_ix**3) / (x5_ix)
            y.append(x3)
            x5 = (x1_ix**3) / (x5_ix)
            y.append(x5)
            x6 = (x1_ix**3) / (x5_ix)
            y.append(x6)
        
             return y


    attr_adder = Assignment4Transformer(add_x6 = True)
    assignment4_extra_attribs = attr_adder.transform(data)

    from sklearn.pipeline import Pipeline
    from sklearn.impute import SimpleImputer
    from sklearn.preprocessing import StandardScaler
    num_pipeline=Pipeline([
                       ('imputer',SimpleImputer(strategy="mean")), 
                       ('attribs_adder', Assignment4Transformer), 
                       ('std_scaler', StandardScaler())
    ])

    data_num = data.drop("x3", axis = 1)
    data_cat = data.drop(["x1", "x2", "x4", "x5"], axis = 1)

    data_num_transformed = num_pipeline.fit_transform(data_num)
    
    Output of last line:
     error 'numpy.ndarray' object has no attribute 'fit
4

1 回答 1

0

在第 4 行中,存在一个附加数据变量。你删除它并检查。转换函数的返回语句也应该与前一行的缩进匹配

y.append(x6)

检查这两点并分享结果。

于 2021-04-21T11:18:16.923 回答