我试图比较在选择训练折叠之前过采样和过采样后的数据集上运行GridSearchCV的效果。我使用的过采样方法是随机过采样。
理解第一种方法是错误的,因为模型已经看到的观察结果渗入了测试集中。只是好奇这会造成多大的差异。
我生成了一个二进制分类数据集,其中包含以下内容:
# Generate binary classification dataset with 5% minority class,
# 3 informative features, introduce noise with flip_y = 15%
X, y = make_classification(n_samples=5000, n_features=3, n_informative=3,
n_redundant=0, n_repeated=0, n_classes=2,
n_clusters_per_class=1,
weights=[0.95, 0.05],
flip_y = 0.15,
class_sep=0.8)
我将其拆分为 60/40% 的训练/测试拆分,并在随机森林模型上使用这两种方法执行 GridSearchCV 。最终基于两种方法的 best_estimator_ 得到以下输出:
Best Params from Post-Oversampled Grid CV: {'n_estimators': 1000}
Best Params from Pre-Oversampled Grid CV: {'classifier__n_estimators': 500}
AUC of Post-Oversampled Grid CV - training set: 0.9996723239846446
AUC of Post-Oversampled Grid CV - test set: 0.6060618701968091
AUC of Pre-Oversampled Grid CV - training set: 0.6578310812852065
AUC of Pre-Oversampled Grid CV - test set: 0.6112671617024038
正如预期的那样,由于过度拟合,过采样后的网格 CV AUC 非常高。然而,在测试集上评估这两个模型会导致 AUC 上的结果非常相似(60.6% 对 61.1%)。
我有两个问题。为什么会出现这种情况?我没有为这些步骤中的任何一个分配 random_state 并重试了很多次,但仍然得到相同的结果。在这种情况下,由于它们在测试集上产生了相似的结果,什么会成为更好的模型?
为了通过管道进行过采样和处理,我使用了 imblearn:
# imblearn functions
from imblearn.over_sampling import RandomOverSampler
from imblearn.pipeline import Pipeline as Imb_Pipeline
如果需要,很高兴分享我的代码。谢谢。