I try to do a tutorial on Pipeline for students but I block. I'm not an expert but I'm trying to improve. So thank you for your indulgence. In fact, I try in a pipeline to execute several steps in preparing a dataframe for a classifier:
- Step 1: Description of the dataframe
- Step 2: Fill NaN Values
- Step 3: Transforming Categorical Values into Numbers
Here is my code:
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 selfclass 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 Xdef fit(self, X,y=None):return selfclass 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 Xdef fit(self, X, y=None):return self
If I execute step 1 and 2 or step 1 and 3 it works but if I execute step 1, 2 and 3 at the same time. I have this error:
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'