[英]Imblearn Pipeline resulting in poor metrics
我正在研究使用以下代码创建的不平衡数据集
X, y = make_classification(n_samples=10000, n_features=2, n_redundant=0, n_clusters_per_class=1,
weights=[0.99], flip_y=0, random_state=1)
我尝试使用 SMOTE 过采样消除不平衡,然后尝试拟合 ML model。 这是使用普通方法然后通过创建管道来完成的。
普通方法
from imblearn.over_sampling import SMOTE
oversampled_data = SMOTE(sampling_strategy=0.5)
X_over, y_over = oversampled_data.fit_resample(X, y)
logistic = LogisticRegression(solver='liblinear')
scoring = ['accuracy', 'precision', 'recall', 'f1']
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# evaluating the model
scores = cross_validate(logistic, X_over, y_over, scoring=scoring, cv=cv, n_jobs=-1, return_train_score=True)
print('Accuracy: {:.2f}, Precison: {:.2f}, Recall: {:.2f} F1: {:.2f}'.format(np.mean(scores['test_accuracy']), np.mean(scores['test_precision']), np.mean(scores['test_recall']), np.mean(scores['test_f1'])))
Output - 精度:0.93,精度: 0.92 ,召回率:0.86, F1:0.89
管道
from imblearn.pipeline import make_pipeline, Pipeline
from sklearn.model_selection import cross_val_score
oversampled_data = SMOTE(sampling_strategy=0.5)
pipeline = Pipeline([('smote', oversampled_data), ('model', LogisticRegression())])
# pipeline = make_pipeline(oversampled_data, logistic)
scoring = ['accuracy', 'precision', 'recall', 'f1']
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# evaluating the model
scores = cross_validate(pipeline, X, y, scoring=scoring, cv=cv, n_jobs=-1, return_train_score=True)
print('Accuracy: {:.2f}, Precison: {:.2f}, Recall: {:.2f}, F1: {:.2f}'.format(np.mean(scores['test_accuracy']), np.mean(scores['test_precision']), np.mean(scores['test_recall']), np.mean(scores['test_f1'])))
Output - 精度:0.96,精度: 0.19 ,召回率:0.84, F1:0.31
使用管道时我做错了什么,为什么使用管道时精度和 F1 分数这么差?
在第一种方法中,您在拆分训练集和测试集之前创建合成示例,而在第二种方法中,您在拆分后进行。
前一种方法将合成数据点添加到测试集中,但后者没有。 此外,前一种方法会因数据泄漏而产生夸大的分数:它(部分)基于训练数据集中的一些数据点添加合成测试样本。 参见例如
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.