Próbuję zrobić samouczek dotyczący Pipeline dla studentów, ale blokuję. Nie jestem ekspertem, ale staram się poprawić. Więc dziękuję za wyrozumiałość. W rzeczywistości, staram się w rurociągu, aby wykonać kilka kroków w przygotowaniu dataframe dla klasyfikatora:Używanie wielu niestandardowych klas za pomocą sklearn Pipeline (Python)
- Krok 1: opis dataframe
- Krok 2: Wypełnij NaN Wartości
- Etap 3: Przekształcenie kategoryczne wartości w liczbach
Oto mój kod:
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
Jeśli wykonam krok 1 i 2 lub krok 1 i 3 to działa, ale jeśli wykonam krok 1, 2 i 3 w tym samym czasie. Mam ten błąd:
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'
Prawdopodobnie niektórzy z nich None: 'X' lub' y'. Pełny stos proszę. – sergzach