Я не понимаю, как лучше всего собрать конвейер, если я делаю и простой кодировщик, и целевой кодировщик. Я нашел этот пример здесь , который иллюстрирует, что проблема связана с необходимостью передавать целевую переменную вместе с переменной, которая должна быть закодирована.
from examples.source_data.loaders import get_mushroom_data
from sklearn.compose import ColumnTransformer
from category_encoders import TargetEncoder
# get data from the mushroom dataset
X, y, _ = get_mushroom_data()
# encode the specified columns
ct = ColumnTransformer(
[
('Target encoding', TargetEncoder(), ['bruises', 'odor'])
], remainder='passthrough'
)
encoded = ct.fit_transform(X=X, y=y)
Однако вместо прямого выполняя fit_transform
, я хотел бы добавить его как часть моего конвейера, чтобы я мог делать это в рамках схемы перекрестной проверки.
Итак, код, который не работает:
pipeline_ordinal = Pipeline(steps=[('imputer', SimpleImputer(strategy='constant', fill_value='missing'))
,('ord encoding', ce.ordinal.OrdinalEncoder())])
pipeline_loo = Pipeline(steps=[('imputer', SimpleImputer(strategy='constant', fill_value='missing'))
,('loo encoding', ce.LeaveOneOutEncoder())])
preprocessor = ColumnTransformer(
transformers=[('simple', pipeline_ordinal, ['x1','x2','x3']),
('targetbased', pipeline_loo, ['x4','x5','y'])
])
rf = RandomForestRegressor()
pipe = Pipeline(steps=[('preprocessor', preprocessor),('regression', rf)])
gs = GridSearchCV(pipe, param_grid=params, cv = cv)
gs.fit(X, y)
Есть идеи, как лучше исправить это все вместе?
Изменить:
Проблема заключается в передаче X в gs.fit (). Как есть, код выше говорит: ValueError: A given column is not a column of the dataframe
Если я попытаюсь сообразить и отправить 'y' вместе с X, тогда он скажет мне ValueError: cannot reindex from a duplicate axis