使用Pipeline sklearn(Python)的多个自定义类

Jer*_*uez 6 python pipeline machine-learning pandas scikit-learn

我尝试为学生做一个关于Pipeline的教程,但我阻止了.我不是专家,但我正在努力改进.谢谢你的放纵.实际上,我尝试在管道中执行几个步骤来为分类器准备数据帧:

  • 第1步:数据帧的描述
  • 第2步:填写NaN值
  • 第3步:将分类值转换为数字

这是我的代码:

class Descr_df(object):

    def transform (self, X):
        print ("Structure of the data: \n {}".format(X.head(5)))
        print ("Features names: \n {}".format(X.columns))
        print ("Target: \n {}".format(X.columns[0]))
        print ("Shape of the data: \n {}".format(X.shape))

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

class Fillna(object):

    def transform(self, X):
        non_numerics_columns = X.columns.difference(X._get_numeric_data().columns)
        for column in X.columns:
            if column in non_numerics_columns:
                X[column] = X[column].fillna(df[column].value_counts().idxmax())
            else:
                 X[column] = X[column].fillna(X[column].mean())            
        return X

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

class Categorical_to_numerical(object):

    def transform(self, X):
        non_numerics_columns = X.columns.difference(X._get_numeric_data().columns)
        le = LabelEncoder()
        for column in non_numerics_columns:
            X[column] = X[column].fillna(X[column].value_counts().idxmax())
            le.fit(X[column])
            X[column] = le.transform(X[column]).astype(int)
        return X

    def fit(self, X, y=None):
        return self
Run Code Online (Sandbox Code Playgroud)

如果我执行步骤1和2或步骤1和3它可以工作但是如果我同时执行步骤1,2和3.我有这个错误:

pipeline = Pipeline([('df_intropesction', Descr_df()), ('fillna',Fillna()), ('Categorical_to_numerical', Categorical_to_numerical())])
pipeline.fit(X, y)
AttributeError: 'NoneType' object has no attribute 'columns'
Run Code Online (Sandbox Code Playgroud)

Viv*_*mar 5

出现此错误是因为在流水线中,第一个估计器的输出转到第二个,然后第二个估计器的输出转到第三个,依此类推...

Pipeline文档中

一个接一个地拟合所有变换并变换数据,然后使用最终估计器拟合变换后的数据。

因此,对于您的管道,执行步骤如下:

  1. Descr_df.fit(X) -> 不做任何事情并返回 self
  2. newX = Descr_df.transform(X) -> 应该返回一些分配给 newX 的值,该值应该传递给下一个估算器,但您的定义不返回任何内容(仅打印)。所以 None 被隐式返回
  3. Fillna.fit(newX) -> 不做任何事情并返回 self
  4. Fillna.transform(newX) -> 调用 newX.columns。但是 newX=None 来自 step2。因此错误。

解决方案:更改 Descr_df 的转换方法以按原样返回数据帧:

def transform (self, X):
    print ("Structure of the data: \n {}".format(X.head(5)))
    print ("Features names: \n {}".format(X.columns))
    print ("Target: \n {}".format(X.columns[0]))
    print ("Shape of the data: \n {}".format(X.shape))
    return X
Run Code Online (Sandbox Code Playgroud)

建议:让您的类继承自 scikit 中的 Base Estimator 和 Transformer 类,以确认良好实践。

即改变class Descr_df(object)class Descr_df(BaseEstimator, TransformerMixin)Fillna(object)Fillna(BaseEstimator, TransformerMixin)等等。

有关 Pipeline 中自定义类的更多详细信息,请参见此示例: