0

我正在研究使用以下代码创建的不平衡数据集

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 模型。这是使用普通方法然后通过创建管道来完成的。

普通法

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'])))

输出 - 准确度: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'])))

输出 - 准确度:0.96,精度:0.19,召回:0.84,F1:0.31

使用管道时我做错了什么,为什么使用管道时精度和 F1 分数这么差?

4

1 回答 1

2

在第一种方法中,您在拆分训练集和测试集之前创建合成示例,而在第二种方法中,您拆分后进行。

前一种方法将合成数据点添加到测试集中,但后者没有。此外,前一种方法会因数据泄漏而产生夸大的分数:它(部分)基于训练数据集中的一些数据点添加合成测试样本。参见例如

于 2020-07-27T19:07:21.837 回答