我有一个管道:
np.random.seed(42)
tf.random.set_seed(42)
pipeline = Pipeline([
('smote', SMOTE()),
('under',RandomUnderSampler()),
('cl', KerasClassifier(build_fn=create_model, verbose=0))
])
param_grid_pipeline = {
'smote__sampling_strategy':[.3],
'smote__random_state':[42],
'under__sampling_strategy':['auto'],
'under__random_state':[42],
'cl__batch_size':[128],
'cl__epochs':[20],
}
cv = StratifiedShuffleSplit(n_splits=40, test_size=0.2, random_state=42)
grid = GridSearchCV(estimator=pipeline, param_grid=param_grid_pipeline, cv=cv, scoring='f1', verbose=1, n_jobs=-1)
grid_result = grid.fit(X,y)
print("best_score_",grid_result.best_score_)
而 best_score_ 是 0.9981313067607172
但是,如果我从管道中排除重新采样并在外部执行它:
np.random.seed(42)
tf.random.set_seed(42)
over = SMOTE(sampling_strategy=0.3,random_state=42)
under = RandomUnderSampler(sampling_strategy='auto',random_state=42)
X,y = over.fit_resample(X,y)
X,y = under.fit_resample(X,y)
pipeline = Pipeline([
('cl', KerasClassifier(build_fn=create_model, verbose=0))
])
param_grid_pipeline = {
'cl__batch_size':[128],
'cl__epochs':[20],
}
cv = StratifiedShuffleSplit(n_splits=40, test_size=0.2, random_state=42)
grid = GridSearchCV(estimator=pipeline, param_grid=param_grid_pipeline, cv=cv, scoring='f1', verbose=1, n_jobs=-1)
grid_result = grid.fit(X,y)
print("best_score_",grid_result.best_score_)
而且我(在多次运行中)得到了更好的结果:0.9999888503305302
在管道外部使用重新采样有什么区别?