I'm making many data transformations and fitting a model using scikit pipeline, but I need to extract X_train and X_test right after transformations (imputer, encoding, etc) in order to use it for another analysis. How can I get it?
This is my pipeline:
imputer_num = SimpleImputer(strategy = 'median')
imputer_cat = SimpleImputer(strategy = 'most_frequent')
XGB = XGBClassifier()
BBC = BalancedBaggingClassifier()
BRC = BalancedRandomForestClassifier()
models = [XGB, BBC, BRC]
numeric_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy = 'median'))
,('scaler', StandardScaler())
])
categorical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy = 'most_frequent'))
,('encod', encoder)
])
preprocessor = ColumnTransformer(
transformers=[
('num', numeric_transformer, numericas_all)
,('cat', categorical_transformer, categoricas_all)
])
for item in models:
pipe = Pipeline(steps=[('preprocessor', preprocessor),('classifier', item)])
model = pipe.fit(X_train, y_train)
y_pred = model.predict(X_test)
test_probs = model.predict_proba(X_test)
print(model)
print(balanced_accuracy_score(y_test, y_pred))
print(roc_auc_score(y_test, y_pred))